WordCharacters.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var callBound = require('call-bind/callBound');
  4. var $indexOf = callBound('String.prototype.indexOf', true);
  5. var Canonicalize = require('./Canonicalize');
  6. var caseFolding = require('../helpers/caseFolding.json');
  7. var forEach = require('../helpers/forEach');
  8. var OwnPropertyKeys = require('../helpers/OwnPropertyKeys');
  9. var A = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'; // step 1
  10. // https://262.ecma-international.org/8.0/#sec-runtime-semantics-wordcharacters-abstract-operation
  11. module.exports = function WordCharacters(IgnoreCase, Unicode) {
  12. if (typeof IgnoreCase !== 'boolean' || typeof Unicode !== 'boolean') {
  13. throw new $TypeError('Assertion failed: `IgnoreCase` and `Unicode` must be booleans');
  14. }
  15. var U = '';
  16. forEach(OwnPropertyKeys(caseFolding.C), function (c) {
  17. if (
  18. $indexOf(A, c) === -1 // c not in A
  19. && $indexOf(A, Canonicalize(c, IgnoreCase, Unicode)) > -1 // canonicalized c IS in A
  20. ) {
  21. U += caseFolding.C[c]; // step 3
  22. }
  23. });
  24. forEach(OwnPropertyKeys(caseFolding.S), function (c) {
  25. if (
  26. $indexOf(A, c) === -1 // c not in A
  27. && $indexOf(A, Canonicalize(c, IgnoreCase, Unicode)) > -1 // canonicalized c IS in A
  28. ) {
  29. U += caseFolding.S[c]; // step 3
  30. }
  31. });
  32. if ((!Unicode || !IgnoreCase) && U.length > 0) {
  33. throw new $TypeError('Assertion failed: `U` must be empty when `IgnoreCase` and `Unicode` are not both true'); // step 4
  34. }
  35. return A + U; // step 5, 6
  36. };