Tim van der Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 1 | |
| 2 | 'use strict'; |
| 3 | |
Tim van der Lippe | 2c89197 | 2021-07-29 15:22:50 | [diff] [blame^] | 4 | var GetIntrinsic = require('get-intrinsic'); |
Tim van der Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 5 | |
| 6 | var $TypeError = GetIntrinsic('%TypeError%'); |
| 7 | var $parseInt = GetIntrinsic('%parseInt%'); |
| 8 | |
| 9 | var inspect = require('object-inspect'); |
| 10 | |
| 11 | var regexTester = require('../helpers/regexTester'); |
Tim van der Lippe | 2c89197 | 2021-07-29 15:22:50 | [diff] [blame^] | 12 | var callBound = require('call-bind/callBound'); |
Tim van der Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 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 | var IsInteger = require('./IsInteger'); |
| 22 | var Type = require('./Type'); |
| 23 | |
| 24 | var canDistinguishSparseFromUndefined = 0 in [undefined]; // IE 6 - 8 have a bug where this returns false |
| 25 | |
| 26 | var isStringOrHole = function (capture, index, arr) { |
| 27 | return Type(capture) === 'String' || (canDistinguishSparseFromUndefined ? !(index in arr) : Type(capture) === 'Undefined'); |
| 28 | }; |
| 29 | |
Tim van der Lippe | 2c89197 | 2021-07-29 15:22:50 | [diff] [blame^] | 30 | // https://ecma-international.org/ecma-262/6.0/#sec-getsubstitution |
Tim van der Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 31 | |
| 32 | // eslint-disable-next-line max-statements, max-params, max-lines-per-function |
| 33 | module.exports = function GetSubstitution(matched, str, position, captures, replacement) { |
| 34 | if (Type(matched) !== 'String') { |
| 35 | throw new $TypeError('Assertion failed: `matched` must be a String'); |
| 36 | } |
| 37 | var matchLength = matched.length; |
| 38 | |
| 39 | if (Type(str) !== 'String') { |
| 40 | throw new $TypeError('Assertion failed: `str` must be a String'); |
| 41 | } |
| 42 | var stringLength = str.length; |
| 43 | |
| 44 | if (!IsInteger(position) || position < 0 || position > stringLength) { |
| 45 | throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, and less than or equal to the length of `string`, got ' + inspect(position)); |
| 46 | } |
| 47 | |
| 48 | if (!IsArray(captures) || !every(captures, isStringOrHole)) { |
| 49 | throw new $TypeError('Assertion failed: `captures` must be a List of Strings, got ' + inspect(captures)); |
| 50 | } |
| 51 | |
| 52 | if (Type(replacement) !== 'String') { |
| 53 | throw new $TypeError('Assertion failed: `replacement` must be a String'); |
| 54 | } |
| 55 | |
| 56 | var tailPos = position + matchLength; |
| 57 | var m = captures.length; |
| 58 | |
| 59 | var result = ''; |
| 60 | for (var i = 0; i < replacement.length; i += 1) { |
| 61 | // if this is a $, and it's not the end of the replacement |
| 62 | var current = $charAt(replacement, i); |
| 63 | var isLast = (i + 1) >= replacement.length; |
| 64 | var nextIsLast = (i + 2) >= replacement.length; |
| 65 | if (current === '$' && !isLast) { |
| 66 | var next = $charAt(replacement, i + 1); |
| 67 | if (next === '$') { |
| 68 | result += '$'; |
| 69 | i += 1; |
| 70 | } else if (next === '&') { |
| 71 | result += matched; |
| 72 | i += 1; |
| 73 | } else if (next === '`') { |
| 74 | result += position === 0 ? '' : $strSlice(str, 0, position - 1); |
| 75 | i += 1; |
| 76 | } else if (next === "'") { |
| 77 | result += tailPos >= stringLength ? '' : $strSlice(str, tailPos); |
| 78 | i += 1; |
| 79 | } else { |
| 80 | var nextNext = nextIsLast ? null : $charAt(replacement, i + 2); |
| 81 | if (isDigit(next) && next !== '0' && (nextIsLast || !isDigit(nextNext))) { |
| 82 | // $1 through $9, and not followed by a digit |
| 83 | var n = $parseInt(next, 10); |
| 84 | // if (n > m, impl-defined) |
Tim van der Lippe | 2c89197 | 2021-07-29 15:22:50 | [diff] [blame^] | 85 | result += n <= m && Type(captures[n - 1]) === 'Undefined' ? '' : captures[n - 1]; |
Tim van der Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 86 | i += 1; |
| 87 | } else if (isDigit(next) && (nextIsLast || isDigit(nextNext))) { |
| 88 | // $00 through $99 |
| 89 | var nn = next + nextNext; |
| 90 | var nnI = $parseInt(nn, 10) - 1; |
| 91 | // if nn === '00' or nn > m, impl-defined |
Tim van der Lippe | 2c89197 | 2021-07-29 15:22:50 | [diff] [blame^] | 92 | result += nn <= m && Type(captures[nnI]) === 'Undefined' ? '' : captures[nnI]; |
Tim van der Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 93 | i += 2; |
| 94 | } else { |
| 95 | result += '$'; |
| 96 | } |
| 97 | } |
| 98 | } else { |
| 99 | // the final $, or else not a $ |
| 100 | result += $charAt(replacement, i); |
| 101 | } |
| 102 | } |
| 103 | return result; |
| 104 | }; |