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