GetSubstitution.js 5.6 KB

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