source: frontend/node_modules/core-js/internals/fix-regexp-well-known-symbol-logic.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.9 KB
Line 
1'use strict';
2// TODO: Remove from `core-js@4` since it's moved to entry points
3require('../modules/es.regexp.exec');
4var call = require('../internals/function-call');
5var defineBuiltIn = require('../internals/define-built-in');
6var regexpExec = require('../internals/regexp-exec');
7var fails = require('../internals/fails');
8var wellKnownSymbol = require('../internals/well-known-symbol');
9var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
10
11var SPECIES = wellKnownSymbol('species');
12var RegExpPrototype = RegExp.prototype;
13
14module.exports = function (KEY, exec, FORCED, SHAM) {
15 var SYMBOL = wellKnownSymbol(KEY);
16
17 var DELEGATES_TO_SYMBOL = !fails(function () {
18 // String methods call symbol-named RegExp methods
19 var O = {};
20 // eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
21 O[SYMBOL] = function () { return 7; };
22 return ''[KEY](O) !== 7;
23 });
24
25 var DELEGATES_TO_EXEC = DELEGATES_TO_SYMBOL && !fails(function () {
26 // Symbol-named RegExp methods call .exec
27 var execCalled = false;
28 var re = /a/;
29
30 if (KEY === 'split') {
31 // We can't use real regex here since it causes deoptimization
32 // and serious performance degradation in V8
33 // https://github.com/zloirock/core-js/issues/306
34 // RegExp[@@split] doesn't call the regex's exec method, but first creates
35 // a new one. We need to return the patched regex when creating the new one.
36 var constructor = {};
37 // eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
38 constructor[SPECIES] = function () { return re; };
39 re = { constructor: constructor, flags: '' };
40 // eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
41 re[SYMBOL] = /./[SYMBOL];
42 }
43
44 re.exec = function () {
45 execCalled = true;
46 return null;
47 };
48
49 re[SYMBOL]('');
50 return !execCalled;
51 });
52
53 if (
54 !DELEGATES_TO_SYMBOL ||
55 !DELEGATES_TO_EXEC ||
56 FORCED
57 ) {
58 var nativeRegExpMethod = /./[SYMBOL];
59 var methods = exec(SYMBOL, ''[KEY], function (nativeMethod, regexp, str, arg2, forceStringMethod) {
60 var $exec = regexp.exec;
61 if ($exec === regexpExec || $exec === RegExpPrototype.exec) {
62 if (DELEGATES_TO_SYMBOL && !forceStringMethod) {
63 // The native String method already delegates to @@method (this
64 // polyfilled function), leasing to infinite recursion.
65 // We avoid it by directly calling the native @@method method.
66 return { done: true, value: call(nativeRegExpMethod, regexp, str, arg2) };
67 }
68 return { done: true, value: call(nativeMethod, str, regexp, arg2) };
69 }
70 return { done: false };
71 });
72
73 defineBuiltIn(String.prototype, KEY, methods[0]);
74 defineBuiltIn(RegExpPrototype, SYMBOL, methods[1]);
75 }
76
77 if (SHAM) createNonEnumerableProperty(RegExpPrototype[SYMBOL], 'sham', true);
78};
Note: See TracBrowser for help on using the repository browser.