| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var $TypeError = require('es-errors/type');
|
|---|
| 4 | var isObject = require('es-object-atoms/isObject');
|
|---|
| 5 | var regexTester = require('safe-regex-test');
|
|---|
| 6 | var inspect = require('object-inspect');
|
|---|
| 7 | var isInteger = require('math-intrinsics/isInteger');
|
|---|
| 8 |
|
|---|
| 9 | var Get = require('./Get');
|
|---|
| 10 | var IsArray = require('./IsArray');
|
|---|
| 11 | var min = require('./min');
|
|---|
| 12 | var StringIndexOf = require('./StringIndexOf');
|
|---|
| 13 | var StringToNumber = require('./StringToNumber');
|
|---|
| 14 | var substring = require('./substring');
|
|---|
| 15 | var ToString = require('./ToString');
|
|---|
| 16 |
|
|---|
| 17 | var every = require('../helpers/every');
|
|---|
| 18 | var isPrefixOf = require('../helpers/isPrefixOf');
|
|---|
| 19 | var isStringOrUndefined = require('../helpers/isStringOrUndefined');
|
|---|
| 20 |
|
|---|
| 21 | var startsWithDollarDigit = regexTester(/^\$[0-9]/);
|
|---|
| 22 |
|
|---|
| 23 | // http://www.ecma-international.org/ecma-262/13.0/#sec-getsubstitution
|
|---|
| 24 |
|
|---|
| 25 | // eslint-disable-next-line max-statements, max-params, max-lines-per-function
|
|---|
| 26 | module.exports = function GetSubstitution(matched, str, position, captures, namedCaptures, replacementTemplate) {
|
|---|
| 27 | if (typeof matched !== 'string') {
|
|---|
| 28 | throw new $TypeError('Assertion failed: `matched` must be a String');
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | if (typeof str !== 'string') {
|
|---|
| 32 | throw new $TypeError('Assertion failed: `str` must be a String');
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | if (!isInteger(position) || position < 0) {
|
|---|
| 36 | throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, got ' + inspect(position));
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | if (!IsArray(captures) || !every(captures, isStringOrUndefined)) {
|
|---|
| 40 | throw new $TypeError('Assertion failed: `captures` must be a possibly-empty List of Strings or `undefined`, got ' + inspect(captures));
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | if (typeof namedCaptures !== 'undefined' && !isObject(namedCaptures)) {
|
|---|
| 44 | throw new $TypeError('Assertion failed: `namedCaptures` must be `undefined` or an Object');
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | if (typeof replacementTemplate !== 'string') {
|
|---|
| 48 | throw new $TypeError('Assertion failed: `replacementTemplate` must be a String');
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | var stringLength = str.length; // step 1
|
|---|
| 52 |
|
|---|
| 53 | if (position > stringLength) {
|
|---|
| 54 | throw new $TypeError('Assertion failed: position > stringLength, got ' + inspect(position)); // step 2
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | var templateRemainder = replacementTemplate; // step 3
|
|---|
| 58 |
|
|---|
| 59 | var result = ''; // step 4
|
|---|
| 60 |
|
|---|
| 61 | while (templateRemainder !== '') { // step 5
|
|---|
| 62 | // 5.a NOTE: The following steps isolate ref (a prefix of templateRemainder), determine refReplacement (its replacement), and then append that replacement to result.
|
|---|
| 63 |
|
|---|
| 64 | var ref, refReplacement, found, capture;
|
|---|
| 65 | if (isPrefixOf('$$', templateRemainder)) { // step 5.b
|
|---|
| 66 | ref = '$$'; // step 5.b.i
|
|---|
| 67 | refReplacement = '$'; // step 5.b.ii
|
|---|
| 68 | } else if (isPrefixOf('$`', templateRemainder)) { // step 5.c
|
|---|
| 69 | ref = '$`'; // step 5.c.i
|
|---|
| 70 | refReplacement = substring(str, 0, position); // step 5.c.ii
|
|---|
| 71 | } else if (isPrefixOf('$&', templateRemainder)) { // step 5.d
|
|---|
| 72 | ref = '$&'; // step 5.d.i
|
|---|
| 73 | refReplacement = matched; // step 5.d.ii
|
|---|
| 74 | } else if (isPrefixOf('$\'', templateRemainder)) { // step 5.e
|
|---|
| 75 | ref = '$\''; // step 5.e.i
|
|---|
| 76 | var matchLength = matched.length; // step 5.e.ii
|
|---|
| 77 | var tailPos = position + matchLength; // step 5.e.iii
|
|---|
| 78 | refReplacement = substring(str, min(tailPos, stringLength)); // step 5.e.iv
|
|---|
| 79 | // 5.e.v NOTE: tailPos can exceed stringLength only if this abstract operation was invoked by a call to the intrinsic @@replace method of %RegExp.prototype% on an object whose "exec" property is not the intrinsic %RegExp.prototype.exec%.
|
|---|
| 80 | } else if (startsWithDollarDigit(templateRemainder)) { // step 5.f
|
|---|
| 81 | found = false; // step 5.f.i
|
|---|
| 82 | for (var d = 2; d > 0; d -= 1) { // step 5.f.ii
|
|---|
| 83 | // If found is false and templateRemainder starts with "$" followed by d or more decimal digits, then
|
|---|
| 84 | if (!found) { // step 5.f.ii.1
|
|---|
| 85 | found = true; // step 5.f.ii.1.a
|
|---|
| 86 | ref = substring(templateRemainder, 0, 1 + d); // step 5.f.ii.1.b
|
|---|
| 87 | var digits = substring(templateRemainder, 1, 1 + d); // step 5.f.ii.1.c
|
|---|
| 88 | var index = StringToNumber(digits); // step 5.f.ii.1.d
|
|---|
| 89 | if (index < 0 || index > 99) {
|
|---|
| 90 | throw new $TypeError('Assertion failed: `index` must be >= 0 and <= 99'); // step 5.f.ii.1.e
|
|---|
| 91 | }
|
|---|
| 92 | if (index === 0) { // step 5.f.ii.1.f
|
|---|
| 93 | refReplacement = ref;
|
|---|
| 94 | } else if (index <= captures.length) { // step 5.f.ii.1.g
|
|---|
| 95 | capture = captures[index - 1]; // step 5.f.ii.1.g.i
|
|---|
| 96 | if (typeof capture === 'undefined') { // step 5.f.ii.1.g.ii
|
|---|
| 97 | refReplacement = ''; // step 5.f.ii.1.g.ii.i
|
|---|
| 98 | } else { // step 5.f.ii.1.g.iii
|
|---|
| 99 | refReplacement = capture; // step 5.f.ii.1.g.iii.i
|
|---|
| 100 | }
|
|---|
| 101 | } else { // step 5.f.ii.1.h
|
|---|
| 102 | refReplacement = ref; // step 5.f.ii.1.h.i
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | } else if (isPrefixOf('$<', templateRemainder)) { // step 5.g
|
|---|
| 107 | var gtPos = StringIndexOf(templateRemainder, '>', 0); // step 5.g.i
|
|---|
| 108 | if (gtPos === -1 || typeof namedCaptures === 'undefined') { // step 5.g.ii
|
|---|
| 109 | ref = '$<'; // step 5.g.ii.1
|
|---|
| 110 | refReplacement = ref; // step 5.g.ii.2
|
|---|
| 111 | } else { // step 5.g.iii
|
|---|
| 112 | ref = substring(templateRemainder, 0, gtPos + 1); // step 5.g.iii.1
|
|---|
| 113 | var groupName = substring(templateRemainder, 2, gtPos); // step 5.g.iii.2
|
|---|
| 114 | if (!isObject(namedCaptures)) {
|
|---|
| 115 | throw new $TypeError('Assertion failed: Type(namedCaptures) is not Object'); // step 5.g.iii.3
|
|---|
| 116 | }
|
|---|
| 117 | capture = Get(namedCaptures, groupName); // step 5.g.iii.4
|
|---|
| 118 | if (typeof capture === 'undefined') { // step 5.g.iii.5
|
|---|
| 119 | refReplacement = ''; // step 5.g.iii.5.a
|
|---|
| 120 | } else { // step 5.g.iii.6
|
|---|
| 121 | refReplacement = ToString(capture); // step 5.g.iii.6.a
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | } else { // step 5.h
|
|---|
| 125 | ref = substring(templateRemainder, 0, 1); // step 5.h.i
|
|---|
| 126 | refReplacement = ref; // step 5.h.ii
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | var refLength = ref.length; // step 5.i
|
|---|
| 130 |
|
|---|
| 131 | templateRemainder = substring(templateRemainder, refLength); // step 5.j
|
|---|
| 132 |
|
|---|
| 133 | result += refReplacement; // step 5.k
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | return result; // step 6
|
|---|
| 137 | };
|
|---|