Skip to content

Instantly share code, notes, and snippets.

@quanon
Created July 4, 2017 06:10
Show Gist options
  • Select an option

  • Save quanon/b2ee4e9c7361fe5ce7801ceb2c1e1ffc to your computer and use it in GitHub Desktop.

Select an option

Save quanon/b2ee4e9c7361fe5ce7801ceb2c1e1ffc to your computer and use it in GitHub Desktop.
文章の文字数を、改行や空白文字などを除いてカウントする。
/* @flow */
import { ucs2 } from 'punycode';
export default (string: string): number => {
if (!string) return 0;
const processedString: string =
string
.trim()
.replace(/[\n\r\s\u200B]+/g, ''); // U+200B: ZERO WIDTH SPACE
/*
* processedString.length では、サロゲートペアで表現される文字 (基本多言語面 (BMP) 以外の文字) を
* 2 と符号単位数でカウントしてしまう。
* そこで punycode.ucs2.decode() で Unicode 符号位置の配列に変換することで、
* 符号単位ではなく符号位置の数をカウントする。
*/
const codePoints: Array<number> = ucs2.decode(processedString);
return codePoints.length;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment