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