alea-deck 3.0.0 → 4.0.0
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.
- package/index.js +20 -20
- package/package.json +7 -7
package/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
const random = require('alea-random');
|
|
2
|
+
const isPlainObject = require('lodash.isplainobject');
|
|
3
|
+
const keys = require('lodash.keys');
|
|
4
|
+
const reduce = require('lodash.reduce');
|
|
5
|
+
const isNumber = require('lodash.isnumber');
|
|
6
6
|
|
|
7
7
|
function deck(xs) {
|
|
8
8
|
if (!Array.isArray(xs) && !isPlainObject(xs)) {
|
|
@@ -17,25 +17,25 @@ function deck(xs) {
|
|
|
17
17
|
|
|
18
18
|
function shuffle(xs) {
|
|
19
19
|
if (Array.isArray(xs)) {
|
|
20
|
-
|
|
21
|
-
for (
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
const res = xs.slice();
|
|
21
|
+
for (let i = res.length - 1; i >= 0; i--) {
|
|
22
|
+
const n = Math.floor(random(true) * i);
|
|
23
|
+
const t = res[i];
|
|
24
24
|
res[i] = res[n];
|
|
25
25
|
res[n] = t;
|
|
26
26
|
}
|
|
27
27
|
return res;
|
|
28
28
|
}
|
|
29
29
|
else if (isPlainObject(xs)) {
|
|
30
|
-
|
|
30
|
+
const weights = reduce(keys(xs), function(acc, key) {
|
|
31
31
|
acc[key] = xs[key];
|
|
32
32
|
return acc;
|
|
33
33
|
}, {});
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
const ret = [];
|
|
36
36
|
|
|
37
37
|
while (keys(weights).length > 0) {
|
|
38
|
-
|
|
38
|
+
const key = pick(weights);
|
|
39
39
|
delete weights[key];
|
|
40
40
|
ret.push(key);
|
|
41
41
|
}
|
|
@@ -52,16 +52,16 @@ function pick(xs) {
|
|
|
52
52
|
return xs[Math.floor(random(true) * xs.length)];
|
|
53
53
|
}
|
|
54
54
|
else if (isPlainObject(xs)) {
|
|
55
|
-
|
|
55
|
+
const weights = normalize(xs);
|
|
56
56
|
if (!weights) {
|
|
57
57
|
return undefined;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
const n = random(true);
|
|
61
|
+
let threshold = 0;
|
|
62
|
+
const keyz = keys(weights);
|
|
63
63
|
|
|
64
|
-
for (
|
|
64
|
+
for (let i = 0; i < keyz.length; i++) {
|
|
65
65
|
threshold += weights[keyz[i]];
|
|
66
66
|
if (n < threshold) {
|
|
67
67
|
return keyz[i];
|
|
@@ -79,13 +79,13 @@ function normalize(weights) {
|
|
|
79
79
|
throw new TypeError('`weights` must be an object');
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
|
|
82
|
+
const keyz = keys(weights);
|
|
83
83
|
if (!keyz.length) {
|
|
84
84
|
return undefined;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
const total = reduce(keyz, function(sum, key) {
|
|
88
|
+
const x = weights[key];
|
|
89
89
|
if (x < 0) {
|
|
90
90
|
throw new Error('Negative weight encountered at key ' + key);
|
|
91
91
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "alea-deck",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Uniform and weighted shuffling and sampling with Alea",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"alea",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"test": "test"
|
|
20
20
|
},
|
|
21
21
|
"engines": {
|
|
22
|
-
"node": "
|
|
22
|
+
"node": "14 || 16 || >=18"
|
|
23
23
|
},
|
|
24
24
|
"scripts": {
|
|
25
25
|
"lint": "eslint *.js test/*.js",
|
|
@@ -35,15 +35,15 @@
|
|
|
35
35
|
"lodash.reduce": "^4.6.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@kenan/eslint-config": "^
|
|
38
|
+
"@kenan/eslint-config": "^10.0.1",
|
|
39
39
|
"@semantic-release/changelog": "^6.0.1",
|
|
40
40
|
"@semantic-release/git": "^10.0.1",
|
|
41
|
-
"conventional-changelog-conventionalcommits": "^
|
|
42
|
-
"eslint": "^
|
|
41
|
+
"conventional-changelog-conventionalcommits": "^5.0.0",
|
|
42
|
+
"eslint": "^8.28.0",
|
|
43
43
|
"lodash.every": "^4.6.0",
|
|
44
44
|
"lodash.isundefined": "^3.0.1",
|
|
45
45
|
"lodash.map": "^4.6.0",
|
|
46
|
-
"semantic-release": "^19.0.
|
|
47
|
-
"tape": "^5.
|
|
46
|
+
"semantic-release": "^19.0.5",
|
|
47
|
+
"tape": "^5.6.1"
|
|
48
48
|
}
|
|
49
49
|
}
|