Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

es.string.replace-all.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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, position, replacement;
  27. var endOfLastMatch = 0;
  28. var result = '';
  29. if (!isNullOrUndefined(searchValue)) {
  30. IS_REG_EXP = isRegExp(searchValue);
  31. if (IS_REG_EXP) {
  32. flags = toString(requireObjectCoercible(getRegExpFlags(searchValue)));
  33. if (!~indexOf(flags, 'g')) throw new $TypeError('`.replaceAll` does not allow non-global regexes');
  34. }
  35. replacer = getMethod(searchValue, REPLACE);
  36. if (replacer) return call(replacer, searchValue, O, replaceValue);
  37. if (IS_PURE && IS_REG_EXP) return replace(toString(O), searchValue, replaceValue);
  38. }
  39. string = toString(O);
  40. searchString = toString(searchValue);
  41. functionalReplace = isCallable(replaceValue);
  42. if (!functionalReplace) replaceValue = toString(replaceValue);
  43. searchLength = searchString.length;
  44. advanceBy = max(1, searchLength);
  45. position = indexOf(string, searchString);
  46. while (position !== -1) {
  47. replacement = functionalReplace
  48. ? toString(replaceValue(searchString, position, string))
  49. : getSubstitution(searchString, string, position, [], undefined, replaceValue);
  50. result += stringSlice(string, endOfLastMatch, position) + replacement;
  51. endOfLastMatch = position + searchLength;
  52. position = position + advanceBy > string.length ? -1 : indexOf(string, searchString, position + advanceBy);
  53. }
  54. if (endOfLastMatch < string.length) {
  55. result += stringSlice(string, endOfLastMatch);
  56. }
  57. return result;
  58. }
  59. });