@esportsplus/random 0.0.7 → 0.0.9

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/build/item.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { rng } from '@esportsplus/crypto';
2
- const BASIS_POINTS = 10000;
2
+ let cache = new WeakMap();
3
3
  export default (items, weights) => {
4
4
  if (weights === undefined) {
5
5
  return items[Math.floor(rng() * items.length)];
@@ -7,16 +7,17 @@ export default (items, weights) => {
7
7
  if (items.length !== weights.length) {
8
8
  throw new Error('Random: each item requires a weight');
9
9
  }
10
- let random = rng() * BASIS_POINTS, total = BASIS_POINTS;
11
- for (let i = 0, n = weights.length; i < n; i++) {
12
- total -= weights[i];
13
- }
14
- if (total !== 0) {
15
- throw new Error('Random: weights use basis point math, they must add up to 10000 ( 100% )');
10
+ let total = cache.get(weights) || 0;
11
+ if (!total) {
12
+ for (let i = 0, n = weights.length; i < n; i++) {
13
+ total += weights[i];
14
+ }
15
+ cache.set(weights, total);
16
16
  }
17
+ let current = 0, random = rng() * total;
17
18
  for (let i = 0, n = items.length; i < n; i++) {
18
- total += weights[i];
19
- if (random <= total) {
19
+ current += weights[i];
20
+ if (random <= current) {
20
21
  return items[i];
21
22
  }
22
23
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "ICJR",
3
3
  "description": "Random",
4
4
  "devDependencies": {
5
- "@esportsplus/rspack": "^0.0.10"
5
+ "@esportsplus/typescript": "^0.0.15"
6
6
  },
7
7
  "main": "build/index.js",
8
8
  "name": "@esportsplus/random",
@@ -14,8 +14,8 @@
14
14
  "prepublishOnly": "npm run build"
15
15
  },
16
16
  "types": "build/index.d.ts",
17
- "version": "0.0.7",
17
+ "version": "0.0.9",
18
18
  "dependencies": {
19
- "@esportsplus/crypto": "^0.0.2"
19
+ "@esportsplus/crypto": "^0.0.4"
20
20
  }
21
21
  }
package/src/item.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { rng } from '@esportsplus/crypto';
2
2
 
3
3
 
4
- const BASIS_POINTS = 10000;
4
+ let cache = new WeakMap();
5
5
 
6
6
 
7
7
  export default <T>(items: T[], weights?: number[]): T => {
@@ -13,21 +13,23 @@ export default <T>(items: T[], weights?: number[]): T => {
13
13
  throw new Error('Random: each item requires a weight');
14
14
  }
15
15
 
16
- let random = rng() * BASIS_POINTS,
17
- total = BASIS_POINTS;
16
+ let total = cache.get(weights) || 0;
18
17
 
19
- for (let i = 0, n = weights.length; i < n; i++) {
20
- total -= weights[i];
21
- }
18
+ if (!total) {
19
+ for (let i = 0, n = weights.length; i < n; i++) {
20
+ total += weights[i];
21
+ }
22
22
 
23
- if (total !== 0) {
24
- throw new Error('Random: weights use basis point math, they must add up to 10000 ( 100% )');
23
+ cache.set(weights, total);
25
24
  }
26
25
 
26
+ let current = 0,
27
+ random = rng() * total;
28
+
27
29
  for (let i = 0, n = items.length; i < n; i++) {
28
- total += weights[i];
30
+ current += weights[i];
29
31
 
30
- if (random <= total) {
32
+ if (random <= current) {
31
33
  return items[i];
32
34
  }
33
35
  }
package/tsconfig.json CHANGED
@@ -5,6 +5,6 @@
5
5
  "outDir": "build",
6
6
  },
7
7
  "exclude": ["node_modules"],
8
- "extends": "@esportsplus/rspack/tsconfig.base.json",
8
+ "extends": "@esportsplus/typescript/tsconfig.base.json",
9
9
  "include": ["src"]
10
10
  }