|
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 | | - } |
| 1 | +/** |
| 2 | + * To throw error in case of invalid datatype. |
| 3 | + * |
| 4 | + * @returns TypeError |
| 5 | + */ |
| 6 | +function _throw() { |
| 7 | + throw new TypeError("Must be a valid array!"); |
| 8 | +} |
6 | 9 |
|
7 | | - // To check if arr is array. |
8 | | - static is_array(arr) { |
9 | | - return Array.isArray(arr) ? true : this._throw(); |
10 | | - } |
| 10 | +/** |
| 11 | + * To check if arr is array. |
| 12 | + * |
| 13 | + * @param array arr |
| 14 | + * @returns bool | TypeError |
| 15 | + */ |
| 16 | +function is_array(arr) { |
| 17 | + return Array.isArray(arr) ? true : _throw(); |
| 18 | +} |
11 | 19 |
|
12 | | - // To check if arr has only nums. |
13 | | - static is_num_array(arr) { |
14 | | - var a = arr.reduce(function(result, val) { |
15 | | - return result && typeof val === 'number'; |
16 | | - }, true); |
17 | | - if (a == false) { |
18 | | - throw new TypeError("Must be an array of numbers!") |
19 | | - } |
| 20 | +// To check if arr has only nums. |
| 21 | +function is_num_array(arr) { |
| 22 | + var a = arr.reduce(function (result, val) { |
| 23 | + return result && typeof val === "number"; |
| 24 | + }, true); |
| 25 | + if (a == false) { |
| 26 | + throw new TypeError("Must be an array of numbers!"); |
20 | 27 | } |
| 28 | +} |
21 | 29 |
|
22 | | - // To get the head or first element of the array. |
23 | | - static head(arr) { |
24 | | - this.is_array(arr); |
25 | | - return arr[0]; |
26 | | - } |
| 30 | +// To get the head or first element of the array. |
| 31 | +function head(arr) { |
| 32 | + is_array(arr); |
| 33 | + return arr[0]; |
| 34 | +} |
27 | 35 |
|
28 | | - // To get the tail last element of the array. |
29 | | - static tail(arr) { |
30 | | - this.is_array(arr); |
31 | | - let element = arr.pop(); |
32 | | - arr.push(element); |
33 | | - return element; |
34 | | - } |
| 36 | +// To get the tail last element of the array. |
| 37 | +function tail(arr) { |
| 38 | + is_array(arr); |
| 39 | + let element = arr.pop(); |
| 40 | + arr.push(element); |
| 41 | + return element; |
| 42 | +} |
35 | 43 |
|
36 | | - // To check the existence of an element inside the array. |
37 | | - static in_array(arr, value) { |
38 | | - this.is_array(arr); |
39 | | - return arr.includes(value); |
40 | | - } |
| 44 | +// To check the existence of an element inside the array. |
| 45 | +function in_array(arr, value) { |
| 46 | + is_array(arr); |
| 47 | + return arr.includes(value); |
| 48 | +} |
41 | 49 |
|
42 | | - // To split arrays into fixed size chunks. |
43 | | - static array_chunk(arr, chunk_size) { |
44 | | - this.is_array(arr); |
45 | | - if (typeof chunk_size != "number") { |
46 | | - throw new TypeError("chunk_size should be a integer!"); |
47 | | - } |
| 50 | +// To split arrays into fixed size chunks. |
| 51 | +function array_chunk(arr, chunk_size) { |
| 52 | + is_array(arr); |
| 53 | + if (typeof chunk_size != "number") { |
| 54 | + throw new TypeError("chunk_size should be a integer!"); |
| 55 | + } |
48 | 56 |
|
49 | | - let length = arr.length; |
50 | | - chunk_size = Math.abs(Math.round(chunk_size)); |
| 57 | + let length = arr.length; |
| 58 | + chunk_size = Math.abs(Math.round(chunk_size)); |
51 | 59 |
|
52 | | - if (chunk_size > length || [0, null, NaN].includes(chunk_size)) { |
53 | | - return arr; |
54 | | - } else { |
55 | | - let modified_array = []; |
56 | | - for (let index = 0; index < length; index += chunk_size) { |
57 | | - modified_array.push(arr.slice(index, index + chunk_size)); |
58 | | - } |
59 | | - arr = modified_array; |
60 | | - return arr; |
| 60 | + if (chunk_size > length || [0, null, NaN].includes(chunk_size)) { |
| 61 | + return arr; |
| 62 | + } else { |
| 63 | + let modified_array = []; |
| 64 | + for (let index = 0; index < length; index += chunk_size) { |
| 65 | + modified_array.push(arr.slice(index, index + chunk_size)); |
61 | 66 | } |
62 | | - } |
63 | | - |
64 | | - // To filter out arrays by removing nullish values. |
65 | | - static array_filter(arr) { |
66 | | - this.is_array(arr); |
67 | | - arr = arr.filter((e) => { |
68 | | - return e === 0 || e; |
69 | | - }); |
| 67 | + arr = modified_array; |
70 | 68 | return arr; |
71 | 69 | } |
| 70 | +} |
72 | 71 |
|
73 | | - // To get the frequency of occurence of each unique element inside the array. |
74 | | - static array_frequency(arr) { |
75 | | - this.is_array(arr); |
76 | | - let freq_obj = {}; |
77 | | - arr.forEach((value) => { |
78 | | - if (value in freq_obj) { |
79 | | - freq_obj[value] += 1; |
80 | | - } else { |
81 | | - freq_obj[value] = 1; |
82 | | - } |
83 | | - }); |
84 | | - return freq_obj; |
85 | | - } |
| 72 | +// To filter out arrays by removing nullish values. |
| 73 | +function array_filter(arr) { |
| 74 | + is_array(arr); |
| 75 | + arr = arr.filter((e) => { |
| 76 | + return e === 0 || e; |
| 77 | + }); |
| 78 | + return arr; |
| 79 | +} |
86 | 80 |
|
87 | | - // To convert Objects into Arrays. |
88 | | - static object_to_array(obj) { |
89 | | - let temp = []; |
90 | | - const entries = Object.entries(obj); |
91 | | - entries.forEach((ent) => temp.push(ent[1])); |
92 | | - return temp; |
93 | | - } |
| 81 | +// To get the frequency of occurence of each unique element inside the array. |
| 82 | +function array_frequency(arr) { |
| 83 | + is_array(arr); |
| 84 | + let freq_obj = {}; |
| 85 | + arr.forEach((value) => { |
| 86 | + if (value in freq_obj) { |
| 87 | + freq_obj[value] += 1; |
| 88 | + } else { |
| 89 | + freq_obj[value] = 1; |
| 90 | + } |
| 91 | + }); |
| 92 | + return freq_obj; |
| 93 | +} |
94 | 94 |
|
95 | | - // To get indexes of all occurences of an element inside an array. |
96 | | - static get_all_indexes(arr, val) { |
97 | | - this.is_array(arr); |
98 | | - var indexes = []; |
99 | | - for (let i = 0; i < arr.length; i++) { |
100 | | - if (arr[i] === val) { |
101 | | - indexes.push(i); |
102 | | - } |
| 95 | +// To convert Objects into Arrays. |
| 96 | +function object_to_array(obj) { |
| 97 | + let temp = []; |
| 98 | + const entries = Object.entries(obj); |
| 99 | + entries.forEach((ent) => temp.push(ent[1])); |
| 100 | + return temp; |
| 101 | +} |
| 102 | + |
| 103 | +// To get indexes of all occurences of an element inside an array. |
| 104 | +function get_all_indexes(arr, val) { |
| 105 | + is_array(arr); |
| 106 | + var indexes = []; |
| 107 | + for (let i = 0; i < arr.length; i++) { |
| 108 | + if (arr[i] === val) { |
| 109 | + indexes.push(i); |
103 | 110 | } |
104 | | - return indexes; |
105 | 111 | } |
| 112 | + return indexes; |
| 113 | +} |
106 | 114 |
|
107 | | - // To check if a substr exists within an array of strings |
108 | | - static search_in_array(query, arr) { |
109 | | - this.is_array(arr); |
110 | | - return arr.filter((item) => item.toLowerCase().search(query.toLowerCase()) !== -1); |
111 | | - } |
| 115 | +// To check if a substr exists within an array of strings |
| 116 | +function search_in_array(query, arr) { |
| 117 | + is_array(arr); |
| 118 | + return arr.filter((item) => item.toLowerCase().search(query.toLowerCase()) !== -1); |
| 119 | +} |
112 | 120 |
|
113 | | - // Get sum of array |
114 | | - static array_sum(arr) { |
115 | | - this.is_array(arr); |
116 | | - return arr.reduce((prev, curr) => (isNaN(curr) ? 0 : prev + curr), 0); |
117 | | - } |
| 121 | +// Get sum of array |
| 122 | +function array_sum(arr) { |
| 123 | + is_array(arr); |
| 124 | + return arr.reduce((prev, curr) => (isNaN(curr) ? 0 : prev + curr), 0); |
| 125 | +} |
118 | 126 |
|
119 | | - // Get sum of a subarray |
120 | | - static array_slice_sum(arr, slice_start = 0, slice_end = arr.length, includes = true) { |
121 | | - this.is_array(arr); |
122 | | - if ( |
123 | | - Number.isInteger(slice_start) && |
124 | | - Number.isInteger(slice_end) && |
125 | | - typeof includes === "boolean" |
126 | | - ) { |
127 | | - if (includes) { |
128 | | - return this.array_sum(arr.slice(slice_start, slice_end + 1)); |
129 | | - } else { |
130 | | - return this.array_sum(arr.slice(slice_start, slice_end)); |
131 | | - } |
| 127 | +// Get sum of a subarray |
| 128 | +function array_slice_sum(arr, slice_start = 0, slice_end = arr.length, includes = true) { |
| 129 | + is_array(arr); |
| 130 | + if ( |
| 131 | + Number.isInteger(slice_start) && |
| 132 | + Number.isInteger(slice_end) && |
| 133 | + typeof includes === "boolean" |
| 134 | + ) { |
| 135 | + if (includes) { |
| 136 | + return array_sum(arr.slice(slice_start, slice_end + 1)); |
132 | 137 | } else { |
133 | | - throw new TypeError("Input parameters not valid!"); |
| 138 | + return array_sum(arr.slice(slice_start, slice_end)); |
134 | 139 | } |
| 140 | + } else { |
| 141 | + throw new TypeError("Input parameters not valid!"); |
135 | 142 | } |
136 | | - //To sort numeric arrays ascending and descending |
137 | | - static sort_nums(arr, reverse = false) { |
138 | | - this.is_array(arr); |
139 | | - this.is_num_array(arr); |
140 | | - if (reverse) { |
141 | | - return arr.sort(function(a, b){return b - a}); |
142 | | - } else { |
143 | | - return arr.sort(function(a, b){return a - b}); |
144 | | - } |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * To sort numeric arrays ascending and descending |
| 147 | + * |
| 148 | + * @param array arr |
| 149 | + * @param bool reverse |
| 150 | + * @returns array |
| 151 | + */ |
| 152 | +function sort_nums(arr, reverse = false) { |
| 153 | + is_array(arr); |
| 154 | + is_num_array(arr); |
| 155 | + if (reverse) { |
| 156 | + return arr.sort(function (a, b) { |
| 157 | + return b - a; |
| 158 | + }); |
| 159 | + } else { |
| 160 | + return arr.sort(function (a, b) { |
| 161 | + return a - b; |
| 162 | + }); |
145 | 163 | } |
146 | 164 | } |
147 | 165 |
|
148 | | -export default Helper; |
| 166 | +export { |
| 167 | + is_array, |
| 168 | + is_num_array, |
| 169 | + head, |
| 170 | + tail, |
| 171 | + in_array, |
| 172 | + array_chunk, |
| 173 | + array_filter, |
| 174 | + array_frequency, |
| 175 | + object_to_array, |
| 176 | + get_all_indexes, |
| 177 | + search_in_array, |
| 178 | + array_sum, |
| 179 | + array_slice_sum, |
| 180 | + sort_nums, |
| 181 | +}; |
0 commit comments