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