source: imaps-frontend/node_modules/es-abstract/2022/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.6 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var regexTester = require('safe-regex-test');
6var inspect = require('object-inspect');
7var isInteger = require('math-intrinsics/isInteger');
8
9var Get = require('./Get');
10var IsArray = require('./IsArray');
11var min = require('./min');
12var StringIndexOf = require('./StringIndexOf');
13var StringToNumber = require('./StringToNumber');
14var substring = require('./substring');
15var ToString = require('./ToString');
16
17var every = require('../helpers/every');
18var isObject = require('../helpers/isObject');
19var isPrefixOf = require('../helpers/isPrefixOf');
20var isStringOrUndefined = require('../helpers/isStringOrUndefined');
21
22var startsWithDollarDigit = regexTester(/^\$[0-9]/);
23
24// http://www.ecma-international.org/ecma-262/13.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, found, 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 found = false; // step 5.f.i
83 for (var d = 2; d > 0; d -= 1) { // step 5.f.ii
84 // If found is false and templateRemainder starts with "$" followed by d or more decimal digits, then
85 if (!found) { // step 5.f.ii.1
86 found = true; // step 5.f.ii.1.a
87 ref = substring(templateRemainder, 0, 1 + d); // step 5.f.ii.1.b
88 var digits = substring(templateRemainder, 1, 1 + d); // step 5.f.ii.1.c
89 var index = StringToNumber(digits); // step 5.f.ii.1.d
90 if (index < 0 || index > 99) {
91 throw new $TypeError('Assertion failed: `index` must be >= 0 and <= 99'); // step 5.f.ii.1.e
92 }
93 if (index === 0) { // step 5.f.ii.1.f
94 refReplacement = ref;
95 } else if (index <= captures.length) { // step 5.f.ii.1.g
96 capture = captures[index - 1]; // step 5.f.ii.1.g.i
97 if (typeof capture === 'undefined') { // step 5.f.ii.1.g.ii
98 refReplacement = ''; // step 5.f.ii.1.g.ii.i
99 } else { // step 5.f.ii.1.g.iii
100 refReplacement = capture; // step 5.f.ii.1.g.iii.i
101 }
102 } else { // step 5.f.ii.1.h
103 refReplacement = ref; // step 5.f.ii.1.h.i
104 }
105 }
106 }
107 } else if (isPrefixOf('$<', templateRemainder)) { // step 5.g
108 var gtPos = StringIndexOf(templateRemainder, '>', 0); // step 5.g.i
109 if (gtPos === -1 || typeof namedCaptures === 'undefined') { // step 5.g.ii
110 ref = '$<'; // step 5.g.ii.1
111 refReplacement = ref; // step 5.g.ii.2
112 } else { // step 5.g.iii
113 ref = substring(templateRemainder, 0, gtPos + 1); // step 5.g.iii.1
114 var groupName = substring(templateRemainder, 2, gtPos); // step 5.g.iii.2
115 if (!isObject(namedCaptures)) {
116 throw new $TypeError('Assertion failed: Type(namedCaptures) is not Object'); // step 5.g.iii.3
117 }
118 capture = Get(namedCaptures, groupName); // step 5.g.iii.4
119 if (typeof capture === 'undefined') { // step 5.g.iii.5
120 refReplacement = ''; // step 5.g.iii.5.a
121 } else { // step 5.g.iii.6
122 refReplacement = ToString(capture); // step 5.g.iii.6.a
123 }
124 }
125 } else { // step 5.h
126 ref = substring(templateRemainder, 0, 1); // step 5.h.i
127 refReplacement = ref; // step 5.h.ii
128 }
129
130 var refLength = ref.length; // step 5.i
131
132 templateRemainder = substring(templateRemainder, refLength); // step 5.j
133
134 result += refReplacement; // step 5.k
135 }
136
137 return result; // step 6
138};
Note: See TracBrowser for help on using the repository browser.