blob: 5bdce38b606a7808c159376410c1ba5103ed4776 [file] [log] [blame]
Tim van der Lippefdbd42e2020-04-07 14:14:361
2'use strict';
3
Tim van der Lippe2c891972021-07-29 15:22:504var GetIntrinsic = require('get-intrinsic');
Tim van der Lippefdbd42e2020-04-07 14:14:365
6var $TypeError = GetIntrinsic('%TypeError%');
7var $parseInt = GetIntrinsic('%parseInt%');
8
9var inspect = require('object-inspect');
10
11var regexTester = require('../helpers/regexTester');
Tim van der Lippe2c891972021-07-29 15:22:5012var callBound = require('call-bind/callBound');
Tim van der Lippefdbd42e2020-04-07 14:14:3613var every = require('../helpers/every');
14
15var isDigit = regexTester(/^[0-9]$/);
16
17var $charAt = callBound('String.prototype.charAt');
18var $strSlice = callBound('String.prototype.slice');
19
20var IsArray = require('./IsArray');
21var IsInteger = require('./IsInteger');
22var Type = require('./Type');
23
24var canDistinguishSparseFromUndefined = 0 in [undefined]; // IE 6 - 8 have a bug where this returns false
25
26var isStringOrHole = function (capture, index, arr) {
27 return Type(capture) === 'String' || (canDistinguishSparseFromUndefined ? !(index in arr) : Type(capture) === 'Undefined');
28};
29
Tim van der Lippe2c891972021-07-29 15:22:5030// https://ecma-international.org/ecma-262/6.0/#sec-getsubstitution
Tim van der Lippefdbd42e2020-04-07 14:14:3631
32// eslint-disable-next-line max-statements, max-params, max-lines-per-function
33module.exports = function GetSubstitution(matched, str, position, captures, replacement) {
34 if (Type(matched) !== 'String') {
35 throw new $TypeError('Assertion failed: `matched` must be a String');
36 }
37 var matchLength = matched.length;
38
39 if (Type(str) !== 'String') {
40 throw new $TypeError('Assertion failed: `str` must be a String');
41 }
42 var stringLength = str.length;
43
44 if (!IsInteger(position) || position < 0 || position > stringLength) {
45 throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, and less than or equal to the length of `string`, got ' + inspect(position));
46 }
47
48 if (!IsArray(captures) || !every(captures, isStringOrHole)) {
49 throw new $TypeError('Assertion failed: `captures` must be a List of Strings, got ' + inspect(captures));
50 }
51
52 if (Type(replacement) !== 'String') {
53 throw new $TypeError('Assertion failed: `replacement` must be a String');
54 }
55
56 var tailPos = position + matchLength;
57 var m = captures.length;
58
59 var result = '';
60 for (var i = 0; i < replacement.length; i += 1) {
61 // if this is a $, and it's not the end of the replacement
62 var current = $charAt(replacement, i);
63 var isLast = (i + 1) >= replacement.length;
64 var nextIsLast = (i + 2) >= replacement.length;
65 if (current === '$' && !isLast) {
66 var next = $charAt(replacement, i + 1);
67 if (next === '$') {
68 result += '$';
69 i += 1;
70 } else if (next === '&') {
71 result += matched;
72 i += 1;
73 } else if (next === '`') {
74 result += position === 0 ? '' : $strSlice(str, 0, position - 1);
75 i += 1;
76 } else if (next === "'") {
77 result += tailPos >= stringLength ? '' : $strSlice(str, tailPos);
78 i += 1;
79 } else {
80 var nextNext = nextIsLast ? null : $charAt(replacement, i + 2);
81 if (isDigit(next) && next !== '0' && (nextIsLast || !isDigit(nextNext))) {
82 // $1 through $9, and not followed by a digit
83 var n = $parseInt(next, 10);
84 // if (n > m, impl-defined)
Tim van der Lippe2c891972021-07-29 15:22:5085 result += n <= m && Type(captures[n - 1]) === 'Undefined' ? '' : captures[n - 1];
Tim van der Lippefdbd42e2020-04-07 14:14:3686 i += 1;
87 } else if (isDigit(next) && (nextIsLast || isDigit(nextNext))) {
88 // $00 through $99
89 var nn = next + nextNext;
90 var nnI = $parseInt(nn, 10) - 1;
91 // if nn === '00' or nn > m, impl-defined
Tim van der Lippe2c891972021-07-29 15:22:5092 result += nn <= m && Type(captures[nnI]) === 'Undefined' ? '' : captures[nnI];
Tim van der Lippefdbd42e2020-04-07 14:14:3693 i += 2;
94 } else {
95 result += '$';
96 }
97 }
98 } else {
99 // the final $, or else not a $
100 result += $charAt(replacement, i);
101 }
102 }
103 return result;
104};