[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
| 4 |
|
---|
| 5 | var callBound = require('call-bind/callBound');
|
---|
| 6 | var $indexOf = callBound('String.prototype.indexOf', true);
|
---|
| 7 |
|
---|
| 8 | var Canonicalize = require('./Canonicalize');
|
---|
| 9 |
|
---|
| 10 | var caseFolding = require('../helpers/caseFolding.json');
|
---|
| 11 | var forEach = require('../helpers/forEach');
|
---|
| 12 | var isRegExpRecord = require('../helpers/records/regexp-record');
|
---|
| 13 | var OwnPropertyKeys = require('../helpers/OwnPropertyKeys');
|
---|
| 14 |
|
---|
| 15 | var basicWordChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'; // step 1
|
---|
| 16 |
|
---|
| 17 | // https://262.ecma-international.org/14.0/#sec-runtime-semantics-wordcharacters-abstract-operation
|
---|
| 18 |
|
---|
| 19 | module.exports = function WordCharacters(rer) {
|
---|
| 20 | if (!isRegExpRecord(rer)) {
|
---|
| 21 | throw new $TypeError('Assertion failed: `rer` must be a RegExp Record');
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | var extraWordChars = '';
|
---|
| 25 | forEach(OwnPropertyKeys(caseFolding.C), 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.C[c]; // step 3
|
---|
| 31 | }
|
---|
| 32 | });
|
---|
| 33 | forEach(OwnPropertyKeys(caseFolding.S), function (c) {
|
---|
| 34 | if (
|
---|
| 35 | $indexOf(basicWordChars, c) === -1 // c not in A
|
---|
| 36 | && $indexOf(basicWordChars, Canonicalize(rer, c)) > -1 // canonicalized c IS in A
|
---|
| 37 | ) {
|
---|
| 38 | extraWordChars += caseFolding.S[c]; // step 3
|
---|
| 39 | }
|
---|
| 40 | });
|
---|
| 41 |
|
---|
| 42 | if ((!rer['[[Unicode]]'] || !rer['[[IgnoreCase]]']) && extraWordChars.length > 0) {
|
---|
| 43 | throw new $TypeError('Assertion failed: `extraWordChars` must be empty when `rer.[[IgnoreCase]]` and `rer.[[Unicode]]` are not both true'); // step 3
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | return basicWordChars + extraWordChars; // step 4
|
---|
| 47 | };
|
---|