source: trip-planner-front/node_modules/core-js/modules/es.string.replace-all.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1'use strict';
2var $ = require('../internals/export');
3var requireObjectCoercible = require('../internals/require-object-coercible');
4var isRegExp = require('../internals/is-regexp');
5var toString = require('../internals/to-string');
6var getRegExpFlags = require('../internals/regexp-flags');
7var getSubstitution = require('../internals/get-substitution');
8var wellKnownSymbol = require('../internals/well-known-symbol');
9var IS_PURE = require('../internals/is-pure');
10
11var REPLACE = wellKnownSymbol('replace');
12var RegExpPrototype = RegExp.prototype;
13var max = Math.max;
14
15var 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});
Note: See TracBrowser for help on using the repository browser.