blob: be2123e3b963ea8907bc291d23777482b67495a7 [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
Nikolay Vitkovd76576c2024-12-02 14:10:156var $TypeError = require('es-errors/type');
Tim van der Lippefdbd42e2020-04-07 14:14:367var $parseInt = GetIntrinsic('%parseInt%');
8
9var inspect = require('object-inspect');
10
Nikolay Vitkovd76576c2024-12-02 14:10:1511var regexTester = require('safe-regex-test');
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');
Tim van der Lippefdbd42e2020-04-07 14:14:3621
Nikolay Vitkovd76576c2024-12-02 14:10:1522var isInteger = require('../helpers/isInteger');
23var isStringOrUndefined = require('../helpers/isStringOrUndefined');
Tim van der Lippefdbd42e2020-04-07 14:14:3624
Nikolay Vitkovd76576c2024-12-02 14:10:1525// https://262.ecma-international.org/6.0/#sec-getsubstitution
Tim van der Lippefdbd42e2020-04-07 14:14:3626
Nikolay Vitkovd76576c2024-12-02 14:10:1527// eslint-disable-next-line max-statements, max-lines-per-function
Tim van der Lippefdbd42e2020-04-07 14:14:3628module.exports = function GetSubstitution(matched, str, position, captures, replacement) {
Nikolay Vitkovd76576c2024-12-02 14:10:1529 if (typeof matched !== 'string') {
Tim van der Lippefdbd42e2020-04-07 14:14:3630 throw new $TypeError('Assertion failed: `matched` must be a String');
31 }
32 var matchLength = matched.length;
33
Nikolay Vitkovd76576c2024-12-02 14:10:1534 if (typeof str !== 'string') {
Tim van der Lippefdbd42e2020-04-07 14:14:3635 throw new $TypeError('Assertion failed: `str` must be a String');
36 }
37 var stringLength = str.length;
38
Nikolay Vitkovd76576c2024-12-02 14:10:1539 if (!isInteger(position) || position < 0 || position > stringLength) {
Tim van der Lippefdbd42e2020-04-07 14:14:3640 throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, and less than or equal to the length of `string`, got ' + inspect(position));
41 }
42
Nikolay Vitkovd76576c2024-12-02 14:10:1543 if (!IsArray(captures) || !every(captures, isStringOrUndefined)) {
Tim van der Lippefdbd42e2020-04-07 14:14:3644 throw new $TypeError('Assertion failed: `captures` must be a List of Strings, got ' + inspect(captures));
45 }
46
Nikolay Vitkovd76576c2024-12-02 14:10:1547 if (typeof replacement !== 'string') {
Tim van der Lippefdbd42e2020-04-07 14:14:3648 throw new $TypeError('Assertion failed: `replacement` must be a String');
49 }
50
51 var tailPos = position + matchLength;
52 var m = captures.length;
53
54 var result = '';
55 for (var i = 0; i < replacement.length; i += 1) {
56 // if this is a $, and it's not the end of the replacement
57 var current = $charAt(replacement, i);
58 var isLast = (i + 1) >= replacement.length;
59 var nextIsLast = (i + 2) >= replacement.length;
60 if (current === '$' && !isLast) {
61 var next = $charAt(replacement, i + 1);
62 if (next === '$') {
63 result += '$';
64 i += 1;
65 } else if (next === '&') {
66 result += matched;
67 i += 1;
68 } else if (next === '`') {
69 result += position === 0 ? '' : $strSlice(str, 0, position - 1);
70 i += 1;
71 } else if (next === "'") {
72 result += tailPos >= stringLength ? '' : $strSlice(str, tailPos);
73 i += 1;
74 } else {
75 var nextNext = nextIsLast ? null : $charAt(replacement, i + 2);
76 if (isDigit(next) && next !== '0' && (nextIsLast || !isDigit(nextNext))) {
77 // $1 through $9, and not followed by a digit
78 var n = $parseInt(next, 10);
79 // if (n > m, impl-defined)
Nikolay Vitkovd76576c2024-12-02 14:10:1580 result += n <= m && typeof captures[n - 1] === 'undefined' ? '' : captures[n - 1];
Tim van der Lippefdbd42e2020-04-07 14:14:3681 i += 1;
82 } else if (isDigit(next) && (nextIsLast || isDigit(nextNext))) {
83 // $00 through $99
84 var nn = next + nextNext;
85 var nnI = $parseInt(nn, 10) - 1;
86 // if nn === '00' or nn > m, impl-defined
Nikolay Vitkovd76576c2024-12-02 14:10:1587 result += nn <= m && typeof captures[nnI] === 'undefined' ? '' : captures[nnI];
Tim van der Lippefdbd42e2020-04-07 14:14:3688 i += 2;
89 } else {
90 result += '$';
91 }
92 }
93 } else {
94 // the final $, or else not a $
95 result += $charAt(replacement, i);
96 }
97 }
98 return result;
99};