GetSubstitution.js 3.2 KB

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