1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var regexTester = require('safe-regex-test');
|
---|
6 | var every = require('../helpers/every');
|
---|
7 |
|
---|
8 | var inspect = require('object-inspect');
|
---|
9 |
|
---|
10 | var Get = require('./Get');
|
---|
11 | var IsArray = require('./IsArray');
|
---|
12 | var min = require('./min');
|
---|
13 | var StringIndexOf = require('./StringIndexOf');
|
---|
14 | var StringToNumber = require('./StringToNumber');
|
---|
15 | var substring = require('./substring');
|
---|
16 | var ToString = require('./ToString');
|
---|
17 | var Type = require('./Type');
|
---|
18 |
|
---|
19 | var isInteger = require('../helpers/isInteger');
|
---|
20 | var isStringOrUndefined = require('../helpers/isStringOrUndefined');
|
---|
21 | var isPrefixOf = require('../helpers/isPrefixOf');
|
---|
22 |
|
---|
23 | var startsWithDollarDigit = regexTester(/^\$[0-9]/);
|
---|
24 |
|
---|
25 | // http://www.ecma-international.org/ecma-262/13.0/#sec-getsubstitution
|
---|
26 |
|
---|
27 | // eslint-disable-next-line max-statements, max-params, max-lines-per-function
|
---|
28 | module.exports = function GetSubstitution(matched, str, position, captures, namedCaptures, replacementTemplate) {
|
---|
29 | if (typeof matched !== 'string') {
|
---|
30 | throw new $TypeError('Assertion failed: `matched` must be a String');
|
---|
31 | }
|
---|
32 |
|
---|
33 | if (typeof str !== 'string') {
|
---|
34 | throw new $TypeError('Assertion failed: `str` must be a String');
|
---|
35 | }
|
---|
36 |
|
---|
37 | if (!isInteger(position) || position < 0) {
|
---|
38 | throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, got ' + inspect(position));
|
---|
39 | }
|
---|
40 |
|
---|
41 | if (!IsArray(captures) || !every(captures, isStringOrUndefined)) {
|
---|
42 | throw new $TypeError('Assertion failed: `captures` must be a possibly-empty List of Strings or `undefined`, got ' + inspect(captures));
|
---|
43 | }
|
---|
44 |
|
---|
45 | if (typeof namedCaptures !== 'undefined' && Type(namedCaptures) !== 'Object') {
|
---|
46 | throw new $TypeError('Assertion failed: `namedCaptures` must be `undefined` or an Object');
|
---|
47 | }
|
---|
48 |
|
---|
49 | if (typeof replacementTemplate !== 'string') {
|
---|
50 | throw new $TypeError('Assertion failed: `replacementTemplate` must be a String');
|
---|
51 | }
|
---|
52 |
|
---|
53 | var stringLength = str.length; // step 1
|
---|
54 |
|
---|
55 | if (position > stringLength) {
|
---|
56 | throw new $TypeError('Assertion failed: position > stringLength, got ' + inspect(position)); // step 2
|
---|
57 | }
|
---|
58 |
|
---|
59 | var templateRemainder = replacementTemplate; // step 3
|
---|
60 |
|
---|
61 | var result = ''; // step 4
|
---|
62 |
|
---|
63 | while (templateRemainder !== '') { // step 5
|
---|
64 | // 5.a NOTE: The following steps isolate ref (a prefix of templateRemainder), determine refReplacement (its replacement), and then append that replacement to result.
|
---|
65 |
|
---|
66 | var ref, refReplacement, found, capture;
|
---|
67 | if (isPrefixOf('$$', templateRemainder)) { // step 5.b
|
---|
68 | ref = '$$'; // step 5.b.i
|
---|
69 | refReplacement = '$'; // step 5.b.ii
|
---|
70 | } else if (isPrefixOf('$`', templateRemainder)) { // step 5.c
|
---|
71 | ref = '$`'; // step 5.c.i
|
---|
72 | refReplacement = substring(str, 0, position); // step 5.c.ii
|
---|
73 | } else if (isPrefixOf('$&', templateRemainder)) { // step 5.d
|
---|
74 | ref = '$&'; // step 5.d.i
|
---|
75 | refReplacement = matched; // step 5.d.ii
|
---|
76 | } else if (isPrefixOf('$\'', templateRemainder)) { // step 5.e
|
---|
77 | ref = '$\''; // step 5.e.i
|
---|
78 | var matchLength = matched.length; // step 5.e.ii
|
---|
79 | var tailPos = position + matchLength; // step 5.e.iii
|
---|
80 | refReplacement = substring(str, min(tailPos, stringLength)); // step 5.e.iv
|
---|
81 | // 5.e.v NOTE: tailPos can exceed stringLength only if this abstract operation was invoked by a call to the intrinsic @@replace method of %RegExp.prototype% on an object whose "exec" property is not the intrinsic %RegExp.prototype.exec%.
|
---|
82 | } else if (startsWithDollarDigit(templateRemainder)) { // step 5.f
|
---|
83 | found = false; // step 5.f.i
|
---|
84 | for (var d = 2; d > 0; d -= 1) { // step 5.f.ii
|
---|
85 | // If found is false and templateRemainder starts with "$" followed by d or more decimal digits, then
|
---|
86 | if (!found) { // step 5.f.ii.1
|
---|
87 | found = true; // step 5.f.ii.1.a
|
---|
88 | ref = substring(templateRemainder, 0, 1 + d); // step 5.f.ii.1.b
|
---|
89 | var digits = substring(templateRemainder, 1, 1 + d); // step 5.f.ii.1.c
|
---|
90 | var index = StringToNumber(digits); // step 5.f.ii.1.d
|
---|
91 | if (index < 0 || index > 99) {
|
---|
92 | throw new $TypeError('Assertion failed: `index` must be >= 0 and <= 99'); // step 5.f.ii.1.e
|
---|
93 | }
|
---|
94 | if (index === 0) { // step 5.f.ii.1.f
|
---|
95 | refReplacement = ref;
|
---|
96 | } else if (index <= captures.length) { // step 5.f.ii.1.g
|
---|
97 | capture = captures[index - 1]; // step 5.f.ii.1.g.i
|
---|
98 | if (typeof capture === 'undefined') { // step 5.f.ii.1.g.ii
|
---|
99 | refReplacement = ''; // step 5.f.ii.1.g.ii.i
|
---|
100 | } else { // step 5.f.ii.1.g.iii
|
---|
101 | refReplacement = capture; // step 5.f.ii.1.g.iii.i
|
---|
102 | }
|
---|
103 | } else { // step 5.f.ii.1.h
|
---|
104 | refReplacement = ref; // step 5.f.ii.1.h.i
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | } else if (isPrefixOf('$<', templateRemainder)) { // step 5.g
|
---|
109 | var gtPos = StringIndexOf(templateRemainder, '>', 0); // step 5.g.i
|
---|
110 | if (gtPos === -1 || typeof namedCaptures === 'undefined') { // step 5.g.ii
|
---|
111 | ref = '$<'; // step 5.g.ii.1
|
---|
112 | refReplacement = ref; // step 5.g.ii.2
|
---|
113 | } else { // step 5.g.iii
|
---|
114 | ref = substring(templateRemainder, 0, gtPos + 1); // step 5.g.iii.1
|
---|
115 | var groupName = substring(templateRemainder, 2, gtPos); // step 5.g.iii.2
|
---|
116 | if (Type(namedCaptures) !== 'Object') {
|
---|
117 | throw new $TypeError('Assertion failed: Type(namedCaptures) is not Object'); // step 5.g.iii.3
|
---|
118 | }
|
---|
119 | capture = Get(namedCaptures, groupName); // step 5.g.iii.4
|
---|
120 | if (typeof capture === 'undefined') { // step 5.g.iii.5
|
---|
121 | refReplacement = ''; // step 5.g.iii.5.a
|
---|
122 | } else { // step 5.g.iii.6
|
---|
123 | refReplacement = ToString(capture); // step 5.g.iii.6.a
|
---|
124 | }
|
---|
125 | }
|
---|
126 | } else { // step 5.h
|
---|
127 | ref = substring(templateRemainder, 0, 1); // step 5.h.i
|
---|
128 | refReplacement = ref; // step 5.h.ii
|
---|
129 | }
|
---|
130 |
|
---|
131 | var refLength = ref.length; // step 5.i
|
---|
132 |
|
---|
133 | templateRemainder = substring(templateRemainder, refLength); // step 5.j
|
---|
134 |
|
---|
135 | result += refReplacement; // step 5.k
|
---|
136 | }
|
---|
137 |
|
---|
138 | return result; // step 6
|
---|
139 | };
|
---|