| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var $TypeError = require('es-errors/type');
|
|---|
| 4 | var callBound = require('call-bound');
|
|---|
| 5 | var hasOwn = require('hasown');
|
|---|
| 6 |
|
|---|
| 7 | var isRegExpRecord = require('../helpers/records/regexp-record');
|
|---|
| 8 |
|
|---|
| 9 | var $charAt = callBound('String.prototype.charAt');
|
|---|
| 10 |
|
|---|
| 11 | // https://262.ecma-international.org/16.0/#sec-updatemodifiers
|
|---|
| 12 |
|
|---|
| 13 | module.exports = function UpdateModifiers(rer, add, remove) {
|
|---|
| 14 | if (!isRegExpRecord(rer)) {
|
|---|
| 15 | throw new $TypeError('Assertion failed: `rer` must be a RegExp Record');
|
|---|
| 16 | }
|
|---|
| 17 | if (typeof add !== 'string') {
|
|---|
| 18 | throw new $TypeError('Assertion failed: `add` must be a string');
|
|---|
| 19 | }
|
|---|
| 20 | if (typeof remove !== 'string') {
|
|---|
| 21 | throw new $TypeError('Assertion failed: `remove` must be a string');
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | // 1. Assert: add and remove have no elements in common.
|
|---|
| 25 | var adds = { __proto__: null };
|
|---|
| 26 | var removes = { __proto__: null };
|
|---|
| 27 | for (var i = 0; i < add.length; i++) {
|
|---|
| 28 | var toAdd = $charAt(add, i);
|
|---|
| 29 | adds[toAdd] = true;
|
|---|
| 30 | }
|
|---|
| 31 | for (var j = 0; j < remove.length; j++) {
|
|---|
| 32 | var toRemove = $charAt(remove, j);
|
|---|
| 33 | if (hasOwn(adds, toRemove)) {
|
|---|
| 34 | throw new $TypeError('Assertion failed: `add` and `remove` have elements in common');
|
|---|
| 35 | }
|
|---|
| 36 | removes[toRemove] = true;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | var ignoreCase = rer['[[IgnoreCase]]']; // step 2
|
|---|
| 40 |
|
|---|
| 41 | var multiline = rer['[[Multiline]]']; // step 3
|
|---|
| 42 |
|
|---|
| 43 | var dotAll = rer['[[DotAll]]']; // step 4
|
|---|
| 44 |
|
|---|
| 45 | var unicode = rer['[[Unicode]]']; // step 5
|
|---|
| 46 |
|
|---|
| 47 | var unicodeSets = rer['[[UnicodeSets]]']; // step 6
|
|---|
| 48 |
|
|---|
| 49 | var capturingGroupsCount = rer['[[CapturingGroupsCount]]']; // step 7
|
|---|
| 50 |
|
|---|
| 51 | if (hasOwn(removes, 'i')) {
|
|---|
| 52 | ignoreCase = false; // step 8
|
|---|
| 53 | } else if (hasOwn(adds, 'i')) {
|
|---|
| 54 | ignoreCase = true; // step 9
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if (hasOwn(removes, 'm')) {
|
|---|
| 58 | multiline = false; // step 10
|
|---|
| 59 | } else if (hasOwn(adds, 'm')) {
|
|---|
| 60 | multiline = true; // step 11
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | if (hasOwn(removes, 's')) {
|
|---|
| 64 | dotAll = false; // step 12
|
|---|
| 65 | } else if (hasOwn(adds, 's')) {
|
|---|
| 66 | dotAll = true; // step 13
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | return {
|
|---|
| 70 | '[[IgnoreCase]]': !!ignoreCase,
|
|---|
| 71 | '[[Multiline]]': !!multiline,
|
|---|
| 72 | '[[DotAll]]': !!dotAll,
|
|---|
| 73 | '[[Unicode]]': !!unicode,
|
|---|
| 74 | '[[UnicodeSets]]': !!unicodeSets,
|
|---|
| 75 | '[[CapturingGroupsCount]]': capturingGroupsCount
|
|---|
| 76 | }; // step 14
|
|---|
| 77 | };
|
|---|