Skip to content

Commit 2befaa4

Browse files
committed
Major changes v3
- Migrated from funtion based to a class based approach Minor changes - Updated the README.md according to new changes - added two new helper methods - node version changes in workflow file
1 parent f57505b commit 2befaa4

File tree

4 files changed

+122
-109
lines changed

4 files changed

+122
-109
lines changed

.github/workflows/release-package.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- uses: actions/checkout@v3
1212
- uses: actions/setup-node@v3
1313
with:
14-
node-version: 12
14+
node-version: "16.x"
1515
- run: npm ci
1616
- run: npm test
1717

@@ -25,7 +25,7 @@ jobs:
2525
- uses: actions/checkout@v3
2626
- uses: actions/setup-node@v3
2727
with:
28-
node-version: 12
28+
node-version: "16.x"
2929
registry-url: https://npm.pkg.github.com/
3030
- run: npm ci
3131
- run: npm publish

README.md

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

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

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

2424
### object_to_array
2525
```js
26-
const objectX = {
27-
0:"Apple",
28-
1:"Microsoft",
29-
2:"Google"
30-
}
26+
const objectX = {
27+
0: "Apple",
28+
1: "Microsoft",
29+
2: "Google"
30+
}
3131

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

3635
### search_in_array
3736
```js
38-
39-
const mang = [ 'Microsoft','apple','netflix','Google' ]
37+
const mang = ['Microsoft', 'apple', 'netflix', 'Google']
4038

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

4341
console.log(result); // ['apple']
44-
45-
4642
```

index.js

Lines changed: 109 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,128 @@
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+
}
106

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

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

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

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);
3630
}
3731

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

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;
4751
}
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+
});
4960
return arr;
5061
}
51-
}
5262

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

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+
}
7193
}
72-
});
73-
return freq_obj;
74-
}
94+
return indexes;
95+
}
7596

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

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);
92107
}
93-
return indexes;
94-
}
95108

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+
}
98126
}
99127

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;

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": "2.2.3",
3+
"version": "3.2.3",
44
"description": "Array Helper functions.",
55
"main": "index.js",
66
"type": "module",

0 commit comments

Comments
 (0)