-
Notifications
You must be signed in to change notification settings - Fork 0
/
supplementary.js
55 lines (28 loc) · 1.26 KB
/
supplementary.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
Array.prototype.pick = function () { return this[Math.floor(Math.random() * (this.length - 1))] };
Object.defineProperty(Array.prototype, 'first', {
get () { return this[0] },
set (value) { this.unshift(value) }
});
Object.defineProperty(Array.prototype, 'last', {
get () { return this[this.length - 1] },
set (value) { this[this.length] = value }
});
Array.prototype.remove = function (item) {
if (!this.includes(item)) console.warn(`Tried to remove item ${item} from array ${this} but array does not contain item.`);
this.splice(this.indexOf(item), 1);
return this;
};
Array.prototype.shuffle = function () {
for (let index = this.length - 1; index > 0; index--) {
let random = Math.floor(Math.random() * (index + 1));
[this[index], this[random]] = [this[random], this[index]];
};
return this;
};
Array.through = function (length = 0, from = 0, step = 1) {
return Array.from("0".repeat(length)).map((item, index) => from + (index * step));
};
// String.prototype.belongsTo = function (array) { return array.includes(this.valueOf()) };
Object.prototype.belongsTo = function (array) { return array.includes(this.valueOf()) };
Number.prototype.ms = function () { return this * 1000 };
Object.prototype.pipe = function (fn) { return fn(this.valueOf()) };