GetSubstitution.js 3.2 KB

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