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