Skip to content

Commit 14f1d2e

Browse files
committed
Major Changes:
- Reverted back to function based system from Class based system Minor Changes - Updated README.md
1 parent 937cc1c commit 14f1d2e

File tree

3 files changed

+160
-127
lines changed

3 files changed

+160
-127
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ In your `package.json` add the following, `"type": "module"`.
1515

1616
### is_array()
1717
```js
18-
import Helper from "@hetarth02/js-array-helpers";
18+
import { is_array } from "@hetarth02/js-array-helpers";
1919

2020
let arr = [1, 2];
21-
console.log(Helper.is_array(arr)); // true
21+
console.log(is_array(arr)); // true
2222
```
2323

2424
### object_to_array
@@ -29,14 +29,14 @@ const objectX = {
2929
2: "Google"
3030
}
3131

32-
console.log(Helper.object_to_array(objectX)) // ['Apple', 'Microsoft', 'Google']
32+
console.log(object_to_array(objectX)) // ['Apple', 'Microsoft', 'Google']
3333
```
3434

3535
### search_in_array
3636
```js
3737
const mang = ['Microsoft', 'apple', 'netflix', 'Google']
3838

39-
const result = Helper.search_in_array("app", mang);
39+
const result = search_in_array("app", mang);
4040

4141
console.log(result); // ['apple']
4242
```

index.js

Lines changed: 155 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,181 @@
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+
}
69

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+
}
1119

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!");
2027
}
28+
}
2129

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+
}
2735

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+
}
3543

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+
}
4149

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+
}
4856

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));
5159

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));
6166
}
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;
7068
return arr;
7169
}
70+
}
7271

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+
}
8680

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+
}
9494

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);
103110
}
104-
return indexes;
105111
}
112+
return indexes;
113+
}
106114

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+
}
112120

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+
}
118126

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));
132137
} else {
133-
throw new TypeError("Input parameters not valid!");
138+
return array_sum(arr.slice(slice_start, slice_end));
134139
}
140+
} else {
141+
throw new TypeError("Input parameters not valid!");
135142
}
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+
});
145163
}
146164
}
147165

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+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hetarth02/js-array-helpers",
3-
"version": "3.2.3",
3+
"version": "4.0.0",
44
"description": "Array Helper functions.",
55
"main": "index.js",
66
"type": "module",

0 commit comments

Comments
 (0)