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