1 |
|
---|
2 | 'use strict';
|
---|
3 |
|
---|
4 | var GetIntrinsic = require('get-intrinsic');
|
---|
5 |
|
---|
6 | var $TypeError = require('es-errors/type');
|
---|
7 | var $parseInt = GetIntrinsic('%parseInt%');
|
---|
8 |
|
---|
9 | var inspect = require('object-inspect');
|
---|
10 |
|
---|
11 | var regexTester = require('safe-regex-test');
|
---|
12 | var callBound = require('call-bind/callBound');
|
---|
13 | var every = require('../helpers/every');
|
---|
14 |
|
---|
15 | var isDigit = regexTester(/^[0-9]$/);
|
---|
16 |
|
---|
17 | var $charAt = callBound('String.prototype.charAt');
|
---|
18 | var $strSlice = callBound('String.prototype.slice');
|
---|
19 |
|
---|
20 | var IsArray = require('./IsArray');
|
---|
21 |
|
---|
22 | var isInteger = require('../helpers/isInteger');
|
---|
23 | var isStringOrUndefined = require('../helpers/isStringOrUndefined');
|
---|
24 |
|
---|
25 | // https://262.ecma-international.org/6.0/#sec-getsubstitution
|
---|
26 |
|
---|
27 | // eslint-disable-next-line max-statements, max-lines-per-function
|
---|
28 | module.exports = function GetSubstitution(matched, str, position, captures, replacement) {
|
---|
29 | if (typeof matched !== 'string') {
|
---|
30 | throw new $TypeError('Assertion failed: `matched` must be a String');
|
---|
31 | }
|
---|
32 | var matchLength = matched.length;
|
---|
33 |
|
---|
34 | if (typeof str !== 'string') {
|
---|
35 | throw new $TypeError('Assertion failed: `str` must be a String');
|
---|
36 | }
|
---|
37 | var stringLength = str.length;
|
---|
38 |
|
---|
39 | if (!isInteger(position) || position < 0 || position > stringLength) {
|
---|
40 | 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 |
|
---|
43 | if (!IsArray(captures) || !every(captures, isStringOrUndefined)) {
|
---|
44 | throw new $TypeError('Assertion failed: `captures` must be a List of Strings, got ' + inspect(captures));
|
---|
45 | }
|
---|
46 |
|
---|
47 | if (typeof replacement !== 'string') {
|
---|
48 | 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)
|
---|
80 | result += n <= m && typeof captures[n - 1] === 'undefined' ? '' : captures[n - 1];
|
---|
81 | 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
|
---|
87 | result += nn <= m && typeof captures[nnI] === 'undefined' ? '' : captures[nnI];
|
---|
88 | 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 | };
|
---|