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

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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