random-weighted-pick 2.0.1 → 2.1.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/LICENSE +1 -1
- package/README.md +209 -116
- package/dist/cjs/index.d.ts +18 -0
- package/dist/cjs/index.js +3 -45
- package/dist/cjs/weightList.d.ts +42 -0
- package/dist/cjs/weightList.js +200 -138
- package/dist/esm/index.d.ts +2 -4
- package/dist/esm/index.js +1 -7
- package/dist/esm/weightList.d.ts +24 -18
- package/dist/esm/weightList.js +200 -138
- package/package.json +35 -22
- package/.babelrc +0 -6
- package/.circleci/config.yml +0 -20
- package/.eslintrc.json +0 -7
- package/.local-tests/esm-smoke/package-lock.json +0 -25
- package/.local-tests/esm-smoke/package.json +0 -16
- package/.local-tests/esm-smoke/test.mjs +0 -52
- package/.local-tests/npm-smoke/package-lock.json +0 -25
- package/.local-tests/npm-smoke/package.json +0 -15
- package/.local-tests/npm-smoke/test.js +0 -61
- package/.prettierrc +0 -4
- package/CONTRIBUTING.md +0 -9
- package/jest.config.cjs +0 -5
- package/random-weighted-pick-2.0.0.tgz +0 -0
- package/random-weighted-pick-2.0.1.tgz +0 -0
- package/tsconfig.cjs.json +0 -8
- package/tsconfig.esm.json +0 -9
- package/tsconfig.json +0 -20
|
@@ -1,61 +0,0 @@
|
|
|
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/.prettierrc
DELETED
package/CONTRIBUTING.md
DELETED
package/jest.config.cjs
DELETED
|
Binary file
|
|
Binary file
|
package/tsconfig.cjs.json
DELETED
package/tsconfig.esm.json
DELETED
package/tsconfig.json
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"outDir": "./dist",
|
|
7
|
-
"strict": true,
|
|
8
|
-
"esModuleInterop": true,
|
|
9
|
-
"forceConsistentCasingInFileNames": true,
|
|
10
|
-
"types": ["node"],
|
|
11
|
-
"moduleResolution": "node"
|
|
12
|
-
},
|
|
13
|
-
"include": ["src/**/*"],
|
|
14
|
-
"exclude": [
|
|
15
|
-
"node_modules",
|
|
16
|
-
"dist",
|
|
17
|
-
"**/*.test.ts",
|
|
18
|
-
"src/utils/tests.utils.ts"
|
|
19
|
-
]
|
|
20
|
-
}
|