-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (106 loc) · 2.91 KB
/
index.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
Array.prototype.random = function() {
return this[Math.floor((Math.random()*this.length))];
}
const arrs = new Map();
Array.prototype.randomFlush = function (identifier) {
let random = this[Math.floor((Math.random()*this.length))];
if (!arrs.has(identifier)) arrs.set(identifier, new Set());
const usedCache = arrs.get(identifier);
while (usedCache.has(random)) {
if (usedCache.size === this.length) {
usedCache.clear();
};
random = this[Math.floor((Math.random()*this.length))];
};
usedCache.add(random);
return random;
}; //one wrong move and kaboom
Object.defineProperty(Array.prototype, "splitTo", {
value: function (size) {
let returnVal = [];
for (let i = 0; i < this.length; i += size)
returnVal.push(this.slice(i, i + size));
return returnVal;
}
});
const sliceSize = 9; //fixed
let chunkString = function(s, n = sliceSize) {
if(s.split && typeof n === "number") {
var l = s.length,
z = ~~(l/n),
zx = (z*n<l) ? l-z*(n-1) : z;
let a = [];
for (var i=0; i<n-1; i++) {
a[i] = s.slice(i*z,i*z+z);
};
a[Math.ceil(n-1)] = s.slice(-zx);
return a;
} else {
return false;
}
};
//this is actually useful
function memoize(fn) {
let cache = {};
return function(...args) {
let key = JSON.stringify(args);
if (key in cache) {
return cache[key];
} else {
let result = fn(...args);
cache[key] = result;
return result;
}
};
};
// String.prototype.capitalize() method
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
};
// Number.prototype.clamp() method
Number.prototype.clamp = function(min, max) {
return Math.min(Math.max(this, min), max);
};
// Function.prototype.before() method
Function.prototype.before = function(beforeFn) {
return (...args) => {
beforeFn();
return this(...args);
};
};
// Function.prototype.after() method
Function.prototype.after = function(afterFn) {
return (...args) => {
let result = this(...args);
afterFn();
return result;
};
};
// Removes duplicates from an array
Array.prototype.unique = function() {
return [...new Set(this)];
};
// Returns the last item in an array
Array.prototype.last = function() {
return this[this.length - 1];
};
// Removes an item from an array by value
Array.prototype.remove = function(value) {
const index = this.indexOf(value);
if (index !== -1) {
this.splice(index, 1);
}
return this;
};
// Returns the sum of all values in an array of numbers
Array.prototype.sum = function() {
return this.reduce((sum, value) => sum + value, 0);
};
// Returns the average of all values in an array of numbers
Array.prototype.average = function() {
return this.sum() / this.length;
};
module.exports = {
chunkString,
memoize
};