+var uncurryThis = require('../internals/function-uncurry-this');
var toObject = require('../internals/to-object');
var floor = Math.floor;
-var replace = ''.replace;
+var charAt = uncurryThis(''.charAt);
+var replace = uncurryThis(''.replace);
+var stringSlice = uncurryThis(''.slice);
var SUBSTITUTION_SYMBOLS = /\$([$&'`]|\d{1,2}|<[^>]*>)/g;
var SUBSTITUTION_SYMBOLS_NO_NAMED = /\$([$&'`]|\d{1,2})/g;
+// `GetSubstitution` abstract operation
// https://tc39.es/ecma262/#sec-getsubstitution
module.exports = function (matched, str, position, captures, namedCaptures, replacement) {
var tailPos = position + matched.length;
namedCaptures = toObject(namedCaptures);
symbols = SUBSTITUTION_SYMBOLS;
}
- return replace.call(replacement, symbols, function (match, ch) {
+ return replace(replacement, symbols, function (match, ch) {
var capture;
- switch (ch.charAt(0)) {
+ switch (charAt(ch, 0)) {
case '$': return '$';
case '&': return matched;
- case '`': return str.slice(0, position);
- case "'": return str.slice(tailPos);
+ case '`': return stringSlice(str, 0, position);
+ case "'": return stringSlice(str, tailPos);
case '<':
- capture = namedCaptures[ch.slice(1, -1)];
+ capture = namedCaptures[stringSlice(ch, 1, -1)];
break;
default: // \d\d?
var n = +ch;
if (n > m) {
var f = floor(n / 10);
if (f === 0) return match;
- if (f <= m) return captures[f - 1] === undefined ? ch.charAt(1) : captures[f - 1] + ch.charAt(1);
+ if (f <= m) return captures[f - 1] === undefined ? charAt(ch, 1) : captures[f - 1] + charAt(ch, 1);
return match;
}
capture = captures[n - 1];