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

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

initial commit

  • Property mode set to 100644
File size: 4.9 KB
Line 
1'use strict';
2var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
3var fails = require('../internals/fails');
4var anObject = require('../internals/an-object');
5var toInteger = require('../internals/to-integer');
6var toLength = require('../internals/to-length');
7var toString = require('../internals/to-string');
8var requireObjectCoercible = require('../internals/require-object-coercible');
9var advanceStringIndex = require('../internals/advance-string-index');
10var getSubstitution = require('../internals/get-substitution');
11var regExpExec = require('../internals/regexp-exec-abstract');
12var wellKnownSymbol = require('../internals/well-known-symbol');
13
14var REPLACE = wellKnownSymbol('replace');
15var max = Math.max;
16var min = Math.min;
17
18var maybeToString = function (it) {
19 return it === undefined ? it : String(it);
20};
21
22// IE <= 11 replaces $0 with the whole match, as if it was $&
23// https://stackoverflow.com/questions/6024666/getting-ie-to-replace-a-regex-with-the-literal-string-0
24var REPLACE_KEEPS_$0 = (function () {
25 // eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing
26 return 'a'.replace(/./, '$0') === '$0';
27})();
28
29// Safari <= 13.0.3(?) substitutes nth capture where n>m with an empty string
30var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = (function () {
31 if (/./[REPLACE]) {
32 return /./[REPLACE]('a', '$0') === '';
33 }
34 return false;
35})();
36
37var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
38 var re = /./;
39 re.exec = function () {
40 var result = [];
41 result.groups = { a: '7' };
42 return result;
43 };
44 return ''.replace(re, '$<a>') !== '7';
45});
46
47// @@replace logic
48fixRegExpWellKnownSymbolLogic('replace', function (_, nativeReplace, maybeCallNative) {
49 var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0';
50
51 return [
52 // `String.prototype.replace` method
53 // https://tc39.es/ecma262/#sec-string.prototype.replace
54 function replace(searchValue, replaceValue) {
55 var O = requireObjectCoercible(this);
56 var replacer = searchValue == undefined ? undefined : searchValue[REPLACE];
57 return replacer !== undefined
58 ? replacer.call(searchValue, O, replaceValue)
59 : nativeReplace.call(toString(O), searchValue, replaceValue);
60 },
61 // `RegExp.prototype[@@replace]` method
62 // https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
63 function (string, replaceValue) {
64 var rx = anObject(this);
65 var S = toString(string);
66
67 if (
68 typeof replaceValue === 'string' &&
69 replaceValue.indexOf(UNSAFE_SUBSTITUTE) === -1 &&
70 replaceValue.indexOf('$<') === -1
71 ) {
72 var res = maybeCallNative(nativeReplace, rx, S, replaceValue);
73 if (res.done) return res.value;
74 }
75
76 var functionalReplace = typeof replaceValue === 'function';
77 if (!functionalReplace) replaceValue = toString(replaceValue);
78
79 var global = rx.global;
80 if (global) {
81 var fullUnicode = rx.unicode;
82 rx.lastIndex = 0;
83 }
84 var results = [];
85 while (true) {
86 var result = regExpExec(rx, S);
87 if (result === null) break;
88
89 results.push(result);
90 if (!global) break;
91
92 var matchStr = toString(result[0]);
93 if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode);
94 }
95
96 var accumulatedResult = '';
97 var nextSourcePosition = 0;
98 for (var i = 0; i < results.length; i++) {
99 result = results[i];
100
101 var matched = toString(result[0]);
102 var position = max(min(toInteger(result.index), S.length), 0);
103 var captures = [];
104 // NOTE: This is equivalent to
105 // captures = result.slice(1).map(maybeToString)
106 // but for some reason `nativeSlice.call(result, 1, result.length)` (called in
107 // the slice polyfill when slicing native arrays) "doesn't work" in safari 9 and
108 // causes a crash (https://pastebin.com/N21QzeQA) when trying to debug it.
109 for (var j = 1; j < result.length; j++) captures.push(maybeToString(result[j]));
110 var namedCaptures = result.groups;
111 if (functionalReplace) {
112 var replacerArgs = [matched].concat(captures, position, S);
113 if (namedCaptures !== undefined) replacerArgs.push(namedCaptures);
114 var replacement = toString(replaceValue.apply(undefined, replacerArgs));
115 } else {
116 replacement = getSubstitution(matched, S, position, captures, namedCaptures, replaceValue);
117 }
118 if (position >= nextSourcePosition) {
119 accumulatedResult += S.slice(nextSourcePosition, position) + replacement;
120 nextSourcePosition = position + matched.length;
121 }
122 }
123 return accumulatedResult + S.slice(nextSourcePosition);
124 }
125 ];
126}, !REPLACE_SUPPORTS_NAMED_GROUPS || !REPLACE_KEEPS_$0 || REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE);
Note: See TracBrowser for help on using the repository browser.