_unicodeSize.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /** Used to compose unicode character classes. */
  2. var rsAstralRange = '\\ud800-\\udfff',
  3. rsComboMarksRange = '\\u0300-\\u036f',
  4. reComboHalfMarksRange = '\\ufe20-\\ufe2f',
  5. rsComboSymbolsRange = '\\u20d0-\\u20ff',
  6. rsComboRange = rsComboMarksRange + reComboHalfMarksRange + rsComboSymbolsRange,
  7. rsVarRange = '\\ufe0e\\ufe0f';
  8. /** Used to compose unicode capture groups. */
  9. var rsAstral = '[' + rsAstralRange + ']',
  10. rsCombo = '[' + rsComboRange + ']',
  11. rsFitz = '\\ud83c[\\udffb-\\udfff]',
  12. rsModifier = '(?:' + rsCombo + '|' + rsFitz + ')',
  13. rsNonAstral = '[^' + rsAstralRange + ']',
  14. rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}',
  15. rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]',
  16. rsZWJ = '\\u200d';
  17. /** Used to compose unicode regexes. */
  18. var reOptMod = rsModifier + '?',
  19. rsOptVar = '[' + rsVarRange + ']?',
  20. rsOptJoin = '(?:' + rsZWJ + '(?:' + [rsNonAstral, rsRegional, rsSurrPair].join('|') + ')' + rsOptVar + reOptMod + ')*',
  21. rsSeq = rsOptVar + reOptMod + rsOptJoin,
  22. rsSymbol = '(?:' + [rsNonAstral + rsCombo + '?', rsCombo, rsRegional, rsSurrPair, rsAstral].join('|') + ')';
  23. /** Used to match [string symbols](https://mathiasbynens.be/notes/javascript-unicode). */
  24. var reUnicode = RegExp(rsFitz + '(?=' + rsFitz + ')|' + rsSymbol + rsSeq, 'g');
  25. /**
  26. * Gets the size of a Unicode `string`.
  27. *
  28. * @private
  29. * @param {string} string The string inspect.
  30. * @returns {number} Returns the string size.
  31. */
  32. function unicodeSize(string) {
  33. var result = reUnicode.lastIndex = 0;
  34. while (reUnicode.test(string)) {
  35. ++result;
  36. }
  37. return result;
  38. }
  39. module.exports = unicodeSize;