@xtia/alea-rc 0.0.5 → 0.0.7
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 +1 -1
- package/{browser.js → entry/browser.js} +1 -1
- package/entry/common.d.ts +5 -0
- package/entry/common.js +3 -0
- package/{node.js → entry/node.js} +1 -1
- package/{other.d.ts → entry/other.d.ts} +1 -1
- package/{other.js → entry/other.js} +1 -1
- package/internal/alea.d.ts +8 -2
- package/internal/alea.js +29 -18
- package/internal/mathalea.d.ts +5 -0
- package/internal/mathalea.js +5 -0
- package/package.json +10 -9
- package/common.d.ts +0 -8
- package/common.js +0 -7
- /package/{browser.d.ts → entry/browser.d.ts} +0 -0
- /package/{node.d.ts → entry/node.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ Alea is a utility wrapper for turning random numbers into useful values. Give it
|
|
|
7
7
|
* Fully typed
|
|
8
8
|
* Crypto-safe and seeded algorithms out-of-the-box
|
|
9
9
|
* No dependencies
|
|
10
|
-
* ~2.3kb minified core
|
|
10
|
+
* ~2.3kb minified core (1.04kb gzipped)
|
|
11
11
|
* Ranged int, array shuffling, dice roll, weighted sampling, phrase generation, UUID, bytes and many more
|
|
12
12
|
|
|
13
13
|
## Brief:
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Alea } from "../internal/alea.js";
|
|
2
|
+
export type { Alea };
|
|
3
|
+
export { createAleaFromByteSource, createAleaFromSeed, createAleaFromFunc, } from "../internal/factories.js";
|
|
4
|
+
export { charsets } from "../internal/charsets.js";
|
|
5
|
+
export { alea } from "../internal/mathalea.js";
|
package/entry/common.js
ADDED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createAleaFromByteSource } from "
|
|
1
|
+
import { createAleaFromByteSource } from "../internal/factories.js";
|
|
2
2
|
import { randomBytes } from 'node:crypto';
|
|
3
3
|
export * from "./common.js";
|
|
4
4
|
export const cryptoAlea = createAleaFromByteSource(arr => {
|
package/internal/alea.d.ts
CHANGED
|
@@ -80,10 +80,16 @@ export declare class Alea {
|
|
|
80
80
|
};
|
|
81
81
|
/**
|
|
82
82
|
* Generate a sequence of bytes
|
|
83
|
-
* @param
|
|
83
|
+
* @param size Number of bytes to generate
|
|
84
84
|
* @returns Random byte array
|
|
85
85
|
*/
|
|
86
|
-
bytes(
|
|
86
|
+
bytes(size: number): Uint8Array;
|
|
87
|
+
/**
|
|
88
|
+
* Fill a byte buffer with random bytes
|
|
89
|
+
* @param buffer Any TypedArray or DataView
|
|
90
|
+
* @returns The same buffer, filled
|
|
91
|
+
*/
|
|
92
|
+
bytes<T extends ArrayBufferView>(buffer: T): T;
|
|
87
93
|
/**
|
|
88
94
|
* Round a value up or down, according to probability defined by its non-integral part
|
|
89
95
|
* @example
|
package/internal/alea.js
CHANGED
|
@@ -80,7 +80,7 @@ export class Alea {
|
|
|
80
80
|
if (!charset || charset.length === 0)
|
|
81
81
|
throw new RangeError("charset must not be empty");
|
|
82
82
|
const chars = Array.from({ length }, () => charset[Math.floor(this.next() * charset.length)]);
|
|
83
|
-
return chars.join(
|
|
83
|
+
return chars.join("");
|
|
84
84
|
}
|
|
85
85
|
/**
|
|
86
86
|
* Generates a phrase from a table and a root string
|
|
@@ -98,17 +98,17 @@ export class Alea {
|
|
|
98
98
|
* @returns Generated phrase
|
|
99
99
|
*/
|
|
100
100
|
phrase(table, root) {
|
|
101
|
-
return root.replace(/\{([^}]+)\}/g, (
|
|
101
|
+
return root.replace(/\{([^}]+)\}/g, (_, key) => {
|
|
102
102
|
if (table[key] === undefined)
|
|
103
103
|
return `{${key}}`;
|
|
104
104
|
if (typeof table[key] == "function") {
|
|
105
|
-
return table[key](template => this.phrase(table, template));
|
|
105
|
+
return table[key]((template) => this.phrase(table, template));
|
|
106
106
|
}
|
|
107
107
|
const source = table[key];
|
|
108
108
|
if (typeof source == "string")
|
|
109
109
|
return this.phrase(table, source);
|
|
110
110
|
return this.phrase(table, this.sample(source));
|
|
111
|
-
})
|
|
111
|
+
});
|
|
112
112
|
}
|
|
113
113
|
/**
|
|
114
114
|
* Create a factory to pick random items from a biased list
|
|
@@ -116,7 +116,7 @@ export class Alea {
|
|
|
116
116
|
* @returns Random item factory
|
|
117
117
|
*/
|
|
118
118
|
createWeightedSampler(table) {
|
|
119
|
-
const filtered = table.filter(v => Number.isFinite(v[1]) && v[1] > 0);
|
|
119
|
+
const filtered = table.filter((v) => Number.isFinite(v[1]) && v[1] > 0);
|
|
120
120
|
if (filtered.length === 0) {
|
|
121
121
|
throw new Error("Weighted source has no viable candidates");
|
|
122
122
|
}
|
|
@@ -142,16 +142,27 @@ export class Alea {
|
|
|
142
142
|
},
|
|
143
143
|
};
|
|
144
144
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
145
|
+
bytes(sizeOrBuffer) {
|
|
146
|
+
let byteArray;
|
|
147
|
+
let result;
|
|
148
|
+
if (typeof sizeOrBuffer === "number") {
|
|
149
|
+
byteArray = new Uint8Array(sizeOrBuffer);
|
|
150
|
+
result = byteArray;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
byteArray = new Uint8Array(sizeOrBuffer.buffer, sizeOrBuffer.byteOffset, sizeOrBuffer.byteLength);
|
|
154
|
+
result = sizeOrBuffer;
|
|
155
|
+
}
|
|
156
|
+
const len = byteArray.length;
|
|
157
|
+
const words = Math.floor(len / 4);
|
|
158
|
+
const view = new DataView(byteArray.buffer, byteArray.byteOffset, byteArray.byteLength);
|
|
159
|
+
for (let i = 0; i < words; i++) {
|
|
160
|
+
view.setUint32(i * 4, this.int(0, 0xffffffff));
|
|
161
|
+
}
|
|
162
|
+
for (let i = words * 4; i < len; i++) {
|
|
163
|
+
byteArray[i] = this.int(0, 255);
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
155
166
|
}
|
|
156
167
|
/**
|
|
157
168
|
* Round a value up or down, according to probability defined by its non-integral part
|
|
@@ -219,13 +230,13 @@ export class Alea {
|
|
|
219
230
|
const bytes = this.bytes(16);
|
|
220
231
|
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
221
232
|
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
222
|
-
const hex = Array.from(bytes, byte => byte.toString(16).padStart(2,
|
|
233
|
+
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
223
234
|
return [
|
|
224
235
|
hex.slice(0, 8),
|
|
225
236
|
hex.slice(8, 12),
|
|
226
237
|
hex.slice(12, 16),
|
|
227
238
|
hex.slice(16, 20),
|
|
228
|
-
hex.slice(20, 32)
|
|
229
|
-
].join(
|
|
239
|
+
hex.slice(20, 32),
|
|
240
|
+
].join("-");
|
|
230
241
|
}
|
|
231
242
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xtia/alea-rc",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "RNG utilities",
|
|
5
5
|
"repository": {
|
|
6
6
|
"url": "https://github.com/tiadrop/alea",
|
|
@@ -11,19 +11,20 @@
|
|
|
11
11
|
"main": "./index.js",
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
|
-
"types": "./other.d.ts",
|
|
15
|
-
"browser": "./browser.js",
|
|
16
|
-
"workerd": "./browser.js",
|
|
17
|
-
"deno": "./browser.js",
|
|
18
|
-
"node": "./node.js",
|
|
19
|
-
"bun": "./node.js",
|
|
20
|
-
"default": "./other.js"
|
|
14
|
+
"types": "./entry/other.d.ts",
|
|
15
|
+
"browser": "./entry/browser.js",
|
|
16
|
+
"workerd": "./entry/browser.js",
|
|
17
|
+
"deno": "./entry/browser.js",
|
|
18
|
+
"node": "./entry/node.js",
|
|
19
|
+
"bun": "./entry/node.js",
|
|
20
|
+
"default": "./entry/other.js"
|
|
21
21
|
},
|
|
22
22
|
"./prng": {
|
|
23
23
|
"types": "./prng/index.d.ts",
|
|
24
24
|
"default": "./prng/index.js"
|
|
25
25
|
},
|
|
26
|
-
"./internal/*": null
|
|
26
|
+
"./internal/*": null,
|
|
27
|
+
"./entry/*": null
|
|
27
28
|
},
|
|
28
29
|
"scripts": {
|
|
29
30
|
"prepublishOnly": "cp ../README.md .",
|
package/common.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Alea } from "./internal/alea.js";
|
|
2
|
-
export type { Alea };
|
|
3
|
-
export { createAleaFromByteSource, createAleaFromSeed, createAleaFromFunc, } from "./internal/factories.js";
|
|
4
|
-
export { charsets } from "./internal/charsets.js";
|
|
5
|
-
/**
|
|
6
|
-
* An Alea instance that uses Math.random() as a source
|
|
7
|
-
*/
|
|
8
|
-
export declare const alea: Alea;
|
package/common.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { Alea } from "./internal/alea.js";
|
|
2
|
-
export { createAleaFromByteSource, createAleaFromSeed, createAleaFromFunc, } from "./internal/factories.js";
|
|
3
|
-
export { charsets } from "./internal/charsets.js";
|
|
4
|
-
/**
|
|
5
|
-
* An Alea instance that uses Math.random() as a source
|
|
6
|
-
*/
|
|
7
|
-
export const alea = new Alea(Math.random);
|
|
File without changes
|
|
File without changes
|