GetSubstitution.js 3.9 KB

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