@guillaume-docquier/tools-ts 6.6.0 → 7.0.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 CHANGED
@@ -35,14 +35,15 @@ external APIs, CLI input, local storage, or other unknown data.
35
35
  | `getEnumKey` | Looks up the enum key associated with a value. | You have an enum value and need the more descriptive key for display, logs, or diagnostics. |
36
36
  | `asArray` | Normalizes a single value or array into an array. | An API accepts both `T` and `T[]`, but downstream logic should iterate uniformly. |
37
37
 
38
- ### Numeric Ranges
38
+ ### Numeric Utilities
39
39
 
40
- Use this tool when code needs a reusable, validated numeric interval instead of
41
- duplicating min/max checks across call sites.
40
+ Use these tools when code needs reusable numeric operations or validated
41
+ intervals.
42
42
 
43
- | Export | What it is | Use it when |
44
- | ------- | ---------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
45
- | `Range` | Helpers for creating, validating, comparing, and testing ranges. | You need finite integer or float intervals with inclusive minimums and configurable maximums. |
43
+ | Export | What it is | Use it when |
44
+ | -------- | ---------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
45
+ | `Scalar` | Helpers for scalar number operations. | You need to constrain a number to an inclusive minimum and maximum. |
46
+ | `Range` | Helpers for creating, validating, comparing, and testing ranges. | You need finite integer or float intervals with inclusive minimums and configurable maximums. |
46
47
 
47
48
  ### Sorting
48
49
 
@@ -59,8 +60,7 @@ pseudo-random sequences.
59
60
 
60
61
  | Export | What it is | Use it when |
61
62
  | ---------------- | -------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
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. |
63
+ | `Rng` | 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
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
65
  | `mulberry32Prng` | Deterministic seeded pseudo-random generator factory. | Tests, simulations, or generation code need repeatable pseudo-random sequences. |
66
66
 
@@ -0,0 +1,7 @@
1
+ export declare const Scalar: {
2
+ /**
3
+ * Restricts a number to the inclusive range between min and max.
4
+ * It will not validate that min < max.
5
+ */
6
+ clamp: (value: number, min: number, max: number) => number;
7
+ };
package/dist/Scalar.js ADDED
@@ -0,0 +1,10 @@
1
+ export const Scalar = {
2
+ /**
3
+ * Restricts a number to the inclusive range between min and max.
4
+ * It will not validate that min < max.
5
+ */
6
+ clamp: (value, min, max) => {
7
+ return Math.min(Math.max(value, min), max);
8
+ },
9
+ };
10
+ //# sourceMappingURL=Scalar.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Scalar.js","sourceRoot":"","sources":["../src/Scalar.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB;;;OAGG;IACH,KAAK,EAAE,CAAC,KAAa,EAAE,GAAW,EAAE,GAAW,EAAU,EAAE;QACzD,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,CAAA;IAC5C,CAAC;CACF,CAAA"}
@@ -11,6 +11,7 @@ export { Assert } from "./Assert.js";
11
11
  export { Result, type Success, type Failure } from "./Result.js";
12
12
  export { Profile } from "./Profile.js";
13
13
  export { Range } from "./Range.js";
14
+ export { Scalar } from "./Scalar.js";
14
15
  export { TypeGuard } from "./TypeGuard.js";
15
16
  export { setTimeoutAsync } from "./setTimeoutAsync.js";
16
17
  export { noop, asyncNoop } from "./noop.js";
@@ -25,7 +26,7 @@ export type { LoggerContext } from "./logging/log-context/LoggerContext.js";
25
26
  export type { LogContextProvider } from "./logging/log-context/LogContextProvider.js";
26
27
  export { debounce } from "./debounce.js";
27
28
  export { Sort } from "./Sort.js";
28
- export { createRng, type Rng, type Generator } from "./rng/rng.js";
29
+ export { Rng, type Generator } from "./rng/Rng.js";
29
30
  export { mulberry32Prng } from "./rng/mulberry32prng.js";
30
31
  export { Datetime } from "./Datetime.js";
31
32
  export { Angle, UnitOfAngle } from "./measurements/Angle.js";
@@ -10,6 +10,7 @@ export { Assert } from "./Assert.js";
10
10
  export { Result } from "./Result.js";
11
11
  export { Profile } from "./Profile.js";
12
12
  export { Range } from "./Range.js";
13
+ export { Scalar } from "./Scalar.js";
13
14
  export { TypeGuard } from "./TypeGuard.js";
14
15
  export { setTimeoutAsync } from "./setTimeoutAsync.js";
15
16
  export { noop, asyncNoop } from "./noop.js";
@@ -20,7 +21,7 @@ export { prettyConsoleFormatter } from "./logging/formatter/prettyConsoleFormatt
20
21
  export { createConsoleLogSink } from "./logging/sink/ConsoleLogSink.js";
21
22
  export { debounce } from "./debounce.js";
22
23
  export { Sort } from "./Sort.js";
23
- export { createRng } from "./rng/rng.js";
24
+ export { Rng } from "./rng/Rng.js";
24
25
  export { mulberry32Prng } from "./rng/mulberry32prng.js";
25
26
  export { Datetime } from "./Datetime.js";
26
27
  export { Angle, UnitOfAngle } from "./measurements/Angle.js";
@@ -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,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,MAAM,EAAE,MAA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E,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,GAAG,EAAkB,MAAM,cAAc,CAAA;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAC5D,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACzD,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AACrE,OAAO,EAAE,OAAO,EAAW,MAAM,2BAA2B,CAAA;AAC5D,OAAO,EAAE,OAAO,EAAY,MAAM,2BAA2B,CAAA;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAgB,OAAO,EAAE,MAAM,YAAY,CAAA"}
@@ -37,8 +37,10 @@ export type Rng = {
37
37
  */
38
38
  normal: (mean?: number, std?: number) => number;
39
39
  };
40
- /**
41
- * Creates a random number generator based on your generator of choice.
42
- * @param generator The generator that returns numbers in the range [0, 1)
43
- */
44
- export declare function createRng(generator: Generator): Rng;
40
+ export declare const Rng: {
41
+ /**
42
+ * Creates a random number generator based on your generator of choice.
43
+ * @param generator The generator that returns numbers in the range [0, 1)
44
+ */
45
+ create: (generator: Generator) => Rng;
46
+ };
@@ -0,0 +1,81 @@
1
+ import { boxMullerSample } from "./boxMullerSample.js";
2
+ const intBoundsOffset = {
3
+ inclusive: 1,
4
+ exclusive: 0,
5
+ };
6
+ export const Rng = {
7
+ /**
8
+ * Creates a random number generator based on your generator of choice.
9
+ * @param generator The generator that returns numbers in the range [0, 1)
10
+ */
11
+ create: (generator) => {
12
+ function random(range) {
13
+ switch (range.numericType) {
14
+ case "float":
15
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- The fact we have to cast like this sucks a little bit, we'll see if we have to do this often or not.
16
+ return float(range);
17
+ case "integer":
18
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- The fact we have to cast like this sucks a little bit, we'll see if we have to do this often or not.
19
+ return int(range);
20
+ }
21
+ }
22
+ function float(range) {
23
+ if (range === undefined) {
24
+ return generator();
25
+ }
26
+ // Because of floating point errors, this is inclusive when min > 1, but that's statistically insignificant.
27
+ return range.min + generator() * (range.max - range.min);
28
+ }
29
+ function int(range) {
30
+ // We +1 the max because float is exclusive of the max, but for int we want the range to be inclusive.
31
+ // We floor before the addition, because the addition can lose precision and generate numbers that are out of bounds when float ~= 1.
32
+ // We don't use Math.floor(float(rangeWithMaxPlus1)) for the same reason
33
+ return range.min + Math.floor(float() * (range.max + intBoundsOffset[range.maxBoundType] - range.min));
34
+ }
35
+ /**
36
+ * Fisher-Yates shuffle in place.
37
+ */
38
+ function shuffle(values) {
39
+ for (let i = values.length - 1; i > 0; i--) {
40
+ const j = Math.floor(generator() * (i + 1));
41
+ [values[i], values[j]] = [values[j], values[i]];
42
+ }
43
+ return values;
44
+ }
45
+ function draw(values, count) {
46
+ const shuffled = shuffle(values.slice());
47
+ return {
48
+ drawn: shuffled,
49
+ remaining: shuffled.splice(count),
50
+ };
51
+ }
52
+ /**
53
+ * box-muller outputs 2 numbers, so we keep the extra value that we can return instead of recomputing
54
+ */
55
+ let spareNormal;
56
+ function normal(mean = 0, std = 1) {
57
+ if (spareNormal !== undefined) {
58
+ const value = spareNormal;
59
+ spareNormal = undefined;
60
+ return mean + std * value;
61
+ }
62
+ // u1 must be non-zero
63
+ let u1;
64
+ do {
65
+ u1 = generator();
66
+ } while (u1 === 0);
67
+ const { z1, z2 } = boxMullerSample(u1, generator());
68
+ spareNormal = z1;
69
+ return mean + std * z2;
70
+ }
71
+ return {
72
+ random,
73
+ float,
74
+ int,
75
+ shuffle,
76
+ draw,
77
+ normal,
78
+ };
79
+ },
80
+ };
81
+ //# sourceMappingURL=Rng.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Rng.js","sourceRoot":"","sources":["../../src/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,4 @@
1
- import type { Generator } from "./rng.js";
1
+ import type { Generator } from "./Rng.js";
2
2
  /**
3
3
  * Creates a deterministic pseudo-random number generator based on a 32-bit integer seed using the mulberry32 algorithm.
4
4
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guillaume-docquier/tools-ts",
3
- "version": "6.6.0",
3
+ "version": "7.0.0",
4
4
  "description": "My personal collection of typescript tools",
5
5
  "homepage": "https://github.com/Guillaume-Docquier/tools-ts",
6
6
  "license": "MIT",
package/dist/rng/rng.js DELETED
@@ -1,79 +0,0 @@
1
- import { boxMullerSample } from "./boxMullerSample.js";
2
- const intBoundsOffset = {
3
- inclusive: 1,
4
- exclusive: 0,
5
- };
6
- /**
7
- * Creates a random number generator based on your generator of choice.
8
- * @param generator The generator that returns numbers in the range [0, 1)
9
- */
10
- export function createRng(generator) {
11
- function random(range) {
12
- switch (range.numericType) {
13
- case "float":
14
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- The fact we have to cast like this sucks a little bit, we'll see if we have to do this often or not.
15
- return float(range);
16
- case "integer":
17
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- The fact we have to cast like this sucks a little bit, we'll see if we have to do this often or not.
18
- return int(range);
19
- }
20
- }
21
- function float(range) {
22
- if (range === undefined) {
23
- return generator();
24
- }
25
- // Because of floating point errors, this is inclusive when min > 1, but that's statistically insignificant.
26
- return range.min + generator() * (range.max - range.min);
27
- }
28
- function int(range) {
29
- // We +1 the max because float is exclusive of the max, but for int we want the range to be inclusive.
30
- // We floor before the addition, because the addition can lose precision and generate numbers that are out of bounds when float ~= 1.
31
- // We don't use Math.floor(float(rangeWithMaxPlus1)) for the same reason
32
- return range.min + Math.floor(float() * (range.max + intBoundsOffset[range.maxBoundType] - range.min));
33
- }
34
- /**
35
- * Fisher-Yates shuffle in place.
36
- */
37
- function shuffle(values) {
38
- for (let i = values.length - 1; i > 0; i--) {
39
- const j = Math.floor(generator() * (i + 1));
40
- [values[i], values[j]] = [values[j], values[i]];
41
- }
42
- return values;
43
- }
44
- function draw(values, count) {
45
- const shuffled = shuffle(values.slice());
46
- return {
47
- drawn: shuffled,
48
- remaining: shuffled.splice(count),
49
- };
50
- }
51
- /**
52
- * box-muller outputs 2 numbers, so we keep the extra value that we can return instead of recomputing
53
- */
54
- let spareNormal;
55
- function normal(mean = 0, std = 1) {
56
- if (spareNormal !== undefined) {
57
- const value = spareNormal;
58
- spareNormal = undefined;
59
- return mean + std * value;
60
- }
61
- // u1 must be non-zero
62
- let u1;
63
- do {
64
- u1 = generator();
65
- } while (u1 === 0);
66
- const { z1, z2 } = boxMullerSample(u1, generator());
67
- spareNormal = z1;
68
- return mean + std * z2;
69
- }
70
- return {
71
- random,
72
- float,
73
- int,
74
- shuffle,
75
- draw,
76
- normal,
77
- };
78
- }
79
- //# sourceMappingURL=rng.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"rng.js","sourceRoot":"","sources":["../../src/rng/rng.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AA4CtD,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,uKAAuK;gBACvK,OAAO,KAAK,CAAC,KAAwC,CAAC,CAAA;YACxD,KAAK,SAAS;gBACZ,uKAAuK;gBACvK,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,S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