-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
559 lines (521 loc) · 12.6 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/**
* To throw error in case of invalid datatype.
*
* @returns TypeError
*/
function _throw() {
throw new TypeError("Must be a valid array!");
}
/**
* To check if arr is array.
*
* @param array arr
* @returns bool | TypeError
*/
function is_array(arr) {
return Array.isArray(arr) ? true : _throw();
}
/**
* To check if array of numbers.
*
* @param array arr
* @returns bool | TypeError
*/
function is_num_array(arr) {
var a = arr.reduce(function (result, val) {
return result && typeof val === "number";
}, true);
if (a == false) {
throw new TypeError("Must be an array of numbers!");
}
}
/**
* To get the head or first element of the array.
*
* @param array arr
* @returns any
*/
function head(arr) {
is_array(arr);
return arr[0];
}
/**
* To get the tail last element of the array.
*
* @param array arr
* @returns any
*/
function tail(arr) {
is_array(arr);
let element = arr.pop();
arr.push(element);
return element;
}
/**
* To check the existence of an element inside the array.
*
* @param array arr
* @param any value
* @returns any
*/
function in_array(arr, value) {
is_array(arr);
return arr.includes(value);
}
/**
* To split arrays into fixed size chunks.
*
* @param array arr
* @param number chunk_size
* @returns any
*/
function array_chunk(arr, chunk_size) {
is_array(arr);
if (typeof chunk_size != "number") {
throw new TypeError("chunk_size should be a integer!");
}
let length = arr.length;
chunk_size = Math.abs(Math.round(chunk_size));
if (chunk_size > length || [0, null, NaN].includes(chunk_size)) {
return [arr];
} else {
let modified_array = [];
for (let index = 0; index < length; index += chunk_size) {
modified_array.push(arr.slice(index, index + chunk_size));
}
arr = modified_array;
return arr;
}
}
/**
* To filter out arrays by removing nullish values.
*
* @param array arr
* @returns array
*/
function array_filter(arr) {
is_array(arr);
arr = arr.filter((e) => {
return e === 0 || e;
});
return arr;
}
/**
* To get the frequency of occurence of each unique element inside the array.
*
* @param array arr
* @returns array
*/
function array_frequency(arr) {
is_array(arr);
let freq_obj = {};
arr.forEach((value) => {
if (value in freq_obj) {
freq_obj[value] += 1;
} else {
freq_obj[value] = 1;
}
});
return freq_obj;
}
/**
* To convert Objects into Arrays.
*
* @param object obj
* @returns array
*/
function object_to_array(obj) {
let temp = [];
const entries = Object.entries(obj);
entries.forEach((ent) => temp.push(ent[1]));
return temp;
}
/**
* To get indexes of all occurences of an element inside an array.
*
* @param array arr
* @param any val
* @returns array
*/
function get_all_indexes(arr, val) {
is_array(arr);
var indexes = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === val) {
indexes.push(i);
}
}
return indexes;
}
/**
* To check if a substr exists within an array of strings
*
* @param string query
* @param array arr
* @returns array
*/
function search_in_array(query, arr) {
is_array(arr);
return arr.filter((item) => item.toLowerCase().search(query.toLowerCase()) !== -1);
}
/**
* Get sum of array
*
* @param array arr
* @returns number
*/
function array_sum(arr) {
is_array(arr);
return arr.reduce((prev, curr) => (isNaN(curr) ? 0 : prev + curr), 0);
}
/**
* Get sum of a subarray
*
* @param array arr
* @param number slice_start
* @param number slice_end
* @param bool includes
* @returns number | TypeError
*/
function array_slice_sum(arr, slice_start = 0, slice_end = arr.length, includes = true) {
is_array(arr);
if (
Number.isInteger(slice_start) &&
Number.isInteger(slice_end) &&
typeof includes === "boolean"
) {
if (includes) {
return array_sum(arr.slice(slice_start, slice_end + 1));
} else {
return array_sum(arr.slice(slice_start, slice_end));
}
} else {
throw new TypeError("Input parameters not valid!");
}
}
/**
* To sort numeric arrays ascending and descending
*
* @param array arr
* @param bool reverse
* @returns array
*/
function sort_nums(arr, reverse = false) {
is_array(arr);
is_num_array(arr);
if (reverse) {
return arr.sort(function (a, b) {
return b - a;
});
} else {
return arr.sort(function (a, b) {
return a - b;
});
}
}
/**
* To get all the elements of an array with given key and value.
* More like Where clause in php just for js array
*
*
* @param array arr
* @param string key
* @param string value
* @param number value
* @returns array
*/
function get_element_by_key_value(arr, key, value) {
is_array(arr);
return arr.filter((item) => item[key] == value);
}
/**
* Flatten a nested array (the nesting can be to any depth).
*
* @param array arr
* @returns array
*/
function flatten(arr) {
is_array(arr);
return arr.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);
}
/**
* Gets the intersection between two arrays (i.e. the elements that are present in both arrays).
*
* @param array arr1
* @param array arr2
* @returns array
*/
function intersection(arr1, arr2) {
is_array(arr1);
is_array(arr2);
return arr1.filter((item) => arr2.includes(item));
}
/**
* Gets the difference between two arrays (i.e. the elements that are present in the first array, but not in the second).
* @param array arr1
* @param array arr2
* @returns array
*/
function difference(arr1, arr2) {
is_array(arr1);
is_array(arr2);
return arr1.filter((item) => !arr2.includes(item));
}
/**
* To sanitize the array of objects with respect to the given schema
*
* See exmaple in Readme for better understanding....
*
* @param array arr
* @param object schema { "keyName": "dataType" }
* @return array
*
*/
function sanitize_array(arr, schema) {
let all_data_types = [];
let all_keys = [];
const schema_array = Object.entries(schema);
schema_array.forEach((type) => all_data_types.push(type[1]));
schema_array.forEach((key) => all_keys.push(key[0]));
arr.forEach((element) => {
all_keys.forEach((key, key_index) => {
if (`${typeof element[all_keys[key_index]]}` !== `${all_data_types[key_index]}`) {
if (all_data_types[key_index] == "number") {
if (!isNaN(parseInt(element[all_keys[key_index]]))) {
element[all_keys[key_index]] = parseInt(element[all_keys[key_index]]);
} else {
element[all_keys[key_index]] = 0;
}
} else if (all_data_types[key_index] == "string") {
element[all_keys[key_index]] = `${element[all_keys[key_index]]}`;
} else if (all_data_types[key_index] == "boolean") {
if (element[all_keys[key_index]] == "true") {
element[all_keys[key_index]] = true;
} else if (element[all_keys[key_index]] == "false") {
element[all_keys[key_index]] = false;
} else if (element[all_keys[key_index]] == null) {
element[all_keys[key_index]] = false;
} else {
element[all_keys[key_index]] = true;
}
} else {
element[all_keys[key_index]] = null;
}
}
});
});
return arr;
}
/**
* Get only selected keys of array
* Like select query
*
* @param array arr
* @param array keys
* @returns array
*/
function get_only(arr, keys) {
is_array(arr);
is_array(keys);
return arr.map((a) =>
keys.reduce((b, key) => {
b[key] = a[key];
return b;
}, {})
);
}
/**
* To reverse an array in parts like {1,2,3,4} if we want to reverse just 3,4 then we can use this function
*
* @param array arr
* @param number start
* @param number end
* @returns array
*/
function array_reverse_part(arr, start, end) {
//is_array(arr);
let temp;
let arr1 = [...arr];
while (start < end) {
//Swapping values at start index and end index.
temp = arr1[start];
arr1[start] = arr1[end];
arr1[end] = temp;
//Updating the values of start and end.
start++;
end--;
}
return arr1;
}
/**
* To get RMS (Root Mean Square) Value.
*
* @param array arr
* @returns number
*/
function get_rms_value(arr) {
is_array(arr);
let sum_square = arr.reduce((ss, curr) => (isNaN(curr) ? 0 : ss + curr * curr), 0);
return Math.sqrt(sum_square / arr.length);
}
/**
* To rotate an array to the left counter-clockwise by x steps, where x is non-negative
*
* @param array arr
* @param number x
* @returns array
*/
function array_rotate(arr, x) {
//is_array(arr);
let arr1 = [...arr];
let n = arr.length;
x = x % n;
//First reversing d elements from starting index.
arr1 = array_reverse_part(arr1, 0, x - 1);
//Then reversing the last n-d elements.
arr1 = array_reverse_part(arr1, x, n - 1);
//Finally, reversing the whole array.
arr1 = array_reverse_part(arr1, 0, n - 1);
return arr1;
}
/**
* Get the equilibrium point of an array which means the array element position where the
* sum of elements on left side and sum of element on right side of that element in array are same.
*
*
* @param array a
* @param number n
* @returns number
*/
function equilibrium_Point(a, n) {
is_array(a);
let i = 0;
let j = n - 1;
let s1 = 0;
let s2 = 0;
while (i < j) {
if (s1 <= s2) {
s1 = s1 + a[i];
i++;
} else {
s2 = s2 + a[j];
j--;
}
}
if (s1 == s2) {
return i + 1;
} else {
return -1;
}
}
/**
* To find and upadate a key value in an array of objects
*
* @param array arr
* @param string key (key to be updated)
* @param string value (old value of the key )
* @param string newValue (new value of the key)
* @returns arr
*/
function find_and_update(arr, key, value, new_value) {
arr.forEach((element) => {
if (element[key] == value) {
element[key] = new_value;
}
});
return arr;
}
/**
* To find and update a key value in an array of
* objects with respect to a parent key value.
*
* @param array arr
* @param string parentKey (parent key to select the object)
* @param string parentValue (parent value to select the object)
* @param string targetKey (key to be updated)
* @param string targetValue (old value of the key to select only desired object)
* @param string targetValue | "*" to update all the targetKey
* @param string newValue (new value of the key)
* @returns arr
*/
function find_key_and_update(arr, parent_key, parent_value, target_key, target_value, new_value) {
arr.forEach((element) => {
if (element[parent_key] == parent_value) {
if (target_value == "*") {
element[target_key] = new_value;
} else if (element[target_key] == target_value) {
element[target_key] = new_value;
}
}
});
return arr;
}
/**
* For in range loop like python
*
* @param integer num
* @param function callback
*/
function for_in_range(num, callback) {
for (let i = 0; i < num; i++) {
callback(i);
}
}
/**
* Group by key in array of object
*
* @param array arr
* @param string key
* @returns object
*/
function group_by(arr, key) {
is_array(arr);
return arr.reduce(function (rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
}
/**
* Get a random value from the array
*
* @param array arr
* @returns any | TypeError
*/
function random_value(arr) {
is_array(arr);
if (arr.length == 0) {
return null;
}
return arr[Math.floor(Math.random() * arr.length)];
}
export {
is_array,
is_num_array,
head,
tail,
in_array,
array_chunk,
array_filter,
array_frequency,
object_to_array,
get_all_indexes,
search_in_array,
array_sum,
array_slice_sum,
sort_nums,
get_element_by_key_value,
flatten,
intersection,
difference,
sanitize_array,
get_only,
array_reverse_part,
array_rotate,
equilibrium_Point,
get_rms_value,
find_key_and_update,
find_and_update,
for_in_range,
group_by,
random_value,
};