WordCharacters.js 1.5 KB

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