@xtia/alea-rc 0.0.11 → 0.0.12
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/README.md +6 -8
- package/entry/other.js +1 -1
- package/internal/alea.d.ts +1 -1
- package/internal/alea.js +1 -1
- package/internal/factories.d.ts +4 -4
- package/internal/factories.js +5 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,11 +6,10 @@ Alea is a utility wrapper for turning random numbers into useful values. Give it
|
|
|
6
6
|
|
|
7
7
|
* Fully typed
|
|
8
8
|
* Crypto-safe and seeded algorithms out-of-the-box
|
|
9
|
+
* Expressive: code with intent
|
|
9
10
|
* No dependencies
|
|
10
11
|
* ~2.4kb minified core
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
## Brief:
|
|
12
|
+
* Array shuffling, weighted sampling, recursive template phrase generation, UUID, bytes and many more
|
|
14
13
|
|
|
15
14
|
`npm i @xtia/alea` (pending release; use `@xtia/alea-rc` to preview)
|
|
16
15
|
|
|
@@ -18,14 +17,13 @@ Alea is a utility wrapper for turning random numbers into useful values. Give it
|
|
|
18
17
|
import { alea, cryptoAlea } from "@xtia/alea";
|
|
19
18
|
|
|
20
19
|
// generate values (driven by Math.random())
|
|
21
|
-
const damage = alea.
|
|
22
|
-
const duration = alea.between(1000, 1500);
|
|
20
|
+
const damage = alea.between(50, 150);
|
|
23
21
|
const loot = alea.chance(0.125) ? "epic" : "common";
|
|
24
22
|
const id = alea.string(5, "abcdef0123456789");
|
|
25
23
|
const npcName = alea.sample(["Alice", "Bob", "Charlie"]);
|
|
26
24
|
|
|
27
|
-
// secure source (driven by environment's crypto)
|
|
28
|
-
const
|
|
25
|
+
// same API, secure source (driven by environment's crypto)
|
|
26
|
+
const id = cryptoAlea.string(16, alphanumeric);
|
|
29
27
|
```
|
|
30
28
|
# Custom sources
|
|
31
29
|
|
|
@@ -64,5 +62,5 @@ const fast = mulberry32("my-seed");
|
|
|
64
62
|
const varied = sfc32(1, 2, 3, 4);
|
|
65
63
|
const strong = xoshiro128pp(5, 6, 7, 8);
|
|
66
64
|
|
|
67
|
-
const
|
|
65
|
+
const reproducibleId = varied.string(12, hexadecimal);
|
|
68
66
|
```
|
package/entry/other.js
CHANGED
|
@@ -4,5 +4,5 @@ export * from "./common.js";
|
|
|
4
4
|
* An Alea instance that uses the runtime environment's `crypto` provider as a source
|
|
5
5
|
*/
|
|
6
6
|
export const cryptoAlea = new Alea(() => {
|
|
7
|
-
throw new Error("cryptoAlea is not available in this environment. Consider using
|
|
7
|
+
throw new Error("cryptoAlea is not available in this environment. Consider using aleaFromByteSource() with your environment's crypto API.");
|
|
8
8
|
});
|
package/internal/alea.d.ts
CHANGED
|
@@ -56,7 +56,7 @@ export declare class Alea {
|
|
|
56
56
|
* greeting: ["hello", "hi", "{int} blessings"],
|
|
57
57
|
* addressee: ["world", "planet", "{adjective} @xtia user"],
|
|
58
58
|
* adjective: ["beautiful", "wonderful"],
|
|
59
|
-
* int: () => alea.
|
|
59
|
+
* int: () => Math.floor(alea.between(3, 9)).toString(),
|
|
60
60
|
* }, "{greeting}, {addressee}!")
|
|
61
61
|
* ```
|
|
62
62
|
* @param table
|
package/internal/alea.js
CHANGED
|
@@ -79,7 +79,7 @@ export class Alea {
|
|
|
79
79
|
* greeting: ["hello", "hi", "{int} blessings"],
|
|
80
80
|
* addressee: ["world", "planet", "{adjective} @xtia user"],
|
|
81
81
|
* adjective: ["beautiful", "wonderful"],
|
|
82
|
-
* int: () => alea.
|
|
82
|
+
* int: () => Math.floor(alea.between(3, 9)).toString(),
|
|
83
83
|
* }, "{greeting}, {addressee}!")
|
|
84
84
|
* ```
|
|
85
85
|
* @param table
|
package/internal/factories.d.ts
CHANGED
|
@@ -4,8 +4,8 @@ import { Alea } from "./alea.js";
|
|
|
4
4
|
* as a RNG source
|
|
5
5
|
* @example
|
|
6
6
|
* ```ts
|
|
7
|
-
* const cryptoAlea =
|
|
8
|
-
* const hwAlea =
|
|
7
|
+
* const cryptoAlea = aleaFromByteSource(crypto.getRandomValues);
|
|
8
|
+
* const hwAlea = aleaFromByteSource(hardwareRng.fillBytes);
|
|
9
9
|
* ```
|
|
10
10
|
* @param applyBytes A callback that fills a Uint8Array with random bytes
|
|
11
11
|
* @returns A byte generator-sourced Alea instance
|
|
@@ -25,8 +25,8 @@ export declare function aleaFromSeed(seed: number | string): Alea;
|
|
|
25
25
|
* Create an Alea instance using a custom function as an RNG source
|
|
26
26
|
* @example
|
|
27
27
|
* ```ts
|
|
28
|
-
* const basicAlea =
|
|
29
|
-
* const lcgAlea =
|
|
28
|
+
* const basicAlea = aleaFromFunc(Math.random);
|
|
29
|
+
* const lcgAlea = aleaFromFunc(customRng.next);
|
|
30
30
|
* ```
|
|
31
31
|
* @param fn Source RNG; a function that returns a value >= 0 and < 1
|
|
32
32
|
* @returns Custom function-sourced Alea instance
|
package/internal/factories.js
CHANGED
|
@@ -5,8 +5,8 @@ import { Alea } from "./alea.js";
|
|
|
5
5
|
* as a RNG source
|
|
6
6
|
* @example
|
|
7
7
|
* ```ts
|
|
8
|
-
* const cryptoAlea =
|
|
9
|
-
* const hwAlea =
|
|
8
|
+
* const cryptoAlea = aleaFromByteSource(crypto.getRandomValues);
|
|
9
|
+
* const hwAlea = aleaFromByteSource(hardwareRng.fillBytes);
|
|
10
10
|
* ```
|
|
11
11
|
* @param applyBytes A callback that fills a Uint8Array with random bytes
|
|
12
12
|
* @returns A byte generator-sourced Alea instance
|
|
@@ -36,8 +36,8 @@ export function aleaFromSeed(seed) {
|
|
|
36
36
|
* Create an Alea instance using a custom function as an RNG source
|
|
37
37
|
* @example
|
|
38
38
|
* ```ts
|
|
39
|
-
* const basicAlea =
|
|
40
|
-
* const lcgAlea =
|
|
39
|
+
* const basicAlea = aleaFromFunc(Math.random);
|
|
40
|
+
* const lcgAlea = aleaFromFunc(customRng.next);
|
|
41
41
|
* ```
|
|
42
42
|
* @param fn Source RNG; a function that returns a value >= 0 and < 1
|
|
43
43
|
* @returns Custom function-sourced Alea instance
|
|
@@ -45,4 +45,4 @@ export function aleaFromSeed(seed) {
|
|
|
45
45
|
export function aleaFromFunc(fn) {
|
|
46
46
|
return new Alea(fn);
|
|
47
47
|
}
|
|
48
|
-
// const xkcdAlea =
|
|
48
|
+
// const xkcdAlea = aleaFromFunc(() => 4/6); // decided by die roll
|