GetSubstitution.js 3.4 KB

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