|
1 |
| -// To throw error in case of invalid datatype. |
2 |
| -function _throw() { |
3 |
| - throw new TypeError("Must be a valid array!"); |
4 |
| -} |
5 |
| - |
6 |
| -// To check if arr is array. |
7 |
| -function is_array(arr) { |
8 |
| - return Array.isArray(arr) ? true : _throw(); |
9 |
| -} |
| 1 | +class Helper { |
| 2 | + // To throw error in case of invalid datatype. |
| 3 | + static _throw() { |
| 4 | + throw new TypeError("Must be a valid array!"); |
| 5 | + } |
10 | 6 |
|
11 |
| -// To get the head or first element of the array. |
12 |
| -function head(arr) { |
13 |
| - is_array(arr); |
14 |
| - return arr[0]; |
15 |
| -} |
| 7 | + // To check if arr is array. |
| 8 | + static is_array(arr) { |
| 9 | + return Array.isArray(arr) ? true : this._throw(); |
| 10 | + } |
16 | 11 |
|
17 |
| -// To get the tail last element of the array. |
18 |
| -function tail(arr) { |
19 |
| - is_array(arr); |
20 |
| - let element = arr.pop(); |
21 |
| - arr.push(element); |
22 |
| - return element; |
23 |
| -} |
| 12 | + // To get the head or first element of the array. |
| 13 | + static head(arr) { |
| 14 | + this.is_array(arr); |
| 15 | + return arr[0]; |
| 16 | + } |
24 | 17 |
|
25 |
| -// To check the existence of an element inside the array. |
26 |
| -function in_array(arr, value) { |
27 |
| - is_array(arr); |
28 |
| - return arr.includes(value); |
29 |
| -} |
| 18 | + // To get the tail last element of the array. |
| 19 | + static tail(arr) { |
| 20 | + this.is_array(arr); |
| 21 | + let element = arr.pop(); |
| 22 | + arr.push(element); |
| 23 | + return element; |
| 24 | + } |
30 | 25 |
|
31 |
| -// To split arrays into fixed size chunks. |
32 |
| -function array_chunk(arr, chunk_size) { |
33 |
| - is_array(arr); |
34 |
| - if (typeof chunk_size != "number") { |
35 |
| - throw new TypeError("chunk_size should be a integer!"); |
| 26 | + // To check the existence of an element inside the array. |
| 27 | + static in_array(arr, value) { |
| 28 | + this.is_array(arr); |
| 29 | + return arr.includes(value); |
36 | 30 | }
|
37 | 31 |
|
38 |
| - let length = arr.length; |
39 |
| - chunk_size = Math.abs(Math.round(chunk_size)); |
| 32 | + // To split arrays into fixed size chunks. |
| 33 | + static array_chunk(arr, chunk_size) { |
| 34 | + this.is_array(arr); |
| 35 | + if (typeof chunk_size != "number") { |
| 36 | + throw new TypeError("chunk_size should be a integer!"); |
| 37 | + } |
40 | 38 |
|
41 |
| - if (chunk_size > length || [0, null, NaN].includes(chunk_size)) { |
42 |
| - return arr; |
43 |
| - } else { |
44 |
| - let modified_array = []; |
45 |
| - for (let index = 0; index < length; index += chunk_size) { |
46 |
| - modified_array.push(arr.slice(index, index + chunk_size)); |
| 39 | + let length = arr.length; |
| 40 | + chunk_size = Math.abs(Math.round(chunk_size)); |
| 41 | + |
| 42 | + if (chunk_size > length || [0, null, NaN].includes(chunk_size)) { |
| 43 | + return arr; |
| 44 | + } else { |
| 45 | + let modified_array = []; |
| 46 | + for (let index = 0; index < length; index += chunk_size) { |
| 47 | + modified_array.push(arr.slice(index, index + chunk_size)); |
| 48 | + } |
| 49 | + arr = modified_array; |
| 50 | + return arr; |
47 | 51 | }
|
48 |
| - arr = modified_array; |
| 52 | + } |
| 53 | + |
| 54 | + // To filter out arrays by removing nullish values. |
| 55 | + static array_filter(arr) { |
| 56 | + this.is_array(arr); |
| 57 | + arr = arr.filter((e) => { |
| 58 | + return e === 0 || e; |
| 59 | + }); |
49 | 60 | return arr;
|
50 | 61 | }
|
51 |
| -} |
52 | 62 |
|
53 |
| -// To filter out arrays based on datatypes. |
54 |
| -function array_filter(arr) { |
55 |
| - is_array(arr); |
56 |
| - arr = arr.filter((e) => { |
57 |
| - return e === 0 || e; |
58 |
| - }); |
59 |
| - return arr; |
60 |
| -} |
| 63 | + // To get the frequency of occurence of each unique element inside the array. |
| 64 | + static array_frequency(arr) { |
| 65 | + this.is_array(arr); |
| 66 | + let freq_obj = {}; |
| 67 | + arr.forEach((value) => { |
| 68 | + if (value in freq_obj) { |
| 69 | + freq_obj[value] += 1; |
| 70 | + } else { |
| 71 | + freq_obj[value] = 1; |
| 72 | + } |
| 73 | + }); |
| 74 | + return freq_obj; |
| 75 | + } |
61 | 76 |
|
62 |
| -// To get the frequency of occurence of each unique element inside the array. |
63 |
| -function array_frequency(arr) { |
64 |
| - is_array(arr); |
65 |
| - let freq_obj = {}; |
66 |
| - arr.forEach((value) => { |
67 |
| - if (value in freq_obj) { |
68 |
| - freq_obj[value] += 1; |
69 |
| - } else { |
70 |
| - freq_obj[value] = 1; |
| 77 | + // To convert Objects into Arrays. |
| 78 | + static object_to_array(obj) { |
| 79 | + let temp = []; |
| 80 | + const entries = Object.entries(obj); |
| 81 | + entries.forEach((ent) => temp.push(ent[1])); |
| 82 | + return temp; |
| 83 | + } |
| 84 | + |
| 85 | + // To get indexes of all occurences of an element inside an array. |
| 86 | + static get_all_indexes(arr, val) { |
| 87 | + this.is_array(arr); |
| 88 | + var indexes = []; |
| 89 | + for (let i = 0; i < arr.length; i++) { |
| 90 | + if (arr[i] === val) { |
| 91 | + indexes.push(i); |
| 92 | + } |
71 | 93 | }
|
72 |
| - }); |
73 |
| - return freq_obj; |
74 |
| -} |
| 94 | + return indexes; |
| 95 | + } |
75 | 96 |
|
76 |
| -// To convert Objects into Arrays. |
77 |
| -function object_to_array(obj) { |
78 |
| - let temp = []; |
79 |
| - const entries = Object.entries(obj); |
80 |
| - entries.forEach((ent) => temp.push(ent[1])); |
81 |
| - return temp; |
82 |
| -} |
| 97 | + // To check if a substr exists within an array of strings |
| 98 | + static search_in_array(query, arr) { |
| 99 | + this.is_array(arr); |
| 100 | + return arr.filter((item) => item.toLowerCase().search(query.toLowerCase()) !== -1); |
| 101 | + } |
83 | 102 |
|
84 |
| -// To get indexes of all occurences of an element inside an array. |
85 |
| -function get_all_indexes(arr, val) { |
86 |
| - is_array(arr); |
87 |
| - var indexes = []; |
88 |
| - for (let i = 0; i < arr.length; i++) { |
89 |
| - if (arr[i] === val) { |
90 |
| - indexes.push(i); |
91 |
| - } |
| 103 | + // Get sum of array |
| 104 | + static array_sum(arr) { |
| 105 | + this.is_array(arr); |
| 106 | + return arr.reduce((prev, curr) => (isNaN(curr) ? 0 : prev + curr), 0); |
92 | 107 | }
|
93 |
| - return indexes; |
94 |
| -} |
95 | 108 |
|
96 |
| -function search_in_array(query,array) { |
97 |
| - return array.filter(item => item.toLowerCase().search(query.toLowerCase()) !== -1) |
| 109 | + // Get sum of a subarray |
| 110 | + static array_slice_sum(arr, slice_start = 0, slice_end = arr.length, includes = true) { |
| 111 | + this.is_array(arr); |
| 112 | + if ( |
| 113 | + Number.isInteger(slice_start) && |
| 114 | + Number.isInteger(slice_end) && |
| 115 | + typeof includes === "boolean" |
| 116 | + ) { |
| 117 | + if (includes) { |
| 118 | + return this.array_sum(arr.slice(slice_start, slice_end + 1)); |
| 119 | + } else { |
| 120 | + return this.array_sum(arr.slice(slice_start, slice_end)); |
| 121 | + } |
| 122 | + } else { |
| 123 | + throw new TypeError("Input parameters not valid!"); |
| 124 | + } |
| 125 | + } |
98 | 126 | }
|
99 | 127 |
|
100 |
| -export { |
101 |
| - head, |
102 |
| - tail, |
103 |
| - is_array, |
104 |
| - in_array, |
105 |
| - array_chunk, |
106 |
| - array_filter, |
107 |
| - array_frequency, |
108 |
| - object_to_array, |
109 |
| - get_all_indexes, |
110 |
| - search_in_array, |
111 |
| -}; |
| 128 | +export default Helper; |
0 commit comments