refactor: polyfills review

This commit is contained in:
dongchengjie
2024-05-30 20:27:12 +08:00
parent 3514cfbd44
commit 01d67eb239
6 changed files with 79 additions and 93 deletions

View File

@@ -1,50 +1,30 @@
(function (global) {
if (typeof global === "object" && global) {
if (typeof global.RegExp !== "undefined") {
const OriginalRegExp = global.RegExp;
const CustomRegExp = function (pattern, flags) {
if (typeof pattern === "string" && typeof flags === "string") {
flags = flags;
} else if (pattern instanceof OriginalRegExp && flags === undefined) {
flags = pattern.flags;
}
if (flags) {
if (!global.RegExp.prototype.hasOwnProperty("unicodeSets")) {
if (flags.includes("v")) {
flags = flags.replace("v", "u");
}
}
if (!global.RegExp.prototype.hasOwnProperty("hasIndices")) {
if (flags.includes("d")) {
flags = flags.replace("d", "");
}
}
}
return new OriginalRegExp(pattern, flags);
};
CustomRegExp.prototype = OriginalRegExp.prototype;
global.RegExp = CustomRegExp;
}
(function () {
if (typeof window.RegExp === "undefined") {
return;
}
})(
(function () {
switch (true) {
case typeof globalThis === "object" && !!globalThis:
return globalThis;
case typeof self === "object" && !!self:
return self;
case typeof window === "object" && !!window:
return window;
case typeof global === "object" && !!global:
return global;
case typeof Function === "function":
return Function("return this")();
const originalRegExp = window.RegExp;
window.RegExp = function (pattern, flags) {
if (pattern instanceof originalRegExp && flags === undefined) {
flags = pattern.flags;
}
return null;
})()
);
if (flags) {
if (!originalRegExp.prototype.hasOwnProperty("unicodeSets")) {
if (flags.includes("v")) {
flags = flags.replace("v", "u");
}
}
if (!originalRegExp.prototype.hasOwnProperty("hasIndices")) {
if (flags.includes("d")) {
flags = flags.replace("d", "");
}
}
}
return new originalRegExp(pattern, flags);
};
window.RegExp.prototype = originalRegExp.prototype;
})();