WordCharacters.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 isRegExpRecord = require('../helpers/records/regexp-record');
  9. var OwnPropertyKeys = require('../helpers/OwnPropertyKeys');
  10. var basicWordChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'; // step 1
  11. // https://262.ecma-international.org/14.0/#sec-runtime-semantics-wordcharacters-abstract-operation
  12. module.exports = function WordCharacters(rer) {
  13. if (!isRegExpRecord(rer)) {
  14. throw new $TypeError('Assertion failed: `rer` must be a RegExp Record');
  15. }
  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. };