massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / core-js / modules / es.string.replace-all.js
1 'use strict';
2 var $ = require('../internals/export');
3 var global = require('../internals/global');
4 var call = require('../internals/function-call');
5 var uncurryThis = require('../internals/function-uncurry-this');
6 var requireObjectCoercible = require('../internals/require-object-coercible');
7 var isCallable = require('../internals/is-callable');
8 var isRegExp = require('../internals/is-regexp');
9 var toString = require('../internals/to-string');
10 var getMethod = require('../internals/get-method');
11 var regExpFlags = require('../internals/regexp-flags');
12 var getSubstitution = require('../internals/get-substitution');
13 var wellKnownSymbol = require('../internals/well-known-symbol');
14 var IS_PURE = require('../internals/is-pure');
15
16 var REPLACE = wellKnownSymbol('replace');
17 var RegExpPrototype = RegExp.prototype;
18 var TypeError = global.TypeError;
19 var getFlags = uncurryThis(regExpFlags);
20 var indexOf = uncurryThis(''.indexOf);
21 var replace = uncurryThis(''.replace);
22 var stringSlice = uncurryThis(''.slice);
23 var max = Math.max;
24
25 var stringIndexOf = function (string, searchValue, fromIndex) {
26   if (fromIndex > string.length) return -1;
27   if (searchValue === '') return fromIndex;
28   return indexOf(string, searchValue, fromIndex);
29 };
30
31 // `String.prototype.replaceAll` method
32 // https://tc39.es/ecma262/#sec-string.prototype.replaceall
33 $({ target: 'String', proto: true }, {
34   replaceAll: function replaceAll(searchValue, replaceValue) {
35     var O = requireObjectCoercible(this);
36     var IS_REG_EXP, flags, replacer, string, searchString, functionalReplace, searchLength, advanceBy, replacement;
37     var position = 0;
38     var endOfLastMatch = 0;
39     var result = '';
40     if (searchValue != null) {
41       IS_REG_EXP = isRegExp(searchValue);
42       if (IS_REG_EXP) {
43         flags = toString(requireObjectCoercible('flags' in RegExpPrototype
44           ? searchValue.flags
45           : getFlags(searchValue)
46         ));
47         if (!~indexOf(flags, 'g')) throw TypeError('`.replaceAll` does not allow non-global regexes');
48       }
49       replacer = getMethod(searchValue, REPLACE);
50       if (replacer) {
51         return call(replacer, searchValue, O, replaceValue);
52       } else if (IS_PURE && IS_REG_EXP) {
53         return replace(toString(O), searchValue, replaceValue);
54       }
55     }
56     string = toString(O);
57     searchString = toString(searchValue);
58     functionalReplace = isCallable(replaceValue);
59     if (!functionalReplace) replaceValue = toString(replaceValue);
60     searchLength = searchString.length;
61     advanceBy = max(1, searchLength);
62     position = stringIndexOf(string, searchString, 0);
63     while (position !== -1) {
64       replacement = functionalReplace
65         ? toString(replaceValue(searchString, position, string))
66         : getSubstitution(searchString, string, position, [], undefined, replaceValue);
67       result += stringSlice(string, endOfLastMatch, position) + replacement;
68       endOfLastMatch = position + searchLength;
69       position = stringIndexOf(string, searchString, position + advanceBy);
70     }
71     if (endOfLastMatch < string.length) {
72       result += stringSlice(string, endOfLastMatch);
73     }
74     return result;
75   }
76 });