UNPKG

voca

Version:

The ultimate JavaScript string library

34 lines (30 loc) 1.23 kB
import './internal/is_nil.js'; import './is_string.js'; import { c as coerceToString } from './internal/coerce_to_string.js'; import { R as REGEXP_UNICODE_CHARACTER } from './internal/const.js'; import { n as nilDefault } from './internal/nil_default.js'; /** * Splits `subject` into an array of graphemes 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 graphemes * @static * @since 1.0.0 * @memberOf Split * @param {string} [subject=''] The string to split into characters. * @return {Array} Returns the array of graphemes. * @example * v.graphemes('\uD835\uDC00\uD835\uDC01'); // or '𝐀𝐁' * // => ['\uD835\uDC00', '\uD835\uDC01'], or * // ['𝐀', '𝐁'] * * v.graphemes('cafe\u0301'); // or 'café' * // => ['c', 'a', 'f', 'e\u0301'], or * // ['c', 'a', 'f', 'é'] */ function graphemes(subject) { var subjectString = coerceToString(subject); return nilDefault(subjectString.match(REGEXP_UNICODE_CHARACTER), []); } export default graphemes;