mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-01 01:38:07 +08:00
Change-Id: I3fcf49bc416301a030d298cfc48c448bfeba4335 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/1878808 Reviewed-by: Tim van der Lippe <tvanderlippe@chromium.org> Reviewed-by: Yang Guo <yangguo@chromium.org> Commit-Queue: Paul Lewis <aerotwist@chromium.org>
48 lines
1.8 KiB
JavaScript
48 lines
1.8 KiB
JavaScript
'use strict';
|
|
var $ = require('../internals/export');
|
|
var requireObjectCoercible = require('../internals/require-object-coercible');
|
|
var isRegExp = require('../internals/is-regexp');
|
|
var getRegExpFlags = require('../internals/regexp-flags');
|
|
var wellKnownSymbol = require('../internals/well-known-symbol');
|
|
var IS_PURE = require('../internals/is-pure');
|
|
|
|
var REPLACE = wellKnownSymbol('replace');
|
|
var RegExpPrototype = RegExp.prototype;
|
|
|
|
// `String.prototype.replaceAll` method
|
|
// https://github.com/tc39/proposal-string-replace-all
|
|
$({ target: 'String', proto: true }, {
|
|
replaceAll: function replaceAll(searchValue, replaceValue) {
|
|
var O = requireObjectCoercible(this);
|
|
var IS_REG_EXP, flags, replacer, string, searchString, template, result, index;
|
|
if (searchValue != null) {
|
|
IS_REG_EXP = isRegExp(searchValue);
|
|
if (IS_REG_EXP) {
|
|
flags = String(requireObjectCoercible('flags' in RegExpPrototype
|
|
? searchValue.flags
|
|
: getRegExpFlags.call(searchValue)
|
|
));
|
|
if (!~flags.indexOf('g')) throw TypeError('`.replaceAll` does not allow non-global regexes');
|
|
}
|
|
replacer = searchValue[REPLACE];
|
|
if (replacer !== undefined) {
|
|
return replacer.call(searchValue, O, replaceValue);
|
|
} else if (IS_PURE && IS_REG_EXP) {
|
|
return String(O).replace(searchValue, replaceValue);
|
|
}
|
|
}
|
|
string = String(O);
|
|
searchString = String(searchValue);
|
|
template = string.split(searchString);
|
|
if (typeof replaceValue !== 'function') {
|
|
return template.join(String(replaceValue));
|
|
}
|
|
result = template[0];
|
|
for (index = 1; index < template.length; index++) {
|
|
result += String(replaceValue(searchString, index - 1, string));
|
|
result += template[index];
|
|
}
|
|
return result;
|
|
}
|
|
});
|