index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict"
  2. // Based on: https://github.com/lodash/lodash/blob/6018350ac10d5ce6a5b7db625140b82aeab804df/.internal/unicodeSize.js
  3. module.exports = () => {
  4. // Used to compose unicode character classes.
  5. const astralRange = "\\ud800-\\udfff"
  6. const comboMarksRange = "\\u0300-\\u036f"
  7. const comboHalfMarksRange = "\\ufe20-\\ufe2f"
  8. const comboSymbolsRange = "\\u20d0-\\u20ff"
  9. const comboMarksExtendedRange = "\\u1ab0-\\u1aff"
  10. const comboMarksSupplementRange = "\\u1dc0-\\u1dff"
  11. const comboRange = comboMarksRange + comboHalfMarksRange + comboSymbolsRange + comboMarksExtendedRange + comboMarksSupplementRange
  12. const varRange = "\\ufe0e\\ufe0f"
  13. const familyRange = "\\uD83D\\uDC69\\uD83C\\uDFFB\\u200D\\uD83C\\uDF93"
  14. // Used to compose unicode capture groups.
  15. const astral = `[${astralRange}]`
  16. const combo = `[${comboRange}]`
  17. const fitz = "\\ud83c[\\udffb-\\udfff]"
  18. const modifier = `(?:${combo}|${fitz})`
  19. const nonAstral = `[^${astralRange}]`
  20. const regional = "(?:\\uD83C[\\uDDE6-\\uDDFF]){2}"
  21. const surrogatePair = "[\\ud800-\\udbff][\\udc00-\\udfff]"
  22. const zwj = "\\u200d"
  23. const blackFlag = "(?:\\ud83c\\udff4\\udb40\\udc67\\udb40\\udc62\\udb40(?:\\udc65|\\udc73|\\udc77)\\udb40(?:\\udc6e|\\udc63|\\udc6c)\\udb40(?:\\udc67|\\udc74|\\udc73)\\udb40\\udc7f)"
  24. const family = `[${familyRange}]`
  25. // Used to compose unicode regexes.
  26. const optModifier = `${modifier}?`
  27. const optVar = `[${varRange}]?`
  28. const optJoin = `(?:${zwj}(?:${[nonAstral, regional, surrogatePair].join("|")})${optVar + optModifier})*`
  29. const seq = optVar + optModifier + optJoin
  30. const nonAstralCombo = `${nonAstral}${combo}?`
  31. const symbol = `(?:${[nonAstralCombo, combo, regional, surrogatePair, astral, family].join("|")})`
  32. // Used to match [String symbols](https://mathiasbynens.be/notes/javascript-unicode).
  33. return new RegExp(`${blackFlag}|${fitz}(?=${fitz})|${symbol + seq}`, "g")
  34. }