1 | 'use strict';
|
---|
2 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
3 | var toObject = require('../internals/to-object');
|
---|
4 |
|
---|
5 | var floor = Math.floor;
|
---|
6 | var charAt = uncurryThis(''.charAt);
|
---|
7 | var replace = uncurryThis(''.replace);
|
---|
8 | var stringSlice = uncurryThis(''.slice);
|
---|
9 | // eslint-disable-next-line redos/no-vulnerable -- safe
|
---|
10 | var SUBSTITUTION_SYMBOLS = /\$([$&'`]|\d{1,2}|<[^>]*>)/g;
|
---|
11 | var SUBSTITUTION_SYMBOLS_NO_NAMED = /\$([$&'`]|\d{1,2})/g;
|
---|
12 |
|
---|
13 | // `GetSubstitution` abstract operation
|
---|
14 | // https://tc39.es/ecma262/#sec-getsubstitution
|
---|
15 | module.exports = function (matched, str, position, captures, namedCaptures, replacement) {
|
---|
16 | var tailPos = position + matched.length;
|
---|
17 | var m = captures.length;
|
---|
18 | var symbols = SUBSTITUTION_SYMBOLS_NO_NAMED;
|
---|
19 | if (namedCaptures !== undefined) {
|
---|
20 | namedCaptures = toObject(namedCaptures);
|
---|
21 | symbols = SUBSTITUTION_SYMBOLS;
|
---|
22 | }
|
---|
23 | return replace(replacement, symbols, function (match, ch) {
|
---|
24 | var capture;
|
---|
25 | switch (charAt(ch, 0)) {
|
---|
26 | case '$': return '$';
|
---|
27 | case '&': return matched;
|
---|
28 | case '`': return stringSlice(str, 0, position);
|
---|
29 | case "'": return stringSlice(str, tailPos);
|
---|
30 | case '<':
|
---|
31 | capture = namedCaptures[stringSlice(ch, 1, -1)];
|
---|
32 | break;
|
---|
33 | default: // \d\d?
|
---|
34 | var n = +ch;
|
---|
35 | if (n === 0) return match;
|
---|
36 | if (n > m) {
|
---|
37 | var f = floor(n / 10);
|
---|
38 | if (f === 0) return match;
|
---|
39 | if (f <= m) return captures[f - 1] === undefined ? charAt(ch, 1) : captures[f - 1] + charAt(ch, 1);
|
---|
40 | return match;
|
---|
41 | }
|
---|
42 | capture = captures[n - 1];
|
---|
43 | }
|
---|
44 | return capture === undefined ? '' : capture;
|
---|
45 | });
|
---|
46 | };
|
---|