1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var requireObjectCoercible = require('../internals/require-object-coercible');
|
---|
4 | var isRegExp = require('../internals/is-regexp');
|
---|
5 | var toString = require('../internals/to-string');
|
---|
6 | var getRegExpFlags = require('../internals/regexp-flags');
|
---|
7 | var getSubstitution = require('../internals/get-substitution');
|
---|
8 | var wellKnownSymbol = require('../internals/well-known-symbol');
|
---|
9 | var IS_PURE = require('../internals/is-pure');
|
---|
10 |
|
---|
11 | var REPLACE = wellKnownSymbol('replace');
|
---|
12 | var RegExpPrototype = RegExp.prototype;
|
---|
13 | var max = Math.max;
|
---|
14 |
|
---|
15 | var stringIndexOf = function (string, searchValue, fromIndex) {
|
---|
16 | if (fromIndex > string.length) return -1;
|
---|
17 | if (searchValue === '') return fromIndex;
|
---|
18 | return string.indexOf(searchValue, fromIndex);
|
---|
19 | };
|
---|
20 |
|
---|
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 (searchValue != null) {
|
---|
31 | IS_REG_EXP = isRegExp(searchValue);
|
---|
32 | if (IS_REG_EXP) {
|
---|
33 | flags = toString(requireObjectCoercible('flags' in RegExpPrototype
|
---|
34 | ? searchValue.flags
|
---|
35 | : getRegExpFlags.call(searchValue)
|
---|
36 | ));
|
---|
37 | if (!~flags.indexOf('g')) throw TypeError('`.replaceAll` does not allow non-global regexes');
|
---|
38 | }
|
---|
39 | replacer = searchValue[REPLACE];
|
---|
40 | if (replacer !== undefined) {
|
---|
41 | return replacer.call(searchValue, O, replaceValue);
|
---|
42 | } else if (IS_PURE && IS_REG_EXP) {
|
---|
43 | return toString(O).replace(searchValue, replaceValue);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | string = toString(O);
|
---|
47 | searchString = toString(searchValue);
|
---|
48 | functionalReplace = typeof replaceValue === 'function';
|
---|
49 | if (!functionalReplace) replaceValue = toString(replaceValue);
|
---|
50 | searchLength = searchString.length;
|
---|
51 | advanceBy = max(1, searchLength);
|
---|
52 | position = stringIndexOf(string, searchString, 0);
|
---|
53 | while (position !== -1) {
|
---|
54 | if (functionalReplace) {
|
---|
55 | replacement = toString(replaceValue(searchString, position, string));
|
---|
56 | } else {
|
---|
57 | replacement = getSubstitution(searchString, string, position, [], undefined, replaceValue);
|
---|
58 | }
|
---|
59 | result += string.slice(endOfLastMatch, position) + replacement;
|
---|
60 | endOfLastMatch = position + searchLength;
|
---|
61 | position = stringIndexOf(string, searchString, position + advanceBy);
|
---|
62 | }
|
---|
63 | if (endOfLastMatch < string.length) {
|
---|
64 | result += string.slice(endOfLastMatch);
|
---|
65 | }
|
---|
66 | return result;
|
---|
67 | }
|
---|
68 | });
|
---|