UNPKG

voca

Version:

The ultimate JavaScript string library

35 lines (30 loc) 1.14 kB
'use strict'; require('./internal/is_nil.js'); require('./is_string.js'); var coerce_to_string = require('./internal/coerce_to_string.js'); var _const = require('./internal/const.js'); /** * Counts the graphemes in `subject` taking care of * <a href="https://rainsoft.io/what-every-javascript-developer-should-know-about-unicode/#24surrogatepairs">surrogate pairs</a> and * <a href="https://rainsoft.io/what-every-javascript-developer-should-know-about-unicode/#25combiningmarks">combining marks</a>. * * @function countGraphemes * @static * @since 1.0.0 * @memberOf Count * @param {string} [subject=''] The string to count graphemes. * @return {number} Returns the number of graphemes in `subject`. * @example * v.countGraphemes('cafe\u0301'); // or 'café' * // => 4 * * v.countGraphemes('\uD835\uDC00\uD835\uDC01'); // or '𝐀𝐁' * // => 2 * * v.countGraphemes('rain'); * // => 4 */ function countGrapheme(subject) { return coerce_to_string.coerceToString(subject).replace(_const.REGEXP_COMBINING_MARKS, '*').replace(_const.REGEXP_SURROGATE_PAIRS, '*').length; } module.exports = countGrapheme;