source: imaps-frontend/node_modules/es-abstract/2023/GetSubstitution.js

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

Update repo after prototype presentation

  • Property mode set to 100644
File size: 5.4 KB
Line 
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/14.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 ref = substring(templateRemainder, 0, 1 + digitCount); // step 5.f.ii
87
88 var digits = substring(templateRemainder, 1, 1 + digitCount); // step 5.f.iii
89
90 var index = StringToNumber(digits); // step 5.f.iv
91
92 if (index < 0 || index > 99) {
93 throw new $TypeError('Assertion failed: `index` must be >= 0 and <= 99'); // step 5.f.v
94 }
95
96 var captureLen = captures.length; // step 5.f.vi
97
98 if (1 <= index && index <= captureLen) { // step 5.f.vii
99 capture = captures[index - 1]; // step 5.f.vii.1
100
101 if (typeof capture === 'undefined') { // step 5.f.vii.2
102 refReplacement = ''; // step 5.f.vii.2.a
103 } else { // step 5.f.vii.3
104 refReplacement = capture; // step 5.f.vii.3.a
105 }
106 } else { // step 5.f.viii
107 refReplacement = ref; // step 5.f.viii.1
108 }
109 } else if (isPrefixOf('$<', templateRemainder)) { // step 5.g
110 var gtPos = StringIndexOf(templateRemainder, '>', 0); // step 5.g.i
111 if (gtPos === -1 || typeof namedCaptures === 'undefined') { // step 5.g.ii
112 ref = '$<'; // step 5.g.ii.1
113 refReplacement = ref; // step 5.g.ii.2
114 } else { // step 5.g.iii
115 ref = substring(templateRemainder, 0, gtPos + 1); // step 5.g.iii.1
116 var groupName = substring(templateRemainder, 2, gtPos); // step 5.g.iii.2
117 if (Type(namedCaptures) !== 'Object') {
118 throw new $TypeError('Assertion failed: Type(namedCaptures) is not Object'); // step 5.g.iii.3
119 }
120 capture = Get(namedCaptures, groupName); // step 5.g.iii.4
121 if (typeof capture === 'undefined') { // step 5.g.iii.5
122 refReplacement = ''; // step 5.g.iii.5.a
123 } else { // step 5.g.iii.6
124 refReplacement = ToString(capture); // step 5.g.iii.6.a
125 }
126 }
127 } else { // step 5.h
128 ref = substring(templateRemainder, 0, 1); // step 5.h.i
129 refReplacement = ref; // step 5.h.ii
130 }
131
132 var refLength = ref.length; // step 5.i
133
134 templateRemainder = substring(templateRemainder, refLength); // step 5.j
135
136 result += refReplacement; // step 5.k
137 }
138
139 return result; // step 6
140};
Note: See TracBrowser for help on using the repository browser.