source: imaps-frontend/node_modules/es-abstract/2024/GetSubstitution.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 5.8 KB
RevLine 
[d565449]1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var regexTester = require('safe-regex-test');
6var every = require('../helpers/every');
7
8var inspect = require('object-inspect');
9
10var Get = require('./Get');
11var IsArray = require('./IsArray');
12var min = require('./min');
13var StringIndexOf = require('./StringIndexOf');
14var StringToNumber = require('./StringToNumber');
15var substring = require('./substring');
16var ToString = require('./ToString');
17var Type = require('./Type');
18
19var isInteger = require('../helpers/isInteger');
20var isStringOrUndefined = require('../helpers/isStringOrUndefined');
21var isPrefixOf = require('../helpers/isPrefixOf');
22
23var startsWithDollarDigit = regexTester(/^\$[0-9]/);
24var startsWithDollarTwoDigit = regexTester(/^\$[0-9][0-9]/);
25
26// http://www.ecma-international.org/ecma-262/15.0/#sec-getsubstitution
27
28// eslint-disable-next-line max-statements, max-params, max-lines-per-function
29module.exports = function GetSubstitution(matched, str, position, captures, namedCaptures, replacementTemplate) {
30 if (typeof matched !== 'string') {
31 throw new $TypeError('Assertion failed: `matched` must be a String');
32 }
33
34 if (typeof str !== 'string') {
35 throw new $TypeError('Assertion failed: `str` must be a String');
36 }
37
38 if (!isInteger(position) || position < 0) {
39 throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, got ' + inspect(position));
40 }
41
42 if (!IsArray(captures) || !every(captures, isStringOrUndefined)) {
43 throw new $TypeError('Assertion failed: `captures` must be a possibly-empty List of Strings or `undefined`, got ' + inspect(captures));
44 }
45
46 if (typeof namedCaptures !== 'undefined' && Type(namedCaptures) !== 'Object') {
47 throw new $TypeError('Assertion failed: `namedCaptures` must be `undefined` or an Object');
48 }
49
50 if (typeof replacementTemplate !== 'string') {
51 throw new $TypeError('Assertion failed: `replacementTemplate` must be a String');
52 }
53
54 var stringLength = str.length; // step 1
55
56 if (position > stringLength) {
57 throw new $TypeError('Assertion failed: position > stringLength, got ' + inspect(position)); // step 2
58 }
59
60 var templateRemainder = replacementTemplate; // step 3
61
62 var result = ''; // step 4
63
64 while (templateRemainder !== '') { // step 5
65 // 5.a NOTE: The following steps isolate ref (a prefix of templateRemainder), determine refReplacement (its replacement), and then append that replacement to result.
66
67 var ref, refReplacement, capture;
68 if (isPrefixOf('$$', templateRemainder)) { // step 5.b
69 ref = '$$'; // step 5.b.i
70 refReplacement = '$'; // step 5.b.ii
71 } else if (isPrefixOf('$`', templateRemainder)) { // step 5.c
72 ref = '$`'; // step 5.c.i
73 refReplacement = substring(str, 0, position); // step 5.c.ii
74 } else if (isPrefixOf('$&', templateRemainder)) { // step 5.d
75 ref = '$&'; // step 5.d.i
76 refReplacement = matched; // step 5.d.ii
77 } else if (isPrefixOf('$\'', templateRemainder)) { // step 5.e
78 ref = '$\''; // step 5.e.i
79 var matchLength = matched.length; // step 5.e.ii
80 var tailPos = position + matchLength; // step 5.e.iii
81 refReplacement = substring(str, min(tailPos, stringLength)); // step 5.e.iv
82 // 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%.
83 } else if (startsWithDollarDigit(templateRemainder)) { // step 5.f
84 var digitCount = startsWithDollarTwoDigit(templateRemainder) ? 2 : 1; // step 5.f.i
85
86 var digits = substring(templateRemainder, 1, 1 + digitCount); // step 5.f.ii
87
88 var index = StringToNumber(digits); // step 5.f.iii
89
90 if (index < 0 || index > 99) {
91 throw new $TypeError('Assertion failed: `index` must be >= 0 and <= 99'); // step 5.f.iv
92 }
93
94 var captureLen = captures.length; // step 5.f.v
95
96 if (index > captureLen && digitCount === 2) { // step 5.f.vi
97 // 1. NOTE: When a two-digit replacement pattern specifies an index exceeding the count of capturing groups, it is treated as a one-digit replacement pattern followed by a literal digit.
98
99 digitCount = 1; // step 5.f.vi.2
100
101 digits = substring(digits, 0, 1); // step 5.f.vi.3
102
103 index = StringToNumber(digits); // step 5.f.vi.4
104 }
105
106 ref = substring(templateRemainder, 0, 1 + digitCount); // step 5.f.vii
107
108 if (1 <= index && index <= captureLen) { // step 5.f.viii
109 capture = captures[index - 1]; // step 5.f.viii.1
110
111 if (typeof capture === 'undefined') { // step 5.f.viii.2
112 refReplacement = ''; // step 5.f.viii.2.a
113 } else { // step 5.f.viii.3
114 refReplacement = capture; // step 5.f.viii.3.a
115 }
116 } else { // step 5.f.ix
117 refReplacement = ref; // step 5.f.ix.1
118 }
119 } else if (isPrefixOf('$<', templateRemainder)) { // step 5.g
120 var gtPos = StringIndexOf(templateRemainder, '>', 0); // step 5.g.i
121 if (gtPos === -1 || typeof namedCaptures === 'undefined') { // step 5.g.ii
122 ref = '$<'; // step 5.g.ii.1
123 refReplacement = ref; // step 5.g.ii.2
124 } else { // step 5.g.iii
125 ref = substring(templateRemainder, 0, gtPos + 1); // step 5.g.iii.1
126 var groupName = substring(templateRemainder, 2, gtPos); // step 5.g.iii.2
127 if (Type(namedCaptures) !== 'Object') {
128 throw new $TypeError('Assertion failed: Type(namedCaptures) is not Object'); // step 5.g.iii.3
129 }
130 capture = Get(namedCaptures, groupName); // step 5.g.iii.4
131 if (typeof capture === 'undefined') { // step 5.g.iii.5
132 refReplacement = ''; // step 5.g.iii.5.a
133 } else { // step 5.g.iii.6
134 refReplacement = ToString(capture); // step 5.g.iii.6.a
135 }
136 }
137 } else { // step 5.h
138 ref = substring(templateRemainder, 0, 1); // step 5.h.i
139 refReplacement = ref; // step 5.h.ii
140 }
141
142 var refLength = ref.length; // step 5.i
143
144 templateRemainder = substring(templateRemainder, refLength); // step 5.j
145
146 result += refReplacement; // step 5.k
147 }
148
149 return result; // step 6
150};
Note: See TracBrowser for help on using the repository browser.