.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / core-js / modules / es.string.replace.js
1 'use strict';
2 var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
3 var anObject = require('../internals/an-object');
4 var toLength = require('../internals/to-length');
5 var toInteger = require('../internals/to-integer');
6 var requireObjectCoercible = require('../internals/require-object-coercible');
7 var advanceStringIndex = require('../internals/advance-string-index');
8 var getSubstitution = require('../internals/get-substitution');
9 var regExpExec = require('../internals/regexp-exec-abstract');
10
11 var max = Math.max;
12 var min = Math.min;
13
14 var maybeToString = function (it) {
15   return it === undefined ? it : String(it);
16 };
17
18 // @@replace logic
19 fixRegExpWellKnownSymbolLogic('replace', 2, function (REPLACE, nativeReplace, maybeCallNative, reason) {
20   var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = reason.REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE;
21   var REPLACE_KEEPS_$0 = reason.REPLACE_KEEPS_$0;
22   var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0';
23
24   return [
25     // `String.prototype.replace` method
26     // https://tc39.es/ecma262/#sec-string.prototype.replace
27     function replace(searchValue, replaceValue) {
28       var O = requireObjectCoercible(this);
29       var replacer = searchValue == undefined ? undefined : searchValue[REPLACE];
30       return replacer !== undefined
31         ? replacer.call(searchValue, O, replaceValue)
32         : nativeReplace.call(String(O), searchValue, replaceValue);
33     },
34     // `RegExp.prototype[@@replace]` method
35     // https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
36     function (regexp, replaceValue) {
37       if (
38         (!REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE && REPLACE_KEEPS_$0) ||
39         (typeof replaceValue === 'string' && replaceValue.indexOf(UNSAFE_SUBSTITUTE) === -1)
40       ) {
41         var res = maybeCallNative(nativeReplace, regexp, this, replaceValue);
42         if (res.done) return res.value;
43       }
44
45       var rx = anObject(regexp);
46       var S = String(this);
47
48       var functionalReplace = typeof replaceValue === 'function';
49       if (!functionalReplace) replaceValue = String(replaceValue);
50
51       var global = rx.global;
52       if (global) {
53         var fullUnicode = rx.unicode;
54         rx.lastIndex = 0;
55       }
56       var results = [];
57       while (true) {
58         var result = regExpExec(rx, S);
59         if (result === null) break;
60
61         results.push(result);
62         if (!global) break;
63
64         var matchStr = String(result[0]);
65         if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode);
66       }
67
68       var accumulatedResult = '';
69       var nextSourcePosition = 0;
70       for (var i = 0; i < results.length; i++) {
71         result = results[i];
72
73         var matched = String(result[0]);
74         var position = max(min(toInteger(result.index), S.length), 0);
75         var captures = [];
76         // NOTE: This is equivalent to
77         //   captures = result.slice(1).map(maybeToString)
78         // but for some reason `nativeSlice.call(result, 1, result.length)` (called in
79         // the slice polyfill when slicing native arrays) "doesn't work" in safari 9 and
80         // causes a crash (https://pastebin.com/N21QzeQA) when trying to debug it.
81         for (var j = 1; j < result.length; j++) captures.push(maybeToString(result[j]));
82         var namedCaptures = result.groups;
83         if (functionalReplace) {
84           var replacerArgs = [matched].concat(captures, position, S);
85           if (namedCaptures !== undefined) replacerArgs.push(namedCaptures);
86           var replacement = String(replaceValue.apply(undefined, replacerArgs));
87         } else {
88           replacement = getSubstitution(matched, S, position, captures, namedCaptures, replaceValue);
89         }
90         if (position >= nextSourcePosition) {
91           accumulatedResult += S.slice(nextSourcePosition, position) + replacement;
92           nextSourcePosition = position + matched.length;
93         }
94       }
95       return accumulatedResult + S.slice(nextSourcePosition);
96     }
97   ];
98 });