Skip to content

Commit 90bc74f

Browse files
committed
feat: Add more polyfills and refactor some
1 parent d79a906 commit 90bc74f

File tree

1 file changed

+69
-15
lines changed

1 file changed

+69
-15
lines changed

src/lib/polyfill.js

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
1+
// polyfill for Object.hasOwn
2+
3+
(function () {
4+
var oldHasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
5+
if (oldHasOwn(Object, "hasOwn")) {
6+
return;
7+
}
8+
Object.defineProperty(Object, "hasOwn", {
9+
configurable: true,
10+
enumerable: false,
11+
writable: true,
12+
value: function hasOwn(obj, prop) {
13+
return oldHasOwn(obj, prop);
14+
},
15+
});
16+
Object.hasOwn.prototype = null;
17+
})();
18+
119
// polyfill for prepend
220

321
(function (arr) {
422
arr.forEach(function (item) {
5-
if (item.hasOwnProperty("prepend")) {
23+
if (Object.hasOwn(item, "prepend")) {
624
return;
725
}
826
Object.defineProperty(item, "prepend", {
927
configurable: true,
10-
enumerable: true,
28+
enumerable: false,
1129
writable: true,
1230
value: function prepend() {
13-
var argArr = Array.prototype.slice.call(arguments),
14-
docFrag = document.createDocumentFragment();
31+
var docFrag = document.createDocumentFragment();
1532

16-
argArr.forEach(function (argItem) {
17-
var node =
33+
var i = 0,
34+
argItem,
35+
length = arguments.length;
36+
while (i < length) {
37+
argItem = arguments[i++];
38+
docFrag.appendChild(
1839
argItem instanceof Node
1940
? argItem
20-
: document.createTextNode(String(argItem));
21-
docFrag.appendChild(node);
22-
});
41+
: document.createTextNode(argItem + ""),
42+
);
43+
}
2344

2445
this.insertBefore(docFrag, this.firstChild);
2546
},
2647
});
48+
item.prepend.prototype = null;
2749
});
2850
})([Element.prototype, Document.prototype, DocumentFragment.prototype]);
2951

@@ -36,7 +58,7 @@
3658
}
3759
Object.defineProperty(item, "closest", {
3860
configurable: true,
39-
enumerable: true,
61+
enumerable: false,
4062
writable: true,
4163
value: function closest(s) {
4264
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
@@ -49,19 +71,20 @@
4971
return el;
5072
},
5173
});
74+
item.closest.prototype = null;
5275
});
5376
})([Element.prototype]);
5477

5578
// polyfill for replaceWith
5679

5780
(function (arr) {
5881
arr.forEach(function (item) {
59-
if (item.hasOwnProperty("replaceWith")) {
82+
if (Object.hasOwn(item, "replaceWith")) {
6083
return;
6184
}
6285
Object.defineProperty(item, "replaceWith", {
6386
configurable: true,
64-
enumerable: true,
87+
enumerable: false,
6588
writable: true,
6689
value: function replaceWith() {
6790
var parent = this.parentNode,
@@ -88,29 +111,31 @@
88111
}
89112
},
90113
});
114+
item.replaceWith.prototype = null;
91115
});
92116
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
93117

94118
// polyfill for toggleAttribute
95119

96120
(function (arr) {
97121
arr.forEach(function (item) {
98-
if (item.hasOwnProperty("toggleAttribute")) {
122+
if (Object.hasOwn(item, "toggleAttribute")) {
99123
return;
100124
}
101125
Object.defineProperty(item, "toggleAttribute", {
102126
configurable: true,
103-
enumerable: true,
127+
enumerable: false,
104128
writable: true,
105129
value: function toggleAttribute() {
106130
var attr = arguments[0];
107131
if (this.hasAttribute(attr)) {
108132
this.removeAttribute(attr);
109133
} else {
110-
this.setAttribute(attr, arguments[1] || "");
134+
this.setAttribute(attr, arguments.length >= 2 ? arguments[1] : "");
111135
}
112136
},
113137
});
138+
item.toggleAttribute.prototype = null;
114139
});
115140
})([Element.prototype]);
116141

@@ -140,3 +165,32 @@
140165
};
141166
}
142167
})();
168+
169+
// polyfill for Promise.withResolvers
170+
171+
if (!Object.hasOwn(Promise, "withResolvers"))
172+
(function () {
173+
Object.defineProperty(Promise, "withResolvers", {
174+
configurable: true,
175+
enumerable: false,
176+
writable: true,
177+
value: function withResolvers() {
178+
var resolve, reject;
179+
var promise = new this(function (_resolve, _reject) {
180+
resolve = _resolve;
181+
reject = _reject;
182+
});
183+
if (typeof resolve !== "function" || typeof reject !== "function") {
184+
throw new TypeError(
185+
"Promise resolve or reject function is not callable",
186+
);
187+
}
188+
return {
189+
promise: promise,
190+
resolve: resolve,
191+
reject: reject,
192+
};
193+
},
194+
});
195+
Promise.withResolvers.prototype = null;
196+
})();

0 commit comments

Comments
 (0)