nv-random-util 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +39 -0
  2. package/index.js +58 -0
  3. package/package.json +15 -0
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ nv-random-util
2
+ ============
3
+ - nv-random-util
4
+
5
+
6
+ install
7
+ =======
8
+ - npm install nv-random-util
9
+
10
+ usage
11
+ =====
12
+
13
+ example
14
+ -------
15
+
16
+ const rnd = require('nv-random-util')
17
+
18
+
19
+
20
+ APIS
21
+ ====
22
+
23
+
24
+ {
25
+ rand_from_ary: [Function: rand_from_ary],
26
+ rand_from_set: [Function: rand_from_set],
27
+ rand_key_from_dict: [Function: rand_key_from_dict],
28
+ rand_value_from_dict: [Function: rand_value_from_dict],
29
+ rand_pair_from_dict: [Function: rand_pair_from_dict],
30
+ rand_pair_from_map: [Function: rand_pair_from_map],
31
+ rand_key_from_map: [Function: rand_key_from_map],
32
+ rand_value_from_map: [Function: rand_value_from_map]
33
+ }
34
+
35
+
36
+
37
+ LICENSE
38
+ =======
39
+ - ISC
package/index.js ADDED
@@ -0,0 +1,58 @@
1
+ const {int} = require("nv-random-number");
2
+
3
+
4
+ function rand_from_ary(ary) {
5
+ let idx = int(0,ary.length);
6
+ return(ary[idx])
7
+ }
8
+
9
+
10
+ function rand_from_set(st) {
11
+ return(rand_from_ary(Array.from(st)))
12
+ }
13
+
14
+
15
+ function rand_key_from_dict(d) {
16
+ return(rand_from_ary(Object.keys(d)))
17
+ }
18
+
19
+ function rand_value_from_dict(d) {
20
+ return(rand_from_ary(Object.values(d)))
21
+ }
22
+
23
+ function rand_pair_from_dict(d) {
24
+ return(rand_from_ary(Object.entries(d)))
25
+ }
26
+
27
+ function rand_pair_from_map(mp) {
28
+ let c = 0;
29
+ let lngth = mp.size;
30
+ let entries = mp.entries();
31
+ let idx = int(0,lngth);
32
+ for(let e of entries) {
33
+ if(idx === c) {
34
+ return(e)
35
+ } else {}
36
+ c = c +1;
37
+ }
38
+ }
39
+
40
+ function rand_key_from_map(mp) {
41
+ return(rand_pair_from_map(mp)[0])
42
+ }
43
+
44
+ function rand_value_from_map(mp) {
45
+ return(rand_pair_from_map(mp)[1])
46
+ }
47
+
48
+ module.exports = {
49
+ rand_from_ary,
50
+ rand_from_set,
51
+ rand_key_from_dict,
52
+ rand_value_from_dict,
53
+ rand_pair_from_dict,
54
+ rand_pair_from_map,
55
+ rand_key_from_map,
56
+ rand_value_from_map
57
+ }
58
+
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "dependencies": {
3
+ "nv-random-number": "^1.0.2"
4
+ },
5
+ "version": "0.0.2",
6
+ "name": "nv-random-util",
7
+ "description": "nv-random-util ============ - nv-random-util",
8
+ "main": "index.js",
9
+ "devDependencies": {},
10
+ "scripts": {
11
+ "test": "echo \"Error: no test specified\" && exit 1"
12
+ },
13
+ "author": "",
14
+ "license": "ISC"
15
+ }