-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathpolyfills.js
More file actions
67 lines (56 loc) · 1.92 KB
/
polyfills.js
File metadata and controls
67 lines (56 loc) · 1.92 KB
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
import "core-js/actual/array/to-spliced";
import "core-js/actual/array/to-sorted";
import "core-js/actual/array/find-last";
import 'core-js/actual/string/starts-with';
import 'core-js/actual/structured-clone';
String.prototype.capitalize = function () {
if (!this) return '';
return this.charAt(0).toUpperCase() + this.slice(1);
}
String.prototype.camelToTitleCase = function () {
if (!this) return '';
const str = String(this).replace(/([A-Z0-9"])/g, ' $1');
return str.charAt(0).toUpperCase() + str.slice(1);
};
String.prototype.capitalizeAllWords = function () {
return this.replace(/\b\w/g, function (char) {
return char.toUpperCase();
});
};
String.prototype.capitalizeAll = function () {
if (!this) return '';
return this.split('_').map((word) => word.capitalize()).join('_');
}
String.prototype.firstCharLowerCase = function () {
return this.charAt(0).toLowerCase() + this.slice(1);
}
String.prototype.toCamelCase = function () {
return this.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
}).replace(/\s+/g, '');
}
Array.prototype.toSimpleObject = function (val = true) {
return this.reduce((res, el) => {
return { ...res, [el]: val };
}, {});
}
Array.prototype.toObjectByIndex = function () {
return Object.entries(this).reduce((res, [key, val]) => {
return { ...res, [key]: val };
}, {});
}
Array.prototype.toChunks = function (perChunk) {
return this.reduce((all, one, i) => {
const ch = Math.floor(i / perChunk);
all[ch] = [].concat((all[ch] || []), one);
return all
}, []);
}
Date.prototype.stdTimezoneOffset = function () {
const jan = new Date(this.getFullYear(), 0, 1);
const jul = new Date(this.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
Date.prototype.isDstObserved = function () {
return this.getTimezoneOffset() < this.stdTimezoneOffset();
}