WordCharacters.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 assertRecord = require('../helpers/assertRecord');
  9. var caseFolding = require('../helpers/caseFolding');
  10. var forEach = require('../helpers/forEach');
  11. var OwnPropertyKeys = require('../helpers/OwnPropertyKeys');
  12. var basicWordChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'; // step 1
  13. // https://262.ecma-international.org/14.0/#sec-runtime-semantics-wordcharacters-abstract-operation
  14. module.exports = function WordCharacters(rer) {
  15. assertRecord(Type, 'RegExp Record', 'rer', rer);
  16. var extraWordChars = '';
  17. forEach(OwnPropertyKeys(caseFolding.C), function (c) {
  18. if (
  19. $indexOf(basicWordChars, c) === -1 // c not in A
  20. && $indexOf(basicWordChars, Canonicalize(rer, c)) > -1 // canonicalized c IS in A
  21. ) {
  22. extraWordChars += caseFolding.C[c]; // step 3
  23. }
  24. });
  25. forEach(OwnPropertyKeys(caseFolding.S), function (c) {
  26. if (
  27. $indexOf(basicWordChars, c) === -1 // c not in A
  28. && $indexOf(basicWordChars, Canonicalize(rer, c)) > -1 // canonicalized c IS in A
  29. ) {
  30. extraWordChars += caseFolding.S[c]; // step 3
  31. }
  32. });
  33. if ((!rer['[[Unicode]]'] || !rer['[[IgnoreCase]]']) && extraWordChars.length > 0) {
  34. throw new $TypeError('Assertion failed: `extraWordChars` must be empty when `rer.[[IgnoreCase]]` and `rer.[[Unicode]]` are not both true'); // step 3
  35. }
  36. return basicWordChars + extraWordChars; // step 4
  37. };