@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 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:
@@ -1,3 +1,3 @@
1
- import { createAleaFromByteSource } from "./internal/factories.js";
1
+ import { createAleaFromByteSource } from "../internal/factories.js";
2
2
  export * from "./common.js";
3
3
  export const cryptoAlea = createAleaFromByteSource(arr => globalThis.crypto.getRandomValues(arr));
@@ -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";
@@ -0,0 +1,3 @@
1
+ export { createAleaFromByteSource, createAleaFromSeed, createAleaFromFunc, } from "../internal/factories.js";
2
+ export { charsets } from "../internal/charsets.js";
3
+ export { alea } from "../internal/mathalea.js";
@@ -1,4 +1,4 @@
1
- import { createAleaFromByteSource } from "./internal/factories.js";
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 => {
@@ -1,4 +1,4 @@
1
- import { Alea } from "./internal/alea.js";
1
+ import { Alea } from "../internal/alea.js";
2
2
  export * from "./common.js";
3
3
  /**
4
4
  * An Alea instance that uses the runtime environment's `crypto` provider as a source
@@ -1,4 +1,4 @@
1
- import { Alea } from "./internal/alea.js";
1
+ import { Alea } from "../internal/alea.js";
2
2
  export * from "./common.js";
3
3
  /**
4
4
  * An Alea instance that uses the runtime environment's `crypto` provider as a source
@@ -80,10 +80,16 @@ export declare class Alea {
80
80
  };
81
81
  /**
82
82
  * Generate a sequence of bytes
83
- * @param count
83
+ * @param size Number of bytes to generate
84
84
  * @returns Random byte array
85
85
  */
86
- bytes(count: number): Uint8Array<ArrayBuffer>;
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, ((_, key) => {
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
- * Generate a sequence of bytes
147
- * @param count
148
- * @returns Random byte array
149
- */
150
- bytes(count) {
151
- const arr = new Uint8Array(count);
152
- for (let i = 0; i < count; i++)
153
- arr[i] = this.int(0, 255);
154
- return arr;
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, '0')).join('');
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
  }
@@ -0,0 +1,5 @@
1
+ import { Alea } from "./alea.js";
2
+ /**
3
+ * An Alea instance that uses Math.random() as a source
4
+ */
5
+ export declare const alea: Alea;
@@ -0,0 +1,5 @@
1
+ import { Alea } from "./alea.js";
2
+ /**
3
+ * An Alea instance that uses Math.random() as a source
4
+ */
5
+ export const alea = new Alea(Math.random);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xtia/alea-rc",
3
- "version": "0.0.5",
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