Skip to content

Commit 736cadb

Browse files
committed
Flatten, Intersection, and Difference functions
1 parent 0353d83 commit 736cadb

File tree

1 file changed

+149
-102
lines changed

1 file changed

+149
-102
lines changed

index.js

Lines changed: 149 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @returns TypeError
55
*/
66
function _throw() {
7-
throw new TypeError("Must be a valid array!");
7+
throw new TypeError("Must be a valid array!");
88
}
99

1010
/**
@@ -14,132 +14,139 @@ function _throw() {
1414
* @returns bool | TypeError
1515
*/
1616
function is_array(arr) {
17-
return Array.isArray(arr) ? true : _throw();
17+
return Array.isArray(arr) ? true : _throw();
1818
}
1919

2020
// To check if arr has only nums.
2121
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!");
27-
}
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!");
27+
}
2828
}
2929

3030
// To get the head or first element of the array.
3131
function head(arr) {
32-
is_array(arr);
33-
return arr[0];
32+
is_array(arr);
33+
return arr[0];
3434
}
3535

3636
// To get the tail last element of the array.
3737
function tail(arr) {
38-
is_array(arr);
39-
let element = arr.pop();
40-
arr.push(element);
41-
return element;
38+
is_array(arr);
39+
let element = arr.pop();
40+
arr.push(element);
41+
return element;
4242
}
4343

4444
// To check the existence of an element inside the array.
4545
function in_array(arr, value) {
46-
is_array(arr);
47-
return arr.includes(value);
46+
is_array(arr);
47+
return arr.includes(value);
4848
}
4949

5050
// To split arrays into fixed size chunks.
5151
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-
}
52+
is_array(arr);
53+
if (typeof chunk_size != "number") {
54+
throw new TypeError("chunk_size should be a integer!");
55+
}
5656

57-
let length = arr.length;
58-
chunk_size = Math.abs(Math.round(chunk_size));
57+
let length = arr.length;
58+
chunk_size = Math.abs(Math.round(chunk_size));
5959

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));
66-
}
67-
arr = modified_array;
68-
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));
6966
}
67+
arr = modified_array;
68+
return arr;
69+
}
7070
}
7171

7272
// To filter out arrays by removing nullish values.
7373
function array_filter(arr) {
74-
is_array(arr);
75-
arr = arr.filter((e) => {
76-
return e === 0 || e;
77-
});
78-
return arr;
74+
is_array(arr);
75+
arr = arr.filter((e) => {
76+
return e === 0 || e;
77+
});
78+
return arr;
7979
}
8080

8181
// To get the frequency of occurence of each unique element inside the array.
8282
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;
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;
9393
}
9494

9595
// To convert Objects into Arrays.
9696
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;
97+
let temp = [];
98+
const entries = Object.entries(obj);
99+
entries.forEach((ent) => temp.push(ent[1]));
100+
return temp;
101101
}
102102

103103
// To get indexes of all occurences of an element inside an array.
104104
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);
110-
}
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);
111110
}
112-
return indexes;
111+
}
112+
return indexes;
113113
}
114114

115115
// To check if a substr exists within an array of strings
116116
function search_in_array(query, arr) {
117-
is_array(arr);
118-
return arr.filter((item) => item.toLowerCase().search(query.toLowerCase()) !== -1);
117+
is_array(arr);
118+
return arr.filter(
119+
(item) => item.toLowerCase().search(query.toLowerCase()) !== -1
120+
);
119121
}
120122

121123
// Get sum of array
122124
function array_sum(arr) {
123-
is_array(arr);
124-
return arr.reduce((prev, curr) => (isNaN(curr) ? 0 : prev + curr), 0);
125+
is_array(arr);
126+
return arr.reduce((prev, curr) => (isNaN(curr) ? 0 : prev + curr), 0);
125127
}
126128

127129
// 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));
137-
} else {
138-
return array_sum(arr.slice(slice_start, slice_end));
139-
}
130+
function array_slice_sum(
131+
arr,
132+
slice_start = 0,
133+
slice_end = arr.length,
134+
includes = true
135+
) {
136+
is_array(arr);
137+
if (
138+
Number.isInteger(slice_start) &&
139+
Number.isInteger(slice_end) &&
140+
typeof includes === "boolean"
141+
) {
142+
if (includes) {
143+
return array_sum(arr.slice(slice_start, slice_end + 1));
140144
} else {
141-
throw new TypeError("Input parameters not valid!");
145+
return array_sum(arr.slice(slice_start, slice_end));
142146
}
147+
} else {
148+
throw new TypeError("Input parameters not valid!");
149+
}
143150
}
144151

145152
/**
@@ -150,17 +157,17 @@ function array_slice_sum(arr, slice_start = 0, slice_end = arr.length, includes
150157
* @returns array
151158
*/
152159
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-
});
163-
}
160+
is_array(arr);
161+
is_num_array(arr);
162+
if (reverse) {
163+
return arr.sort(function (a, b) {
164+
return b - a;
165+
});
166+
} else {
167+
return arr.sort(function (a, b) {
168+
return a - b;
169+
});
170+
}
164171
}
165172

166173
/**
@@ -175,24 +182,64 @@ function sort_nums(arr, reverse = false) {
175182
* @returns array
176183
*/
177184
function get_element_by_key_value(arr, key, value) {
178-
is_array(arr);
179-
return arr.filter((item) => item[key] == value);
185+
is_array(arr);
186+
return arr.filter((item) => item[key] == value);
187+
}
188+
189+
/**
190+
* Flatten a nested array (the nesting can be to any depth).
191+
*
192+
* @param array arr
193+
* @returns array
194+
*/
195+
function flatten(arr) {
196+
is_array(arr);
197+
return arr.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);
198+
}
199+
200+
/**
201+
* Gets the intersection betwene two arrays (i.e. the elements that are present in both arrays).
202+
*
203+
* @param array arr1
204+
* @param array arr2
205+
* @returns array
206+
*/
207+
function intersection(arr1, arr2) {
208+
is_array(arr1);
209+
is_array(arr2);
210+
return arr1.filter((item) => arr2.includes(item));
211+
}
212+
213+
/**
214+
* Gets the difference between two arrays (i.e. the elements that are present in the first array, but not in the second).
215+
* @param array arr1
216+
* @param array arr2
217+
* @returns array
218+
*/
219+
220+
function difference(arr1, arr2) {
221+
is_array(arr1);
222+
is_array(arr2);
223+
return arr1.filter((item) => !arr2.includes(item));
180224
}
181225

182226
export {
183-
is_array,
184-
is_num_array,
185-
head,
186-
tail,
187-
in_array,
188-
array_chunk,
189-
array_filter,
190-
array_frequency,
191-
object_to_array,
192-
get_all_indexes,
193-
search_in_array,
194-
array_sum,
195-
array_slice_sum,
196-
sort_nums,
197-
get_element_by_key_value,
227+
is_array,
228+
is_num_array,
229+
head,
230+
tail,
231+
in_array,
232+
array_chunk,
233+
array_filter,
234+
array_frequency,
235+
object_to_array,
236+
get_all_indexes,
237+
search_in_array,
238+
array_sum,
239+
array_slice_sum,
240+
sort_nums,
241+
get_element_by_key_value,
242+
flatten,
243+
intersection,
244+
difference,
198245
};

0 commit comments

Comments
 (0)