Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

获取中文首字母(常用于通讯录) #6

Open
54leibo opened this issue Nov 6, 2019 · 0 comments
Open

获取中文首字母(常用于通讯录) #6

54leibo opened this issue Nov 6, 2019 · 0 comments

Comments

@54leibo
Copy link
Owner

54leibo commented Nov 6, 2019

/**
 * Get chinese first spell letter 
 *
 * @params {String} c: chinese
 * @return {String} letter
*
function getChineseFirstSpellLetter(c) {
  const classify = 'ABCDEFGHJKLMNOPQRSTWXYZ';
  const boundaryChar = '驁簿錯鵽樲鰒餜靃攟鬠纙鞪黁漚曝裠鶸蜶籜鶩鑂韻糳';
  let idx = -1;
  if(!String.prototype.localeCompare) {
    throw Error('String.prototype.localeCompare not supported.');
  }
  if(/[^\u4e00-\u9fa5]/.test(c)) {
    return c;
  }
  for (let i = 0; i < boundaryChar.length; i++) {
    if(boundaryChar[i].localeCompare(c, 'zh-CN-u-co-pinyin') >= 0) {
      idx = i;
      break;
    }
  }
  return classify[idx];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant