es.string.replace-all.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. var stringIndexOf = function (string, searchValue, fromIndex) {
  22. if (fromIndex > string.length) return -1;
  23. if (searchValue === '') return fromIndex;
  24. return indexOf(string, searchValue, fromIndex);
  25. };
  26. // `String.prototype.replaceAll` method
  27. // https://tc39.es/ecma262/#sec-string.prototype.replaceall
  28. $({ target: 'String', proto: true }, {
  29. replaceAll: function replaceAll(searchValue, replaceValue) {
  30. var O = requireObjectCoercible(this);
  31. var IS_REG_EXP, flags, replacer, string, searchString, functionalReplace, searchLength, advanceBy, replacement;
  32. var position = 0;
  33. var endOfLastMatch = 0;
  34. var result = '';
  35. if (!isNullOrUndefined(searchValue)) {
  36. IS_REG_EXP = isRegExp(searchValue);
  37. if (IS_REG_EXP) {
  38. flags = toString(requireObjectCoercible(getRegExpFlags(searchValue)));
  39. if (!~indexOf(flags, 'g')) throw $TypeError('`.replaceAll` does not allow non-global regexes');
  40. }
  41. replacer = getMethod(searchValue, REPLACE);
  42. if (replacer) {
  43. return call(replacer, searchValue, O, replaceValue);
  44. } else if (IS_PURE && IS_REG_EXP) {
  45. return replace(toString(O), searchValue, replaceValue);
  46. }
  47. }
  48. string = toString(O);
  49. searchString = toString(searchValue);
  50. functionalReplace = isCallable(replaceValue);
  51. if (!functionalReplace) replaceValue = toString(replaceValue);
  52. searchLength = searchString.length;
  53. advanceBy = max(1, searchLength);
  54. position = stringIndexOf(string, searchString, 0);
  55. while (position !== -1) {
  56. replacement = functionalReplace
  57. ? toString(replaceValue(searchString, position, string))
  58. : getSubstitution(searchString, string, position, [], undefined, replaceValue);
  59. result += stringSlice(string, endOfLastMatch, position) + replacement;
  60. endOfLastMatch = position + searchLength;
  61. position = stringIndexOf(string, searchString, position + advanceBy);
  62. }
  63. if (endOfLastMatch < string.length) {
  64. result += stringSlice(string, endOfLastMatch);
  65. }
  66. return result;
  67. }
  68. });