Actualizacion maquina principal
[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 toObject = require('../internals/to-object');
5 var toLength = require('../internals/to-length');
6 var toInteger = require('../internals/to-integer');
7 var requireObjectCoercible = require('../internals/require-object-coercible');
8 var advanceStringIndex = require('../internals/advance-string-index');
9 var regExpExec = require('../internals/regexp-exec-abstract');
10
11 var max = Math.max;
12 var min = Math.min;
13 var floor = Math.floor;
14 var SUBSTITUTION_SYMBOLS = /\$([$&'`]|\d\d?|<[^>]*>)/g;
15 var SUBSTITUTION_SYMBOLS_NO_NAMED = /\$([$&'`]|\d\d?)/g;
16
17 var maybeToString = function (it) {
18   return it === undefined ? it : String(it);
19 };
20
21 // @@replace logic
22 fixRegExpWellKnownSymbolLogic('replace', 2, function (REPLACE, nativeReplace, maybeCallNative, reason) {
23   var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = reason.REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE;
24   var REPLACE_KEEPS_$0 = reason.REPLACE_KEEPS_$0;
25   var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0';
26
27   return [
28     // `String.prototype.replace` method
29     // https://tc39.github.io/ecma262/#sec-string.prototype.replace
30     function replace(searchValue, replaceValue) {
31       var O = requireObjectCoercible(this);
32       var replacer = searchValue == undefined ? undefined : searchValue[REPLACE];
33       return replacer !== undefined
34         ? replacer.call(searchValue, O, replaceValue)
35         : nativeReplace.call(String(O), searchValue, replaceValue);
36     },
37     // `RegExp.prototype[@@replace]` method
38     // https://tc39.github.io/ecma262/#sec-regexp.prototype-@@replace
39     function (regexp, replaceValue) {
40       if (
41         (!REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE && REPLACE_KEEPS_$0) ||
42         (typeof replaceValue === 'string' && replaceValue.indexOf(UNSAFE_SUBSTITUTE) === -1)
43       ) {
44         var res = maybeCallNative(nativeReplace, regexp, this, replaceValue);
45         if (res.done) return res.value;
46       }
47
48       var rx = anObject(regexp);
49       var S = String(this);
50
51       var functionalReplace = typeof replaceValue === 'function';
52       if (!functionalReplace) replaceValue = String(replaceValue);
53
54       var global = rx.global;
55       if (global) {
56         var fullUnicode = rx.unicode;
57         rx.lastIndex = 0;
58       }
59       var results = [];
60       while (true) {
61         var result = regExpExec(rx, S);
62         if (result === null) break;
63
64         results.push(result);
65         if (!global) break;
66
67         var matchStr = String(result[0]);
68         if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode);
69       }
70
71       var accumulatedResult = '';
72       var nextSourcePosition = 0;
73       for (var i = 0; i < results.length; i++) {
74         result = results[i];
75
76         var matched = String(result[0]);
77         var position = max(min(toInteger(result.index), S.length), 0);
78         var captures = [];
79         // NOTE: This is equivalent to
80         //   captures = result.slice(1).map(maybeToString)
81         // but for some reason `nativeSlice.call(result, 1, result.length)` (called in
82         // the slice polyfill when slicing native arrays) "doesn't work" in safari 9 and
83         // causes a crash (https://pastebin.com/N21QzeQA) when trying to debug it.
84         for (var j = 1; j < result.length; j++) captures.push(maybeToString(result[j]));
85         var namedCaptures = result.groups;
86         if (functionalReplace) {
87           var replacerArgs = [matched].concat(captures, position, S);
88           if (namedCaptures !== undefined) replacerArgs.push(namedCaptures);
89           var replacement = String(replaceValue.apply(undefined, replacerArgs));
90         } else {
91           replacement = getSubstitution(matched, S, position, captures, namedCaptures, replaceValue);
92         }
93         if (position >= nextSourcePosition) {
94           accumulatedResult += S.slice(nextSourcePosition, position) + replacement;
95           nextSourcePosition = position + matched.length;
96         }
97       }
98       return accumulatedResult + S.slice(nextSourcePosition);
99     }
100   ];
101
102   // https://tc39.github.io/ecma262/#sec-getsubstitution
103   function getSubstitution(matched, str, position, captures, namedCaptures, replacement) {
104     var tailPos = position + matched.length;
105     var m = captures.length;
106     var symbols = SUBSTITUTION_SYMBOLS_NO_NAMED;
107     if (namedCaptures !== undefined) {
108       namedCaptures = toObject(namedCaptures);
109       symbols = SUBSTITUTION_SYMBOLS;
110     }
111     return nativeReplace.call(replacement, symbols, function (match, ch) {
112       var capture;
113       switch (ch.charAt(0)) {
114         case '$': return '$';
115         case '&': return matched;
116         case '`': return str.slice(0, position);
117         case "'": return str.slice(tailPos);
118         case '<':
119           capture = namedCaptures[ch.slice(1, -1)];
120           break;
121         default: // \d\d?
122           var n = +ch;
123           if (n === 0) return match;
124           if (n > m) {
125             var f = floor(n / 10);
126             if (f === 0) return match;
127             if (f <= m) return captures[f - 1] === undefined ? ch.charAt(1) : captures[f - 1] + ch.charAt(1);
128             return match;
129           }
130           capture = captures[n - 1];
131       }
132       return capture === undefined ? '' : capture;
133     });
134   }
135 });