@guillaume-docquier/tools-ts 4.0.1 → 4.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/README.md +20 -0
- package/dist/Sort.d.ts +6 -0
- package/dist/Sort.js +9 -0
- package/dist/Sort.js.map +1 -0
- package/dist/entry.tools-ts.d.ts +3 -0
- package/dist/entry.tools-ts.js +3 -0
- package/dist/entry.tools-ts.js.map +1 -1
- package/dist/rng/mulberry32prng.d.ts +7 -0
- package/dist/rng/mulberry32prng.js +16 -0
- package/dist/rng/mulberry32prng.js.map +1 -0
- package/dist/rng/rng.d.ts +37 -0
- package/dist/rng/rng.js +58 -0
- package/dist/rng/rng.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -44,6 +44,26 @@ duplicating min/max checks across call sites.
|
|
|
44
44
|
| ------- | ---------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
|
|
45
45
|
| `Range` | Helpers for creating, validating, comparing, and testing ranges. | You need finite integer or float intervals with inclusive minimums and configurable maximums. |
|
|
46
46
|
|
|
47
|
+
### Sorting
|
|
48
|
+
|
|
49
|
+
Use this tool when code needs shared ordering helpers.
|
|
50
|
+
|
|
51
|
+
| Export | What it is | Use it when |
|
|
52
|
+
| ------ | ---------------------------------------------- | --------------------------------------------------------------------------------------- |
|
|
53
|
+
| `Sort` | Comparator helpers for common sort operations. | You want a shared sorting utility instead of rewriting compare callbacks at call sites. |
|
|
54
|
+
|
|
55
|
+
### Randomness
|
|
56
|
+
|
|
57
|
+
Use these tools when code needs injectable randomness or deterministic
|
|
58
|
+
pseudo-random sequences.
|
|
59
|
+
|
|
60
|
+
| Export | What it is | Use it when |
|
|
61
|
+
| ---------------- | -------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
|
|
62
|
+
| `Rng` | Type for the RNG facade returned by `createRng`. | A function accepts or stores the package RNG abstraction. |
|
|
63
|
+
| `createRng` | Builds an RNG facade from a generator that returns values in [0, 1). | You need range-aware random numbers, shuffling, or draws backed by injectable randomness. |
|
|
64
|
+
| `Generator` | Type for raw generators that return values in [0, 1). | You accept or provide the primitive random source used to construct an `Rng`. |
|
|
65
|
+
| `mulberry32Prng` | Deterministic seeded pseudo-random generator factory. | Tests, simulations, or generation code need repeatable pseudo-random sequences. |
|
|
66
|
+
|
|
47
67
|
### Utility Types
|
|
48
68
|
|
|
49
69
|
These exports are type-only helpers for common TypeScript modeling problems.
|
package/dist/Sort.d.ts
ADDED
package/dist/Sort.js
ADDED
package/dist/Sort.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Sort.js","sourceRoot":"","sources":["../src/Sort.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,IAAI,GAAG;IAClB;;OAEG;IACH,WAAW,EAAE,CAAC,CAAS,EAAE,CAAS,EAAU,EAAE;QAC5C,OAAO,CAAC,GAAG,CAAC,CAAA;IACd,CAAC;CACF,CAAA"}
|
package/dist/entry.tools-ts.d.ts
CHANGED
|
@@ -23,3 +23,6 @@ export type { LogContext } from "./logging/log-context/LogContext.js";
|
|
|
23
23
|
export type { LoggerContext } from "./logging/log-context/LoggerContext.js";
|
|
24
24
|
export type { LogContextProvider } from "./logging/log-context/LogContextProvider.js";
|
|
25
25
|
export { debounce } from "./debounce.js";
|
|
26
|
+
export { Sort } from "./Sort.js";
|
|
27
|
+
export { createRng, type Rng, type Generator } from "./rng/rng.js";
|
|
28
|
+
export { mulberry32Prng } from "./rng/mulberry32prng.js";
|
package/dist/entry.tools-ts.js
CHANGED
|
@@ -18,4 +18,7 @@ export { jsonLineFormatter } from "./logging/formatter/jsonLineFormatter.js";
|
|
|
18
18
|
export { prettyConsoleFormatter } from "./logging/formatter/prettyConsoleFormatter.js";
|
|
19
19
|
export { createConsoleLogSink } from "./logging/sink/ConsoleLogSink.js";
|
|
20
20
|
export { debounce } from "./debounce.js";
|
|
21
|
+
export { Sort } from "./Sort.js";
|
|
22
|
+
export { createRng } from "./rng/rng.js";
|
|
23
|
+
export { mulberry32Prng } from "./rng/mulberry32prng.js";
|
|
21
24
|
//# sourceMappingURL=entry.tools-ts.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entry.tools-ts.js","sourceRoot":"","sources":["../src/entry.tools-ts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACnD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAA;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAI5C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,MAAM,EAA8B,MAAM,aAAa,CAAA;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAE3C,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAC5C,OAAO,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAA;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAA;AAC5E,OAAO,EAAE,sBAAsB,EAAE,MAAM,+CAA+C,CAAA;AAEtF,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAA;AAKvE,OAAO,EAAE,QAAQ,EAAE,MAAM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ClE,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Generator } from "./rng.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a deterministic pseudo-random number generator based on a 32-bit integer seed using the mulberry32 algorithm.
|
|
4
|
+
*
|
|
5
|
+
* It generates floats in the range [0, 1)
|
|
6
|
+
*/
|
|
7
|
+
export declare function mulberry32Prng(seed: number): Generator;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a deterministic pseudo-random number generator based on a 32-bit integer seed using the mulberry32 algorithm.
|
|
3
|
+
*
|
|
4
|
+
* It generates floats in the range [0, 1)
|
|
5
|
+
*/
|
|
6
|
+
export function mulberry32Prng(seed) {
|
|
7
|
+
let state = seed >>> 0;
|
|
8
|
+
return () => {
|
|
9
|
+
state += 0x6d2b79f5;
|
|
10
|
+
let next = state;
|
|
11
|
+
next = Math.imul(next ^ (next >>> 15), next | 1);
|
|
12
|
+
next ^= next + Math.imul(next ^ (next >>> 7), next | 61);
|
|
13
|
+
return ((next ^ (next >>> 14)) >>> 0) / 4_294_967_296;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=mulberry32prng.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mulberry32prng.js","sourceRoot":"","sources":["../../src/rng/mulberry32prng.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,IAAI,KAAK,GAAG,IAAI,KAAK,CAAC,CAAA;IAEtB,OAAO,GAAG,EAAE;QACV,KAAK,IAAI,UAAU,CAAA;QAEnB,IAAI,IAAI,GAAG,KAAK,CAAA;QAChB,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,KAAK,EAAE,CAAC,EAAE,IAAI,GAAG,CAAC,CAAC,CAAA;QAChD,IAAI,IAAI,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC,CAAC,EAAE,IAAI,GAAG,EAAE,CAAC,CAAA;QAExD,OAAO,CAAC,CAAC,IAAI,GAAG,CAAC,IAAI,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,aAAa,CAAA;IACvD,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Range } from "../Range.js";
|
|
2
|
+
/**
|
|
3
|
+
* A generator that returns numbers in the range [0, 1)
|
|
4
|
+
*/
|
|
5
|
+
export type Generator = () => number;
|
|
6
|
+
export type Rng = {
|
|
7
|
+
/**
|
|
8
|
+
* Generates a random number based on the given Range.
|
|
9
|
+
* It will output an int given an int Range, or a float given a float Range.
|
|
10
|
+
*/
|
|
11
|
+
random: (range: Range) => number;
|
|
12
|
+
/**
|
|
13
|
+
* Generates a float in the float Range.
|
|
14
|
+
* When no range is provided, returns a float in the range [0, 1).
|
|
15
|
+
*/
|
|
16
|
+
float: (range?: Range<"float">) => number;
|
|
17
|
+
/**
|
|
18
|
+
* Generates an int in the int Range.
|
|
19
|
+
*/
|
|
20
|
+
int: (range: Range<"integer">) => number;
|
|
21
|
+
/**
|
|
22
|
+
* Shuffles an array in place.
|
|
23
|
+
*/
|
|
24
|
+
shuffle: <T>(values: T[]) => T[];
|
|
25
|
+
/**
|
|
26
|
+
* Draws a number of elements without modifying the original array.
|
|
27
|
+
*/
|
|
28
|
+
draw: <T>(values: T[], count: number) => {
|
|
29
|
+
drawn: T[];
|
|
30
|
+
remaining: T[];
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Creates a random number generator based on your generator of choice.
|
|
35
|
+
* @param generator The generator that returns numbers in the range [0, 1)
|
|
36
|
+
*/
|
|
37
|
+
export declare function createRng(generator: Generator): Rng;
|
package/dist/rng/rng.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const intBoundsOffset = {
|
|
2
|
+
inclusive: 1,
|
|
3
|
+
exclusive: 0,
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Creates a random number generator based on your generator of choice.
|
|
7
|
+
* @param generator The generator that returns numbers in the range [0, 1)
|
|
8
|
+
*/
|
|
9
|
+
export function createRng(generator) {
|
|
10
|
+
function random(range) {
|
|
11
|
+
switch (range.numericType) {
|
|
12
|
+
case "float":
|
|
13
|
+
// The fact we have to cast like this sucks a little bit, we'll see if we have to do this often or not.
|
|
14
|
+
return float(range);
|
|
15
|
+
case "integer":
|
|
16
|
+
// The fact we have to cast like this sucks a little bit, we'll see if we have to do this often or not.
|
|
17
|
+
return int(range);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function float(range) {
|
|
21
|
+
if (range === undefined) {
|
|
22
|
+
return generator();
|
|
23
|
+
}
|
|
24
|
+
// Because of floating point errors, this is inclusive when min > 1, but that's statistically insignificant.
|
|
25
|
+
return range.min + generator() * (range.max - range.min);
|
|
26
|
+
}
|
|
27
|
+
function int(range) {
|
|
28
|
+
// We +1 the max because float is exclusive of the max, but for int we want the range to be inclusive.
|
|
29
|
+
// We floor before the addition, because the addition can lose precision and generate numbers that are out of bounds when float ~= 1.
|
|
30
|
+
// We don't use Math.floor(float(rangeWithMaxPlus1)) for the same reason
|
|
31
|
+
return range.min + Math.floor(float() * (range.max + intBoundsOffset[range.maxBoundType] - range.min));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Fisher-Yates shuffle in place.
|
|
35
|
+
*/
|
|
36
|
+
function shuffle(values) {
|
|
37
|
+
for (let i = values.length - 1; i > 0; i--) {
|
|
38
|
+
const j = Math.floor(generator() * (i + 1));
|
|
39
|
+
[values[i], values[j]] = [values[j], values[i]];
|
|
40
|
+
}
|
|
41
|
+
return values;
|
|
42
|
+
}
|
|
43
|
+
function draw(values, count) {
|
|
44
|
+
const shuffled = shuffle(values.slice());
|
|
45
|
+
return {
|
|
46
|
+
drawn: shuffled,
|
|
47
|
+
remaining: shuffled.splice(count),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
random,
|
|
52
|
+
float,
|
|
53
|
+
int,
|
|
54
|
+
shuffle,
|
|
55
|
+
draw,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=rng.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rng.js","sourceRoot":"","sources":["../../src/rng/rng.ts"],"names":[],"mappings":"AAoCA,MAAM,eAAe,GAAG;IACtB,SAAS,EAAE,CAAC;IACZ,SAAS,EAAE,CAAC;CAC4C,CAAA;AAE1D;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,SAAoB;IAC5C,SAAS,MAAM,CAAC,KAAY;QAC1B,QAAQ,KAAK,CAAC,WAAW,EAAE,CAAC;YAC1B,KAAK,OAAO;gBACV,uGAAuG;gBACvG,OAAO,KAAK,CAAC,KAAwC,CAAC,CAAA;YACxD,KAAK,SAAS;gBACZ,uGAAuG;gBACvG,OAAO,GAAG,CAAC,KAAwC,CAAC,CAAA;QACxD,CAAC;IACH,CAAC;IAED,SAAS,KAAK,CAAC,KAAsB;QACnC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO,SAAS,EAAE,CAAA;QACpB,CAAC;QAED,4GAA4G;QAC5G,OAAO,KAAK,CAAC,GAAG,GAAG,SAAS,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,CAAA;IAC1D,CAAC;IAED,SAAS,GAAG,CAAC,KAAuB;QAClC,sGAAsG;QACtG,qIAAqI;QACrI,wEAAwE;QACxE,OAAO,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,GAAG,eAAe,CAAC,KAAK,CAAC,YAAY,CAAC,GAAG,KAAK,CAAC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