es.string.replace-all.js 2.8 KB

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