GetSubstitution.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. var startsWithDollarTwoDigit = regexTester(/^\$[0-9][0-9]/);
  19. // http://www.ecma-international.org/ecma-262/15.0/#sec-getsubstitution
  20. // eslint-disable-next-line max-statements, max-params, max-lines-per-function
  21. module.exports = function GetSubstitution(matched, str, position, captures, namedCaptures, replacementTemplate) {
  22. if (typeof matched !== 'string') {
  23. throw new $TypeError('Assertion failed: `matched` must be a String');
  24. }
  25. if (typeof str !== 'string') {
  26. throw new $TypeError('Assertion failed: `str` must be a String');
  27. }
  28. if (!isInteger(position) || position < 0) {
  29. throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, got ' + inspect(position));
  30. }
  31. if (!IsArray(captures) || !every(captures, isStringOrUndefined)) {
  32. throw new $TypeError('Assertion failed: `captures` must be a possibly-empty List of Strings or `undefined`, got ' + inspect(captures));
  33. }
  34. if (typeof namedCaptures !== 'undefined' && Type(namedCaptures) !== 'Object') {
  35. throw new $TypeError('Assertion failed: `namedCaptures` must be `undefined` or an Object');
  36. }
  37. if (typeof replacementTemplate !== 'string') {
  38. throw new $TypeError('Assertion failed: `replacementTemplate` must be a String');
  39. }
  40. var stringLength = str.length; // step 1
  41. if (position > stringLength) {
  42. throw new $TypeError('Assertion failed: position > stringLength, got ' + inspect(position)); // step 2
  43. }
  44. var templateRemainder = replacementTemplate; // step 3
  45. var result = ''; // step 4
  46. while (templateRemainder !== '') { // step 5
  47. // 5.a NOTE: The following steps isolate ref (a prefix of templateRemainder), determine refReplacement (its replacement), and then append that replacement to result.
  48. var ref, refReplacement, capture;
  49. if (isPrefixOf('$$', templateRemainder)) { // step 5.b
  50. ref = '$$'; // step 5.b.i
  51. refReplacement = '$'; // step 5.b.ii
  52. } else if (isPrefixOf('$`', templateRemainder)) { // step 5.c
  53. ref = '$`'; // step 5.c.i
  54. refReplacement = substring(str, 0, position); // step 5.c.ii
  55. } else if (isPrefixOf('$&', templateRemainder)) { // step 5.d
  56. ref = '$&'; // step 5.d.i
  57. refReplacement = matched; // step 5.d.ii
  58. } else if (isPrefixOf('$\'', templateRemainder)) { // step 5.e
  59. ref = '$\''; // step 5.e.i
  60. var matchLength = matched.length; // step 5.e.ii
  61. var tailPos = position + matchLength; // step 5.e.iii
  62. refReplacement = substring(str, min(tailPos, stringLength)); // step 5.e.iv
  63. // 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%.
  64. } else if (startsWithDollarDigit(templateRemainder)) { // step 5.f
  65. var digitCount = startsWithDollarTwoDigit(templateRemainder) ? 2 : 1; // step 5.f.i
  66. var digits = substring(templateRemainder, 1, 1 + digitCount); // step 5.f.ii
  67. var index = StringToNumber(digits); // step 5.f.iii
  68. if (index < 0 || index > 99) {
  69. throw new $TypeError('Assertion failed: `index` must be >= 0 and <= 99'); // step 5.f.iv
  70. }
  71. var captureLen = captures.length; // step 5.f.v
  72. if (index > captureLen && digitCount === 2) { // step 5.f.vi
  73. // 1. NOTE: When a two-digit replacement pattern specifies an index exceeding the count of capturing groups, it is treated as a one-digit replacement pattern followed by a literal digit.
  74. digitCount = 1; // step 5.f.vi.2
  75. digits = substring(digits, 0, 1); // step 5.f.vi.3
  76. index = StringToNumber(digits); // step 5.f.vi.4
  77. }
  78. ref = substring(templateRemainder, 0, 1 + digitCount); // step 5.f.vii
  79. if (1 <= index && index <= captureLen) { // step 5.f.viii
  80. capture = captures[index - 1]; // step 5.f.viii.1
  81. if (typeof capture === 'undefined') { // step 5.f.viii.2
  82. refReplacement = ''; // step 5.f.viii.2.a
  83. } else { // step 5.f.viii.3
  84. refReplacement = capture; // step 5.f.viii.3.a
  85. }
  86. } else { // step 5.f.ix
  87. refReplacement = ref; // step 5.f.ix.1
  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. };