Skip to content

Commit 0e85a63

Browse files
committed
- Updated README.md
- Code Fixes
1 parent e2be60f commit 0e85a63

File tree

2 files changed

+101
-7
lines changed

2 files changed

+101
-7
lines changed

README.md

+95-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,100 @@
1+
# js-array-helpers
2+
3+
![npm (scoped)](https://img.shields.io/npm/v/@hetarth02/js-array-helpers?style=for-the-badge)
4+
5+
Array Helper functions for your quick use.
6+
7+
# Installation
8+
9+
```cd
10+
npm i @hetarth02/js-array-helpers
11+
```
12+
13+
# Contributing
14+
15+
- To conttribute please refer [CONTRIBUTING.md](CONTRIBUTING.md).
16+
17+
# How to use
18+
19+
In your `package.json` add the following, `"type": "module"`.
20+
21+
# Example Usage
22+
123
```js
2-
import { random_value } from "@hetarth02/js-array-helpers";
24+
import {
25+
is_array,
26+
object_to_array,
27+
search_in_array,
28+
sanitize_array,
29+
get_rms_value,
30+
random_value,
31+
} from "@hetarth02/js-array-helpers";
32+
33+
let arr = [1, 2];
34+
console.log(is_array(arr)); // true
35+
36+
const objectX = {
37+
0: "Apple",
38+
1: "Microsoft",
39+
2: "Google",
40+
};
41+
console.log(object_to_array(objectX)); // ['Apple', 'Microsoft', 'Google']
42+
43+
const mang = ["Microsoft", "apple", "netflix", "Google"];
44+
const result = search_in_array("app", mang);
45+
console.log(result); // ['apple']
46+
47+
// Santized array Example
48+
49+
// Corrupted Data array with diff data types
50+
const my_array = [
51+
{ name: "sam", age: null, isEmployed: "false" },
52+
{ name: "a", age: 456, isEmployed: false },
53+
{ name: "c", age: undefined, isEmployed: 00 },
54+
{ name: null, age: 123, isEmployed: true },
55+
{ name: "asd", age: 123, isEmployed: false },
56+
{ name: 00, age: 123, isEmployed: null },
57+
{ name: "sam", age: "123", isEmployed: undefined },
58+
];
59+
60+
// Given schema for correct data types
61+
const my_schema = {
62+
name: "string",
63+
age: "number",
64+
isEmployed: "boolean",
65+
};
66+
67+
// Run sanitize_array with array and schema
68+
console.log(sanitize_array(my_array, my_schema));
69+
70+
// Sanitized Output
71+
// [
72+
// { name: 'sam', age: 0, isEmployed: false },
73+
// { name: 'a', age: 456, isEmployed: false },
74+
// { name: 'c', age: 0, isEmployed: true },
75+
// { name: 'null', age: 123, isEmployed: true },
76+
// { name: 'asd', age: 123, isEmployed: false },
77+
// { name: '0', age: 123, isEmployed: false },
78+
// { name: 'sam', age: 123, isEmployed: false }
79+
// ]
80+
81+
// get_rms_value example
82+
const values = [23, 54, 19];
83+
console.log(get_rms_value(values)); // 35.61834733205159
84+
85+
// To reverse an array in parts
86+
let my_array = [1, 2, 3, 4, 5];
87+
let reverseInPart_array = array_reverse_part(my_array, 3, 4);
88+
89+
console.log(reverseInPart_array); // [1, 2, 3, 5, 4]
90+
91+
// To rotate array counter clockwise
92+
let my_array1 = [1, 2, 3, 4, 5];
93+
let rotated_array = array_rotate(my_array1, 3);
94+
95+
console.log(rotated_array); // [4, 5, 1, 2, 3]
396

497
let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
98+
599
console.log(random_value(array)); // ex: 5 (Random value from array)
6100
```

index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function is_array(arr) {
2424
* @returns bool | TypeError
2525
*/
2626
function is_num_array(arr) {
27-
var a = arr.reduce(function(result, val) {
27+
var a = arr.reduce(function (result, val) {
2828
return result && typeof val === "number";
2929
}, true);
3030
if (a == false) {
@@ -220,11 +220,11 @@ function sort_nums(arr, reverse = false) {
220220
is_array(arr);
221221
is_num_array(arr);
222222
if (reverse) {
223-
return arr.sort(function(a, b) {
223+
return arr.sort(function (a, b) {
224224
return b - a;
225225
});
226226
} else {
227-
return arr.sort(function(a, b) {
227+
return arr.sort(function (a, b) {
228228
return a - b;
229229
});
230230
}
@@ -506,7 +506,7 @@ function for_in_range(num, callback) {
506506
*/
507507
function group_by(arr, key) {
508508
is_array(arr);
509-
return arr.reduce(function(rv, x) {
509+
return arr.reduce(function (rv, x) {
510510
(rv[x[key]] = rv[x[key]] || []).push(x);
511511
return rv;
512512
}, {});
@@ -555,5 +555,5 @@ export {
555555
find_and_update,
556556
for_in_range,
557557
group_by,
558-
random_value
559-
};
558+
random_value,
559+
};

0 commit comments

Comments
 (0)