|
1 |
| -# js-array-helpers |
2 |
| - |
3 |
| - |
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 |
| - |
23 | 1 | ```js
|
24 |
| -import { |
25 |
| - is_array, |
26 |
| - object_to_array, |
27 |
| - search_in_array, |
28 |
| - sanitize_array, |
29 |
| - get_rms_value, |
30 |
| -} from "@hetarth02/js-array-helpers"; |
31 |
| - |
32 |
| -let arr = [1, 2]; |
33 |
| -console.log(is_array(arr)); // true |
34 |
| - |
35 |
| -const objectX = { |
36 |
| - 0: "Apple", |
37 |
| - 1: "Microsoft", |
38 |
| - 2: "Google", |
39 |
| -}; |
40 |
| -console.log(object_to_array(objectX)); // ['Apple', 'Microsoft', 'Google'] |
41 |
| - |
42 |
| -const mang = ["Microsoft", "apple", "netflix", "Google"]; |
43 |
| -const result = search_in_array("app", mang); |
44 |
| -console.log(result); // ['apple'] |
45 |
| - |
46 |
| -// Santized array Example |
47 |
| - |
48 |
| -// Corrupted Data array with diff data types |
49 |
| -const my_array = [ |
50 |
| - { name: "sam", age: null, isEmployed: "false" }, |
51 |
| - { name: "a", age: 456, isEmployed: false }, |
52 |
| - { name: "c", age: undefined, isEmployed: 00 }, |
53 |
| - { name: null, age: 123, isEmployed: true }, |
54 |
| - { name: "asd", age: 123, isEmployed: false }, |
55 |
| - { name: 00, age: 123, isEmployed: null }, |
56 |
| - { name: "sam", age: "123", isEmployed: undefined }, |
57 |
| -]; |
58 |
| - |
59 |
| -// Given schema for correct data types |
60 |
| -const my_schema = { |
61 |
| - name: "string", |
62 |
| - age: "number", |
63 |
| - isEmployed: "boolean", |
64 |
| -}; |
65 |
| - |
66 |
| -// Run sanitize_array with array and schema |
67 |
| -console.log(sanitize_array(my_array, my_schema)); |
68 |
| - |
69 |
| -// Sanitized Output |
70 |
| -// [ |
71 |
| -// { name: 'sam', age: 0, isEmployed: false }, |
72 |
| -// { name: 'a', age: 456, isEmployed: false }, |
73 |
| -// { name: 'c', age: 0, isEmployed: true }, |
74 |
| -// { name: 'null', age: 123, isEmployed: true }, |
75 |
| -// { name: 'asd', age: 123, isEmployed: false }, |
76 |
| -// { name: '0', age: 123, isEmployed: false }, |
77 |
| -// { name: 'sam', age: 123, isEmployed: false } |
78 |
| -// ] |
79 |
| - |
80 |
| -// get_rms_value example |
81 |
| -const values = [23, 54, 19]; |
82 |
| -console.log(get_rms_value(values)); // 35.61834733205159 |
83 |
| - |
84 |
| -// To reverse an array in parts |
85 |
| -let my_array = [1, 2, 3, 4, 5]; |
86 |
| -let reverseInPart_array = array_reverse_part(my_array, 3, 4); |
87 |
| - |
88 |
| -console.log(reverseInPart_array); // [1, 2, 3, 5, 4] |
89 |
| - |
90 |
| -// To rotate array counter clockwise |
91 |
| -let my_array1 = [1, 2, 3, 4, 5]; |
92 |
| -let rotated_array = array_rotate(my_array1, 3); |
| 2 | +import { random_value } from "@hetarth02/js-array-helpers"; |
93 | 3 |
|
94 |
| -console.log(rotated_array); // [4, 5, 1, 2, 3] |
| 4 | +let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 5 | +console.log(random_value(array)); // ex: 5 (Random value from array) |
95 | 6 | ```
|
0 commit comments