Skip to content

Commit 262c5f4

Browse files
committed
Changes in README
1 parent 779a9ba commit 262c5f4

File tree

4 files changed

+102
-104
lines changed

4 files changed

+102
-104
lines changed

.gitignore

Lines changed: 0 additions & 104 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# js-array-helpers
2+
![npm (scoped)](https://img.shields.io/npm/v/@hetarth02/js-array-helpers?style=for-the-badge)
3+
24
Array Helper functions for your quick use.

index.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function _throw() {
2+
throw new TypeError("Must be a valid array!");
3+
}
4+
5+
function is_array(arr) {
6+
return Array.isArray(arr) ? true : _throw();
7+
}
8+
9+
function head(arr) {
10+
is_array(arr);
11+
return arr[0];
12+
}
13+
14+
function tail(arr) {
15+
is_array(arr);
16+
let element = arr.pop();
17+
arr.push(element);
18+
return element;
19+
}
20+
21+
function in_array(arr, value) {
22+
is_array(arr);
23+
return arr.includes(value);
24+
}
25+
26+
function array_chunk(arr, chunk_size) {
27+
is_array(arr);
28+
if (typeof chunk_size != "number") {
29+
throw new TypeError("chunk_size should be a integer!");
30+
}
31+
32+
let length = arr.length;
33+
chunk_size = Math.abs(Math.round(chunk_size));
34+
35+
if (chunk_size > length || [0, null, NaN].includes(chunk_size)) {
36+
return arr;
37+
} else {
38+
let modified_array = [];
39+
for (let index = 0; index < length; index += chunk_size) {
40+
modified_array.push(arr.slice(index, index + chunk_size));
41+
}
42+
arr = modified_array;
43+
return arr;
44+
}
45+
}
46+
47+
function array_filter(arr) {
48+
is_array(arr);
49+
arr = arr.filter(function (e) {
50+
return e === 0 || e;
51+
});
52+
return arr;
53+
}
54+
55+
function array_frequency(arr) {
56+
is_array(arr);
57+
let freq_obj = {};
58+
arr.forEach((value) => {
59+
if (value in freq_obj) {
60+
freq_obj[value] += 1;
61+
} else {
62+
freq_obj[value] = 1;
63+
}
64+
});
65+
return freq_obj;
66+
}
67+
68+
export { is_array, head, tail, in_array, array_filter, array_chunk, array_frequency };

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "@hetarth02/js-array-helpers",
3+
"version": "1.0.0",
4+
"description": "Array Helper functions.",
5+
"type": "module",
6+
"main": "index.js",
7+
"exports": {
8+
".": {
9+
"require": "./dist/helpers.js",
10+
"default": "./dist/helpers.module.js"
11+
}
12+
},
13+
"license": "MIT",
14+
"author": {
15+
"name": "Hetarth Shah",
16+
"url": "https://hetarth.tech/"
17+
},
18+
"homepage": "https://github.com/Hetarth02/js-array-helpers#README",
19+
"repository": {
20+
"type": "git",
21+
"url": "https://github.com/hetarth02/js-array-helpers.git"
22+
},
23+
"bugs": {
24+
"url": "https://github.com/hetarth02/js-array-helpers/issues"
25+
},
26+
"keywords": [
27+
"array helpers",
28+
"js array helpers",
29+
"package",
30+
"helpers"
31+
]
32+
}

0 commit comments

Comments
 (0)