random-weighted-pick 2.0.0 → 2.0.1
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/.local-tests/esm-smoke/package-lock.json +25 -0
- package/.local-tests/esm-smoke/package.json +16 -0
- package/.local-tests/esm-smoke/test.mjs +52 -0
- package/.local-tests/npm-smoke/package-lock.json +25 -0
- package/.local-tests/npm-smoke/package.json +15 -0
- package/.local-tests/npm-smoke/test.js +61 -0
- package/dist/cjs/index.js +9 -7
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +5 -3
- package/package.json +1 -1
- package/random-weighted-pick-2.0.0.tgz +0 -0
- package/random-weighted-pick-2.0.1.tgz +0 -0
- package/tsconfig.esm.json +2 -1
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "esm-smoke",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"lockfileVersion": 3,
|
|
5
|
+
"requires": true,
|
|
6
|
+
"packages": {
|
|
7
|
+
"": {
|
|
8
|
+
"name": "esm-smoke",
|
|
9
|
+
"version": "1.0.0",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"random-weighted-pick": "file:../../random-weighted-pick-2.0.0.tgz"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"node_modules/random-weighted-pick": {
|
|
16
|
+
"version": "2.0.0",
|
|
17
|
+
"resolved": "file:../../random-weighted-pick-2.0.0.tgz",
|
|
18
|
+
"integrity": "sha512-Q2BUYXnDDTv5boCpsMCgusna4Wd5OzKab0zjcrBgiZ0L8pPCYlweYwhTH3udO9YNrCZqKCygioeT21CXEdvOFA==",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=16"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "esm-smoke",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "test.mjs",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node test.mjs"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"description": "",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"random-weighted-pick": "file:../../random-weighted-pick-2.0.0.tgz"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/* Smoke test for random-weighted-pick (ESM) */
|
|
2
|
+
import def, { weightedPick, pickMany, createWeightedPicker } from 'random-weighted-pick'
|
|
3
|
+
|
|
4
|
+
if (typeof def !== 'function') {
|
|
5
|
+
console.error('Default export is not a function (ESM)')
|
|
6
|
+
process.exit(1)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (typeof weightedPick !== 'function' || typeof pickMany !== 'function' || typeof createWeightedPicker !== 'function') {
|
|
10
|
+
console.error('Named exports missing (ESM)')
|
|
11
|
+
process.exit(1)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const options = [
|
|
15
|
+
{ id: 0, weight: 0.2, item: 'A' },
|
|
16
|
+
{ id: 1, weight: 0.3, item: 'B' },
|
|
17
|
+
{ id: 2, weight: 0.5, item: 'C' },
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
let i = 0
|
|
21
|
+
const values = [0.05, 0.35, 0.85, 0.49, 0.51]
|
|
22
|
+
const rng = () => values[i++ % values.length]
|
|
23
|
+
|
|
24
|
+
const one = def(options, { rng })
|
|
25
|
+
console.log('esm:one:', one)
|
|
26
|
+
|
|
27
|
+
const one2 = weightedPick(options, { rng })
|
|
28
|
+
console.log('esm:one2:', one2)
|
|
29
|
+
|
|
30
|
+
const many = pickMany(options, 3, { rng })
|
|
31
|
+
console.log('esm:many:', many)
|
|
32
|
+
|
|
33
|
+
const picker = createWeightedPicker(options, { method: 'alias', rng })
|
|
34
|
+
const p1 = picker.pick()
|
|
35
|
+
const p2 = picker.pick()
|
|
36
|
+
const many2 = picker.pickMany(2)
|
|
37
|
+
console.log('esm:picker:', { p1, p2, many2 })
|
|
38
|
+
|
|
39
|
+
const assertResult = r => {
|
|
40
|
+
if (!r || typeof r.id !== 'number' || !['A', 'B', 'C'].includes(r.item)) {
|
|
41
|
+
throw new Error('Invalid result shape: ' + JSON.stringify(r))
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
assertResult(one)
|
|
46
|
+
assertResult(one2)
|
|
47
|
+
many.forEach(assertResult)
|
|
48
|
+
many2.forEach(assertResult)
|
|
49
|
+
|
|
50
|
+
console.log('ESM smoke test passed.')
|
|
51
|
+
|
|
52
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "npm-smoke",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"lockfileVersion": 3,
|
|
5
|
+
"requires": true,
|
|
6
|
+
"packages": {
|
|
7
|
+
"": {
|
|
8
|
+
"name": "npm-smoke",
|
|
9
|
+
"version": "1.0.0",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"random-weighted-pick": "^2.0.0"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"node_modules/random-weighted-pick": {
|
|
16
|
+
"version": "2.0.0",
|
|
17
|
+
"resolved": "https://registry.npmjs.org/random-weighted-pick/-/random-weighted-pick-2.0.0.tgz",
|
|
18
|
+
"integrity": "sha512-+1W4Nzs+vOpFSUZxTULfNHBEBjRp9zgG87uim6b4oVM5NXJkBteK5+U5FHTsaQLu8Ir670scsVOWjwN0/iP5Bg==",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=16"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "npm-smoke",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "node test.js"
|
|
7
|
+
},
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"description": "",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"random-weighted-pick": "^2.0.0"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/* Smoke test for random-weighted-pick (CJS) */
|
|
2
|
+
const lib = require('random-weighted-pick')
|
|
3
|
+
|
|
4
|
+
// Library should expose default function and named exports
|
|
5
|
+
if (typeof lib !== 'function') {
|
|
6
|
+
console.error('Default export is not a function')
|
|
7
|
+
process.exit(1)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const { weightedPick, pickMany, createWeightedPicker } = lib
|
|
11
|
+
|
|
12
|
+
if (typeof weightedPick !== 'function' || typeof pickMany !== 'function' || typeof createWeightedPicker !== 'function') {
|
|
13
|
+
console.error('Named exports missing')
|
|
14
|
+
process.exit(1)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const options = [
|
|
18
|
+
{ id: 0, weight: 0.2, item: 'A' },
|
|
19
|
+
{ id: 1, weight: 0.3, item: 'B' },
|
|
20
|
+
{ id: 2, weight: 0.5, item: 'C' },
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
// Deterministic RNG for repeatability
|
|
24
|
+
let i = 0
|
|
25
|
+
const values = [0.05, 0.35, 0.85, 0.49, 0.51]
|
|
26
|
+
const rng = () => values[i++ % values.length]
|
|
27
|
+
|
|
28
|
+
// Single pick via default export
|
|
29
|
+
const one = lib(options, { rng })
|
|
30
|
+
console.log('one:', one)
|
|
31
|
+
|
|
32
|
+
// Single pick via named export
|
|
33
|
+
const one2 = weightedPick(options, { rng })
|
|
34
|
+
console.log('one2:', one2)
|
|
35
|
+
|
|
36
|
+
// Many picks
|
|
37
|
+
const many = pickMany(options, 3, { rng })
|
|
38
|
+
console.log('many:', many)
|
|
39
|
+
|
|
40
|
+
// Picker API
|
|
41
|
+
const picker = createWeightedPicker(options, { method: 'alias', rng })
|
|
42
|
+
const p1 = picker.pick()
|
|
43
|
+
const p2 = picker.pick()
|
|
44
|
+
const many2 = picker.pickMany(2)
|
|
45
|
+
console.log('picker:', { p1, p2, many2 })
|
|
46
|
+
|
|
47
|
+
// Basic assertions
|
|
48
|
+
const assertResult = r => {
|
|
49
|
+
if (!r || typeof r.id !== 'number' || !['A', 'B', 'C'].includes(r.item)) {
|
|
50
|
+
throw new Error('Invalid result shape: ' + JSON.stringify(r))
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
assertResult(one)
|
|
55
|
+
assertResult(one2)
|
|
56
|
+
many.forEach(assertResult)
|
|
57
|
+
many2.forEach(assertResult)
|
|
58
|
+
|
|
59
|
+
console.log('Smoke test passed.')
|
|
60
|
+
|
|
61
|
+
|
package/dist/cjs/index.js
CHANGED
|
@@ -34,10 +34,12 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.createWeightedPicker = exports.pickMany = exports.weightedPick = void 0;
|
|
37
|
-
const
|
|
38
|
-
exports.weightedPick =
|
|
39
|
-
Object.defineProperty(exports, "pickMany", { enumerable: true, get: function () { return
|
|
40
|
-
Object.defineProperty(exports, "createWeightedPicker", { enumerable: true, get: function () { return
|
|
41
|
-
exports.default =
|
|
42
|
-
|
|
43
|
-
module.exports.default
|
|
37
|
+
const weightList_js_1 = __importStar(require("./weightList.js"));
|
|
38
|
+
exports.weightedPick = weightList_js_1.default;
|
|
39
|
+
Object.defineProperty(exports, "pickMany", { enumerable: true, get: function () { return weightList_js_1.pickMany; } });
|
|
40
|
+
Object.defineProperty(exports, "createWeightedPicker", { enumerable: true, get: function () { return weightList_js_1.createWeightedPicker; } });
|
|
41
|
+
exports.default = weightList_js_1.default;
|
|
42
|
+
if (typeof module !== 'undefined' && module?.exports) {
|
|
43
|
+
module.exports = Object.assign(weightList_js_1.default, { weightedPick: weightList_js_1.default, pickMany: weightList_js_1.pickMany, createWeightedPicker: weightList_js_1.createWeightedPicker });
|
|
44
|
+
module.exports.default = weightList_js_1.default;
|
|
45
|
+
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import weightedPick, { pickMany, createWeightedPicker, RNG, PickConfig, Method, WeightedInput, WeightedResult } from './weightList';
|
|
1
|
+
import weightedPick, { pickMany, createWeightedPicker, RNG, PickConfig, Method, WeightedInput, WeightedResult } from './weightList.js';
|
|
2
2
|
export type { RNG, PickConfig, Method, WeightedInput, WeightedResult };
|
|
3
3
|
export { weightedPick, pickMany, createWeightedPicker };
|
|
4
4
|
export default weightedPick;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import weightedPick, { pickMany, createWeightedPicker, } from './weightList';
|
|
1
|
+
import weightedPick, { pickMany, createWeightedPicker, } from './weightList.js';
|
|
2
2
|
export { weightedPick, pickMany, createWeightedPicker };
|
|
3
3
|
export default weightedPick;
|
|
4
|
-
|
|
5
|
-
module.exports
|
|
4
|
+
if (typeof module !== 'undefined' && module?.exports) {
|
|
5
|
+
module.exports = Object.assign(weightedPick, { weightedPick, pickMany, createWeightedPicker });
|
|
6
|
+
module.exports.default = weightedPick;
|
|
7
|
+
}
|
package/package.json
CHANGED
|
Binary file
|
|
Binary file
|