source: imaps-frontend/node_modules/core-js/modules/es.string.replace-all.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.8 KB
Line 
1'use strict';
2var $ = require('../internals/export');
3var call = require('../internals/function-call');
4var uncurryThis = require('../internals/function-uncurry-this');
5var requireObjectCoercible = require('../internals/require-object-coercible');
6var isCallable = require('../internals/is-callable');
7var isNullOrUndefined = require('../internals/is-null-or-undefined');
8var isRegExp = require('../internals/is-regexp');
9var toString = require('../internals/to-string');
10var getMethod = require('../internals/get-method');
11var getRegExpFlags = require('../internals/regexp-get-flags');
12var getSubstitution = require('../internals/get-substitution');
13var wellKnownSymbol = require('../internals/well-known-symbol');
14var IS_PURE = require('../internals/is-pure');
15
16var REPLACE = wellKnownSymbol('replace');
17var $TypeError = TypeError;
18var indexOf = uncurryThis(''.indexOf);
19var replace = uncurryThis(''.replace);
20var stringSlice = uncurryThis(''.slice);
21var 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});
Note: See TracBrowser for help on using the repository browser.