|
| 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 | + |
1 | 23 | ```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] |
3 | 96 |
|
4 | 97 | let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
| 98 | + |
5 | 99 | console.log(random_value(array)); // ex: 5 (Random value from array)
|
6 | 100 | ```
|
0 commit comments