-
Notifications
You must be signed in to change notification settings - Fork 1
/
mini-pf.js
128 lines (116 loc) · 3.29 KB
/
mini-pf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//--- Object ----------------------------------------------------------------
if (!Object.assign) {
Object.assign = $.extend;
}
//--- String ----------------------------------------------------------------
if (!String.prototype.trim) { // ??? only very old IE ???
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
}
function _repeatTo(str, tLen) {
var l1 = str.length;
if (!l1 || !tLen) return '';
if (tLen >= l1) {
while (l1 < tLen) {
var l2 = l1 << 1; // double it
str += (tLen <= l2) ? str : str.substr(0, tLen - l1);
l1 = l2;
}
} else { // target length < initial length
str = str.substr(0, tLen);
}
return str;
}
if (!String.prototype.repeat) {
String.prototype.repeat = function(cnt) {
return _repeatTo(this, this.length*cnt); // Array(cnt+1).join(this);
};
}
// from ECMAScript 2017
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(tLen, padS) {
tLen -= this.length;
return (tLen > 0) ? (_repeatTo(padS || ' ', tLen) + this) : this;
};
}
if (!String.prototype.padEnd) {
String.prototype.padEnd = function padStart(tLen, padS) {
tLen -= this.length;
return (tLen > 0) ? (this + _repeatTo(padS || ' ', tLen)) : this;
};
}
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(sub) {
return this.lastIndexOf(sub, 0) === 0;
};
}
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(sub) {
var l0 = this.length, l1 = sub && sub.length || 0, dl = l0 - l1;
return (dl >= 0) && (this.indexOf(sub, dl) == dl);
};
}
//--- Array -----------------------------------------------------------------
if (!Array.prototype.find) {
//** @type function(Function, Object=) */
Array.prototype.find = function(predicate, thisArg) {
var o;
this.some(function(item, i) {
if (predicate.call(thisArg, item, i, this)) {
o = item;
return true;
}
}, this);
return o;
};
}
if (!Array.prototype.findIndex) {
//** @type function(Function, Object=) */
Array.prototype.findIndex = function(predicate, thisArg) {
var idx = -1;
this.some(function(item, i) {
if (predicate.call(thisArg, item, i, this)) {
idx = i;
return true;
}
}, this);
return idx;
};
}
//--- Element ---------------------------------------------------------------
var pfFnMatches = null;
if (!Element.prototype.matches) {
// does the element match the given selector
Element.prototype.matches =
Element.prototype.matchesSelector ||
Element.prototype.mozMatchesSelector ||
Element.prototype.msMatchesSelector ||
Element.prototype.oMatchesSelector ||
Element.prototype.webkitMatchesSelector ||
(pfFnMatches = function(s) { return $(this).is(s); });
}
if (!Element.prototype.closest) {
// find closest ancestor matching selector
if (pfFnMatches) {
// no native "matches", do not use it for closest()
Element.prototype.closest = function(sel) {
return $(this).closest(sel)[0];
};
} else {
Element.prototype.closest = function(sel) {
var el = this;
while (el) {
if (el.nodeType == 1 && el.matches(sel))
break;
el = el.parentNode;
}
return /** @type {Element} */ (el);
};
}
}
if (!Element.prototype.remove) {
// remove this (current) element from dom
Element.prototype.remove = function() {
var p = this.parentElement || this.parentNode;
p && p.removeChild(this);
}
}