GetSubstitution.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var callBound = require('call-bind/callBound');
  5. var regexTester = require('safe-regex-test');
  6. var every = require('../helpers/every');
  7. var $charAt = callBound('String.prototype.charAt');
  8. var $strSlice = callBound('String.prototype.slice');
  9. var $indexOf = callBound('String.prototype.indexOf');
  10. var $parseInt = parseInt;
  11. var isDigit = regexTester(/^[0-9]$/);
  12. var inspect = require('object-inspect');
  13. var Get = require('./Get');
  14. var IsArray = require('./IsArray');
  15. var ToObject = require('./ToObject');
  16. var ToString = require('./ToString');
  17. var Type = require('./Type');
  18. var isInteger = require('../helpers/isInteger');
  19. var isStringOrHole = require('../helpers/isStringOrHole');
  20. // http://www.ecma-international.org/ecma-262/12.0/#sec-getsubstitution
  21. // eslint-disable-next-line max-statements, max-params, max-lines-per-function
  22. module.exports = function GetSubstitution(matched, str, position, captures, namedCaptures, replacement) {
  23. if (Type(matched) !== 'String') {
  24. throw new $TypeError('Assertion failed: `matched` must be a String');
  25. }
  26. var matchLength = matched.length;
  27. if (Type(str) !== 'String') {
  28. throw new $TypeError('Assertion failed: `str` must be a String');
  29. }
  30. var stringLength = str.length;
  31. if (!isInteger(position) || position < 0 || position > stringLength) {
  32. throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, and less than or equal to the length of `string`, got ' + inspect(position));
  33. }
  34. if (!IsArray(captures) || !every(captures, isStringOrHole)) {
  35. throw new $TypeError('Assertion failed: `captures` must be a possibly-empty List of Strings, got ' + inspect(captures));
  36. }
  37. if (Type(replacement) !== 'String') {
  38. throw new $TypeError('Assertion failed: `replacement` must be a String');
  39. }
  40. var tailPos = position + matchLength;
  41. var m = captures.length;
  42. if (Type(namedCaptures) !== 'Undefined') {
  43. namedCaptures = ToObject(namedCaptures); // eslint-disable-line no-param-reassign
  44. }
  45. var result = '';
  46. for (var i = 0; i < replacement.length; i += 1) {
  47. // if this is a $, and it's not the end of the replacement
  48. var current = $charAt(replacement, i);
  49. var isLast = (i + 1) >= replacement.length;
  50. var nextIsLast = (i + 2) >= replacement.length;
  51. if (current === '$' && !isLast) {
  52. var next = $charAt(replacement, i + 1);
  53. if (next === '$') {
  54. result += '$';
  55. i += 1;
  56. } else if (next === '&') {
  57. result += matched;
  58. i += 1;
  59. } else if (next === '`') {
  60. result += position === 0 ? '' : $strSlice(str, 0, position - 1);
  61. i += 1;
  62. } else if (next === "'") {
  63. result += tailPos >= stringLength ? '' : $strSlice(str, tailPos);
  64. i += 1;
  65. } else {
  66. var nextNext = nextIsLast ? null : $charAt(replacement, i + 2);
  67. if (isDigit(next) && next !== '0' && (nextIsLast || !isDigit(nextNext))) {
  68. // $1 through $9, and not followed by a digit
  69. var n = $parseInt(next, 10);
  70. // if (n > m, impl-defined)
  71. result += n <= m && Type(captures[n - 1]) === 'Undefined' ? '' : captures[n - 1];
  72. i += 1;
  73. } else if (isDigit(next) && (nextIsLast || isDigit(nextNext))) {
  74. // $00 through $99
  75. var nn = next + nextNext;
  76. var nnI = $parseInt(nn, 10) - 1;
  77. // if nn === '00' or nn > m, impl-defined
  78. result += nn <= m && Type(captures[nnI]) === 'Undefined' ? '' : captures[nnI];
  79. i += 2;
  80. } else if (next === '<') {
  81. // eslint-disable-next-line max-depth
  82. if (Type(namedCaptures) === 'Undefined') {
  83. result += '$<';
  84. i += 2;
  85. } else {
  86. var endIndex = $indexOf(replacement, '>', i);
  87. // eslint-disable-next-line max-depth
  88. if (endIndex > -1) {
  89. var groupName = $strSlice(replacement, i + '$<'.length, endIndex);
  90. var capture = Get(namedCaptures, groupName);
  91. // eslint-disable-next-line max-depth
  92. if (Type(capture) !== 'Undefined') {
  93. result += ToString(capture);
  94. }
  95. i += ('<' + groupName + '>').length;
  96. }
  97. }
  98. } else {
  99. result += '$';
  100. }
  101. }
  102. } else {
  103. // the final $, or else not a $
  104. result += $charAt(replacement, i);
  105. }
  106. }
  107. return result;
  108. };