Skip to content
Permalink
Newer
Older
100644 124 lines (108 sloc) 4.12 KB
Ignoring revisions in .git-blame-ignore-revs.
1
'use strict';
2
3
var GetIntrinsic = require('get-intrinsic');
4
5
var $TypeError = GetIntrinsic('%TypeError%');
6
7
var callBound = require('call-bind/callBound');
8
var regexTester = require('safe-regex-test');
9
var every = require('../helpers/every');
10
11
var $charAt = callBound('String.prototype.charAt');
12
var $strSlice = callBound('String.prototype.slice');
13
var $indexOf = callBound('String.prototype.indexOf');
14
var $parseInt = parseInt;
15
16
var isDigit = regexTester(/^[0-9]$/);
17
18
var inspect = require('object-inspect');
19
20
var Get = require('./Get');
21
var IsArray = require('./IsArray');
22
var ToObject = require('./ToObject');
23
var ToString = require('./ToString');
24
var Type = require('./Type');
25
26
var isInteger = require('../helpers/isInteger');
27
var isStringOrHole = require('../helpers/isStringOrHole');
28
29
// http://www.ecma-international.org/ecma-262/12.0/#sec-getsubstitution
30
31
// eslint-disable-next-line max-statements, max-params, max-lines-per-function
32
module.exports = function GetSubstitution(matched, str, position, captures, namedCaptures, replacement) {
33
if (Type(matched) !== 'String') {
34
throw new $TypeError('Assertion failed: `matched` must be a String');
35
}
36
var matchLength = matched.length;
37
38
if (Type(str) !== 'String') {
39
throw new $TypeError('Assertion failed: `str` must be a String');
40
}
41
var stringLength = str.length;
42
43
if (!isInteger(position) || position < 0 || position > stringLength) {
44
throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, and less than or equal to the length of `string`, got ' + inspect(position));
45
}
46
47
if (!IsArray(captures) || !every(captures, isStringOrHole)) {
48
throw new $TypeError('Assertion failed: `captures` must be a possibly-empty List of Strings, got ' + inspect(captures));
49
}
50
51
if (Type(replacement) !== 'String') {
52
throw new $TypeError('Assertion failed: `replacement` must be a String');
53
}
54
55
var tailPos = position + matchLength;
56
var m = captures.length;
57
if (Type(namedCaptures) !== 'Undefined') {
58
namedCaptures = ToObject(namedCaptures); // eslint-disable-line no-param-reassign
59
}
60
61
var result = '';
62
for (var i = 0; i < replacement.length; i += 1) {
63
// if this is a $, and it's not the end of the replacement
64
var current = $charAt(replacement, i);
65
var isLast = (i + 1) >= replacement.length;
66
var nextIsLast = (i + 2) >= replacement.length;
67
if (current === '$' && !isLast) {
68
var next = $charAt(replacement, i + 1);
69
if (next === '$') {
70
result += '$';
71
i += 1;
72
} else if (next === '&') {
73
result += matched;
74
i += 1;
75
} else if (next === '`') {
76
result += position === 0 ? '' : $strSlice(str, 0, position - 1);
77
i += 1;
78
} else if (next === "'") {
79
result += tailPos >= stringLength ? '' : $strSlice(str, tailPos);
80
i += 1;
81
} else {
82
var nextNext = nextIsLast ? null : $charAt(replacement, i + 2);
83
if (isDigit(next) && next !== '0' && (nextIsLast || !isDigit(nextNext))) {
84
// $1 through $9, and not followed by a digit
85
var n = $parseInt(next, 10);
86
// if (n > m, impl-defined)
87
result += n <= m && Type(captures[n - 1]) === 'Undefined' ? '' : captures[n - 1];
88
i += 1;
89
} else if (isDigit(next) && (nextIsLast || isDigit(nextNext))) {
90
// $00 through $99
91
var nn = next + nextNext;
92
var nnI = $parseInt(nn, 10) - 1;
93
// if nn === '00' or nn > m, impl-defined
94
result += nn <= m && Type(captures[nnI]) === 'Undefined' ? '' : captures[nnI];
95
i += 2;
96
} else if (next === '<') {
97
// eslint-disable-next-line max-depth
98
if (Type(namedCaptures) === 'Undefined') {
99
result += '$<';
100
i += 2;
101
} else {
102
var endIndex = $indexOf(replacement, '>', i);
103
// eslint-disable-next-line max-depth
104
if (endIndex > -1) {
105
var groupName = $strSlice(replacement, i + '$<'.length, endIndex);
106
var capture = Get(namedCaptures, groupName);
107
// eslint-disable-next-line max-depth
108
if (Type(capture) !== 'Undefined') {
109
result += ToString(capture);
110
}
111
i += ('<' + groupName + '>').length;
112
}
113
}
114
} else {
115
result += '$';
116
}
117
}
118
} else {
119
// the final $, or else not a $
120
result += $charAt(replacement, i);
121
}
122
}
123
return result;
124
};