GetSubstitution.js 4.4 KB

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