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