-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswoop-utils.js
271 lines (271 loc) · 7.73 KB
/
swoop-utils.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const SWOOP = {
//array functions
ARR: {
//union of 2 arrays
union: function(arr1,arr2) {
return [...new Set([...arr1,...arr2])]
},
//intersection of 2 arrays
intersection: function (arr1,arr2) {
return arr1.filter(element=>{
return arr2.includes(element)
})
},
difference: function(arr1,arr2) {
const a = arr1.filter(element=>{
return !arr2.includes(element)
})
const b = arr2.filter(element=>{
return !arr1.includes(element)
})
return a.concat(b)
},
//returns the sum of all elements in an array
sum: function(arr) {
let total = 0;
for (var i = 0; i < arr.length; i++) {
total += arr[i];
}
return total;
},
//returns the mean of an array
mean: function(arr) {
const len = arr.length;
const mean = SWOOP.ARR.sum(arr) / len;
return mean;
},
//returns the standard deviation of an array
standard_deviation: function(arr) {
const m = SWOOP.ARR.mean(arr);
const s = arr.length - 1;
const variance = arr.map(x => (x - m) ** 2).reduce((a, b) => a + b, 0) / s;
return Math.sqrt(variance);
},
//returns the variance of an array
variance: function(arr) {
const m = SWOOP.ARR.mean(arr);
const s = arr.length - 1;
const variance = arr.map(x => (x - m) ** 2).reduce((a, b) => a + b, 0) / s;
return variance;
},
//returns the median of an array
median: function(arr) {
arr.sort((a, b) => a - b);
const middle = Math.floor(arr.length / 2);
if (arr.length % 2 === 0) {
return (arr[middle - 1] + arr[middle]);
} else {
return arr[middle];
}
},
//returns the minimum value of an array
min: function(arr) {
let min = arr[0];
for (var i = 0; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
},
//returns the maximum value of an array
max: function(arr) {
let max = arr[0];
for (var i = 0; i < arr.length; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
},
//counts the number of unique values in an array
countUnique: function(arr) {
return arr.filter((value, index, self) => self.indexOf(value) === index).length;
},
//counts the number of duplicate values of an arrat
countDuplicates: function(arr) {
return arr.filter((value, index, self) => arr.indexOf(value) !== index).length;
},
//merges 2 arrays
merge: function(arr1, arr2) {
return [...arr1, ...arr2];
},
//returns the frequency of elements in an array
freq: function(arr) {
const frequency = {};
for (const value of arr) {
frequency[value] = (frequency[value] || 0) + 1;
}
return frequency;
},
},
//DOM functions
DOM: {
//selects an element
select: function(selector) {
const elem = document.querySelector(selector);
if (!elem) {
throw new Error(`Element not found: ${selector}`);
}
return elem;
},
//selects an element by id
selectid: function(id) {
const elem = document.getElementById(id);
if (!elem) {
throw new Error(`Element not found: ${id}`);
}
return elem;
},
//selects all elements with matching selector
selectall: function(element) {
const elem = document.querySelectorAll(element);
if (!elem.length) {
throw new Error(`Element not found: ${element}`);
}
return elem;
},
//attaches an event listener to an element
addListener:function(element,event,fn){
return element.addEventListener(event,fn)
},
//remove an event listener
removeListener: function(element, event, fn) {
return element.removeEventListener(event, fn)
},
},
//functional programming utils
FUNC: {
//throttles a function
throttle: function(func,wait) {
let timeout;
return function() {
const context = this;
const args = arguments;
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args);
}, wait);
}
};
},
//debounces a function
debounce: function(func, wait) {
let timeout;
return function() {
const context = this;
const args = arguments;
clearTimeout(timeout);
timeout = setTimeout(() => {
func.apply(context, args);
}, wait);
};
},
//memoize a function
memoize: function(func) {
const cache = new Map();
return function(...args) {
const key = JSON.stringify(args);
if (cache.has(key)) {
return cache.get(key);
}
const result = func(...args);
cache.set(key, result);
return result;
};
},
//curries a function
curry: function(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn(...args);
}
return function(...nextArgs) {
return curried(...args.concat(nextArgs));
};
};
},
//a function will be called only once
once:function(func){
let called = false;
return function(...args){
if (!called) {
let called = true;
return func(...args);
}
}
}
},
//string utils
STR: {
//removes whitespaces
strip: function(str) {
return str.replace(/\s/g, '');
},
//checks if a substring is present in a string
contains: function(str, search, position = 0) {
return str.indexOf(search, position) !== -1;
},
//checks if a string is blank
isBlank: function(str) {
return SWOOP.STR.strip(str) === "";
},
//checks if a string is not blank
isNotBlank: function(str) {
return SWOOP.STR.strip(str)!== "";
},
//checks if a string contains only contains alphabetic characters
isAlphabet: function(str) {
return /^\D+$/.test(str);
},
//checks if a string contains only contains numeric characters
isNumeric: function(str) {
return /^\d+$/.test(str);
},
//repeats a function
echo: function(str, n) {
for (var i = 0; i < n; i++) {
console.log(str.toString());
}
},
//capitalises the first letter of a string
capitalise: function(str){
return replace(/^./,match=>match.toUpperCase())
},
},
MATHS: {
//calculates sine of an angle
sin:(angle)=>Math.sin(angle),
//calculates cosine of an angle
cos:(angle)=>Math.cos(angle),
//calculates tangent of an angle
tan:(angle)=>Math.tan(angle),
//distance of a line
distance:(x1,x2,y1,y2)=>Math.sqrt((x2-x1)**2+(y2+y1)**2),
//calculates midpoint
midpoint:(x1,x2,y1,y2)=>[(x1+x2)/2,(y1+y2)/2],
//calculates linear interpolation
lerp:(a,b,t)=>a+(a-b)*t,
//clamps a number
clamp:(value,min,max)=>Math.min(Math.max(value,min),max),
//returns the sign of a number
signum:(value)=>value===0?0:(value>=0?1:-1),
//finds the square root of a number
sqrt:(value)=>Math.sqrt(value),
//returns the rounded value a number
round:(num)=>Math.round(num),
//ceils a number
ceil:(num)=>Math.ceil(num),
//returns the absolute value of a number
absolute:num=>Math.abs(num),
//calculates the Greatest Common Divisor of 2 numbers
gcd:(a,b)=>b===0?a:SWOOP.MATHS.gcd(b,a%b),
//calculates the Least Common Multiple of 2 numbers
lcm:(a,b)=>SWOOP.MATHS.absolute(a*b)/SWOOP.MATHS.gcd(a,b),
//calculates the hypotenuse when perpendicular and base is given
hypot:(p,b)=>Math.sqrt(p*p+b*b),
//generates a random number
randnum:(min,max)=>Math.floor(Math.random()*(max-min+1)+min)
}
};