Skip to content

Commit e2be60f

Browse files
authored
Merge pull request #29 from MohammadMazin/main
Get Random Value
2 parents e646997 + 171bbb4 commit e2be60f

File tree

3 files changed

+25
-99
lines changed

3 files changed

+25
-99
lines changed

README.md

+3-92
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,6 @@
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-
231
```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";
933

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)
956
```

index.js

+20-5
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,12 +506,26 @@ 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
}, {});
513513
}
514514

515+
/**
516+
* Get a random value from the array
517+
*
518+
* @param array arr
519+
* @returns any | TypeError
520+
*/
521+
function random_value(arr) {
522+
is_array(arr);
523+
if (arr.length == 0) {
524+
return null;
525+
}
526+
return arr[Math.floor(Math.random() * arr.length)];
527+
}
528+
515529
export {
516530
is_array,
517531
is_num_array,
@@ -541,4 +555,5 @@ export {
541555
find_and_update,
542556
for_in_range,
543557
group_by,
544-
};
558+
random_value
559+
};

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)