es.string.replace-all.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var call = require('../internals/function-call');
  4. var uncurryThis = require('../internals/function-uncurry-this');
  5. var requireObjectCoercible = require('../internals/require-object-coercible');
  6. var isCallable = require('../internals/is-callable');
  7. var isRegExp = require('../internals/is-regexp');
  8. var toString = require('../internals/to-string');
  9. var getMethod = require('../internals/get-method');
  10. var getRegExpFlags = require('../internals/regexp-get-flags');
  11. var getSubstitution = require('../internals/get-substitution');
  12. var wellKnownSymbol = require('../internals/well-known-symbol');
  13. var IS_PURE = require('../internals/is-pure');
  14. var REPLACE = wellKnownSymbol('replace');
  15. var $TypeError = TypeError;
  16. var indexOf = uncurryThis(''.indexOf);
  17. var replace = uncurryThis(''.replace);
  18. var stringSlice = uncurryThis(''.slice);
  19. var max = Math.max;
  20. var stringIndexOf = function (string, searchValue, fromIndex) {
  21. if (fromIndex > string.length) return -1;
  22. if (searchValue === '') return fromIndex;
  23. return indexOf(string, searchValue, fromIndex);
  24. };
  25. // `String.prototype.replaceAll` method
  26. // https://tc39.es/ecma262/#sec-string.prototype.replaceall
  27. $({ target: 'String', proto: true }, {
  28. replaceAll: function replaceAll(searchValue, replaceValue) {
  29. var O = requireObjectCoercible(this);
  30. var IS_REG_EXP, flags, replacer, string, searchString, functionalReplace, searchLength, advanceBy, replacement;
  31. var position = 0;
  32. var endOfLastMatch = 0;
  33. var result = '';
  34. if (searchValue != null) {
  35. IS_REG_EXP = isRegExp(searchValue);
  36. if (IS_REG_EXP) {
  37. flags = toString(requireObjectCoercible(getRegExpFlags(searchValue)));
  38. if (!~indexOf(flags, 'g')) throw $TypeError('`.replaceAll` does not allow non-global regexes');
  39. }
  40. replacer = getMethod(searchValue, REPLACE);
  41. if (replacer) {
  42. return call(replacer, searchValue, O, replaceValue);
  43. } else if (IS_PURE && IS_REG_EXP) {
  44. return replace(toString(O), searchValue, replaceValue);
  45. }
  46. }
  47. string = toString(O);
  48. searchString = toString(searchValue);
  49. functionalReplace = isCallable(replaceValue);
  50. if (!functionalReplace) replaceValue = toString(replaceValue);
  51. searchLength = searchString.length;
  52. advanceBy = max(1, searchLength);
  53. position = stringIndexOf(string, searchString, 0);
  54. while (position !== -1) {
  55. replacement = functionalReplace
  56. ? toString(replaceValue(searchString, position, string))
  57. : getSubstitution(searchString, string, position, [], undefined, replaceValue);
  58. result += stringSlice(string, endOfLastMatch, position) + replacement;
  59. endOfLastMatch = position + searchLength;
  60. position = stringIndexOf(string, searchString, position + advanceBy);
  61. }
  62. if (endOfLastMatch < string.length) {
  63. result += stringSlice(string, endOfLastMatch);
  64. }
  65. return result;
  66. }
  67. });