Skip to content

Commit d8bb199

Browse files
committed
style: Polyfill IIFE
1 parent 0470cf7 commit d8bb199

File tree

1 file changed

+123
-121
lines changed

1 file changed

+123
-121
lines changed

src/lib/polyfill.js

Lines changed: 123 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,144 @@
1-
// polyfill for prepend
1+
(function () {
2+
// polyfill for prepend
23

3-
(function (arr) {
4-
arr.forEach(function (item) {
5-
if (item.hasOwnProperty("prepend")) {
6-
return;
7-
}
8-
Object.defineProperty(item, "prepend", {
9-
configurable: true,
10-
enumerable: true,
11-
writable: true,
12-
value: function prepend() {
13-
var argArr = Array.prototype.slice.call(arguments),
14-
docFrag = document.createDocumentFragment();
4+
(function (arr) {
5+
arr.forEach(function (item) {
6+
if (item.hasOwnProperty("prepend")) {
7+
return;
8+
}
9+
Object.defineProperty(item, "prepend", {
10+
configurable: true,
11+
enumerable: true,
12+
writable: true,
13+
value: function prepend() {
14+
var argArr = Array.prototype.slice.call(arguments),
15+
docFrag = document.createDocumentFragment();
1516

16-
argArr.forEach(function (argItem) {
17-
var node =
18-
argItem instanceof Node
19-
? argItem
20-
: document.createTextNode(String(argItem));
21-
docFrag.appendChild(node);
22-
});
17+
argArr.forEach(function (argItem) {
18+
var node =
19+
argItem instanceof Node
20+
? argItem
21+
: document.createTextNode(String(argItem));
22+
docFrag.appendChild(node);
23+
});
2324

24-
this.insertBefore(docFrag, this.firstChild);
25-
},
25+
this.insertBefore(docFrag, this.firstChild);
26+
},
27+
});
2628
});
27-
});
28-
})([Element.prototype, Document.prototype, DocumentFragment.prototype]);
29+
})([Element.prototype, Document.prototype, DocumentFragment.prototype]);
2930

30-
// polyfill for closest
31+
// polyfill for closest
3132

32-
(function (arr) {
33-
arr.forEach(function (item) {
34-
if (item.hasOwnProperty("closest")) {
35-
return;
36-
}
37-
Object.defineProperty(item, "closest", {
38-
configurable: true,
39-
enumerable: true,
40-
writable: true,
41-
value: function closest(s) {
42-
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
43-
i,
44-
el = this;
45-
do {
46-
i = matches.length;
47-
while (--i >= 0 && matches.item(i) !== el) {}
48-
} while (i < 0 && (el = el.parentElement));
49-
return el;
50-
},
33+
(function (arr) {
34+
arr.forEach(function (item) {
35+
if (item.hasOwnProperty("closest")) {
36+
return;
37+
}
38+
Object.defineProperty(item, "closest", {
39+
configurable: true,
40+
enumerable: true,
41+
writable: true,
42+
value: function closest(s) {
43+
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
44+
i,
45+
el = this;
46+
do {
47+
i = matches.length;
48+
while (--i >= 0 && matches.item(i) !== el) {}
49+
} while (i < 0 && (el = el.parentElement));
50+
return el;
51+
},
52+
});
5153
});
52-
});
53-
})([Element.prototype]);
54+
})([Element.prototype]);
5455

55-
// polyfill for replaceWith
56+
// polyfill for replaceWith
5657

57-
(function (arr) {
58-
arr.forEach(function (item) {
59-
if (item.hasOwnProperty("replaceWith")) {
60-
return;
61-
}
62-
Object.defineProperty(item, "replaceWith", {
63-
configurable: true,
64-
enumerable: true,
65-
writable: true,
66-
value: function replaceWith() {
67-
var parent = this.parentNode,
68-
i = arguments.length,
69-
currentNode;
70-
if (!parent) return;
71-
if (!i)
72-
// if there are no arguments
73-
parent.removeChild(this);
74-
while (i--) {
75-
// i-- decrements i and returns the value of i before the decrement
76-
currentNode = arguments[i];
77-
if (typeof currentNode !== "object") {
78-
currentNode = this.ownerDocument.createTextNode(currentNode);
79-
} else if (currentNode.parentNode) {
80-
currentNode.parentNode.removeChild(currentNode);
81-
}
82-
// the value of "i" below is after the decrement
58+
(function (arr) {
59+
arr.forEach(function (item) {
60+
if (item.hasOwnProperty("replaceWith")) {
61+
return;
62+
}
63+
Object.defineProperty(item, "replaceWith", {
64+
configurable: true,
65+
enumerable: true,
66+
writable: true,
67+
value: function replaceWith() {
68+
var parent = this.parentNode,
69+
i = arguments.length,
70+
currentNode;
71+
if (!parent) return;
8372
if (!i)
84-
// if currentNode is the first argument (currentNode === arguments[0])
85-
parent.replaceChild(currentNode, this);
86-
// if currentNode isn't the first
87-
else parent.insertBefore(this.previousSibling, currentNode);
88-
}
89-
},
73+
// if there are no arguments
74+
parent.removeChild(this);
75+
while (i--) {
76+
// i-- decrements i and returns the value of i before the decrement
77+
currentNode = arguments[i];
78+
if (typeof currentNode !== "object") {
79+
currentNode = this.ownerDocument.createTextNode(currentNode);
80+
} else if (currentNode.parentNode) {
81+
currentNode.parentNode.removeChild(currentNode);
82+
}
83+
// the value of "i" below is after the decrement
84+
if (!i)
85+
// if currentNode is the first argument (currentNode === arguments[0])
86+
parent.replaceChild(currentNode, this);
87+
// if currentNode isn't the first
88+
else parent.insertBefore(this.previousSibling, currentNode);
89+
}
90+
},
91+
});
9092
});
91-
});
92-
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
93+
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
9394

94-
// polyfill for toggleAttribute
95+
// polyfill for toggleAttribute
9596

96-
(function (arr) {
97-
arr.forEach(function (item) {
98-
if (item.hasOwnProperty("toggleAttribute")) {
99-
return;
100-
}
101-
Object.defineProperty(item, "toggleAttribute", {
102-
configurable: true,
103-
enumerable: true,
104-
writable: true,
105-
value: function toggleAttribute() {
106-
var attr = arguments[0];
107-
if (this.hasAttribute(attr)) {
108-
this.removeAttribute(attr);
109-
} else {
110-
this.setAttribute(attr, arguments[1] || "");
111-
}
112-
},
97+
(function (arr) {
98+
arr.forEach(function (item) {
99+
if (item.hasOwnProperty("toggleAttribute")) {
100+
return;
101+
}
102+
Object.defineProperty(item, "toggleAttribute", {
103+
configurable: true,
104+
enumerable: true,
105+
writable: true,
106+
value: function toggleAttribute() {
107+
var attr = arguments[0];
108+
if (this.hasAttribute(attr)) {
109+
this.removeAttribute(attr);
110+
} else {
111+
this.setAttribute(attr, arguments[1] || "");
112+
}
113+
},
114+
});
113115
});
114-
});
115-
})([Element.prototype]);
116+
})([Element.prototype]);
116117

117-
// polyfill for performance.now
118+
// polyfill for performance.now
118119

119-
(function () {
120-
if ("performance" in window === false) {
121-
window.performance = {};
122-
}
120+
(function () {
121+
if ("performance" in window === false) {
122+
window.performance = {};
123+
}
123124

124-
Date.now =
125-
Date.now ||
126-
function () {
127-
// thanks IE8
128-
return new Date().getTime();
129-
};
125+
Date.now =
126+
Date.now ||
127+
function () {
128+
// thanks IE8
129+
return new Date().getTime();
130+
};
130131

131-
if ("now" in window.performance === false) {
132-
var nowOffset = Date.now();
132+
if ("now" in window.performance === false) {
133+
var nowOffset = Date.now();
133134

134-
if (performance.timing && performance.timing.navigationStart) {
135-
nowOffset = performance.timing.navigationStart;
136-
}
135+
if (performance.timing && performance.timing.navigationStart) {
136+
nowOffset = performance.timing.navigationStart;
137+
}
137138

138-
window.performance.now = function now() {
139-
return Date.now() - nowOffset;
140-
};
141-
}
139+
window.performance.now = function now() {
140+
return Date.now() - nowOffset;
141+
};
142+
}
143+
})();
142144
})();

0 commit comments

Comments
 (0)