@ghostry/fabricator 0.0.13 → 0.0.14
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/dist/esm/Bound.js +74 -1
- package/dist/esm/Distribution/index.js +103 -2
- package/dist/esm/Enumeration/Enumerate.js +10 -5
- package/dist/esm/Enumeration/Plan.js +2 -2
- package/dist/esm/Error/index.js +96 -27
- package/dist/esm/Fabricator/Constructor.js +7 -5
- package/dist/esm/Instance/Core.js +3 -0
- package/dist/esm/Primitive/array/Admits.js +18 -0
- package/dist/esm/Primitive/array/Fabricator.js +20 -2
- package/dist/esm/Primitive/array/Registry.js +32 -0
- package/dist/esm/Primitive/array/Schema.js +8 -0
- package/dist/esm/Primitive/bigint/Admits.js +11 -0
- package/dist/esm/Primitive/bigint/Fabricator.js +16 -23
- package/dist/esm/Primitive/bigint/Registry.js +17 -6
- package/dist/esm/Primitive/bigint/Schema.js +8 -0
- package/dist/esm/Primitive/date/Admits.js +22 -0
- package/dist/esm/Primitive/date/Defaults.js +27 -1
- package/dist/esm/Primitive/date/Fabricator.js +32 -34
- package/dist/esm/Primitive/date/Registry.js +80 -13
- package/dist/esm/Primitive/date/Schema.js +8 -0
- package/dist/esm/Primitive/enum/Fabricator.js +14 -2
- package/dist/esm/Primitive/enum/Registry.js +38 -25
- package/dist/esm/Primitive/index.js +1 -0
- package/dist/esm/Primitive/number/Admits.js +27 -0
- package/dist/esm/Primitive/number/Fabricator.js +18 -9
- package/dist/esm/Primitive/number/Registry.js +55 -4
- package/dist/esm/Primitive/number/Schema.js +10 -3
- package/dist/esm/Primitive/object/Fabricator.js +2 -0
- package/dist/esm/Primitive/record/Admits.js +42 -0
- package/dist/esm/Primitive/record/Fabricator.js +49 -19
- package/dist/esm/Primitive/record/Registry.js +29 -0
- package/dist/esm/Primitive/record/Schema.js +8 -0
- package/dist/esm/Primitive/recursive/Fabricator.js +3 -2
- package/dist/esm/Primitive/recursive/Terminate.js +16 -2
- package/dist/esm/Primitive/string/Admits.js +26 -0
- package/dist/esm/Primitive/string/Capacity.js +81 -0
- package/dist/esm/Primitive/string/Fabricator.js +23 -43
- package/dist/esm/Primitive/string/Registry.js +31 -9
- package/dist/esm/Primitive/string/Schema.js +10 -3
- package/dist/esm/Primitive/string/Sources.js +41 -0
- package/dist/esm/Primitive/unique/Registry.js +22 -0
- package/dist/esm/Primitive/unique/index.js +2 -0
- package/dist/esm/Types.js +2 -1
- package/dist/esm/Unique/Capacity.js +85 -0
- package/dist/esm/Unique/Distinct.js +187 -0
- package/dist/esm/Unique/Key.js +70 -0
- package/dist/esm/Unique/Override.js +23 -0
- package/dist/esm/Unique/Strategy.js +173 -0
- package/dist/esm/internal.js +1 -0
- package/dist/types/Bound.d.ts +66 -0
- package/dist/types/Distribution/index.d.ts +82 -0
- package/dist/types/Enumeration/Types.d.ts +10 -1
- package/dist/types/Error/index.d.ts +283 -0
- package/dist/types/Fabricator/Constructor.d.ts +6 -2
- package/dist/types/Fabricator/Types.d.ts +13 -0
- package/dist/types/Instance/Types.d.ts +14 -0
- package/dist/types/Primitive/array/Admits.d.ts +7 -0
- package/dist/types/Primitive/array/Fabricator.d.ts +2 -2
- package/dist/types/Primitive/array/Registry.d.ts +38 -1
- package/dist/types/Primitive/array/Schema.d.ts +6 -1
- package/dist/types/Primitive/array/Types.d.ts +33 -1
- package/dist/types/Primitive/bigint/Admits.d.ts +7 -0
- package/dist/types/Primitive/bigint/Fabricator.d.ts +3 -1
- package/dist/types/Primitive/bigint/Registry.d.ts +20 -0
- package/dist/types/Primitive/bigint/Schema.d.ts +9 -1
- package/dist/types/Primitive/bigint/Types.d.ts +27 -3
- package/dist/types/Primitive/date/Admits.d.ts +7 -0
- package/dist/types/Primitive/date/Defaults.d.ts +22 -1
- package/dist/types/Primitive/date/Fabricator.d.ts +3 -1
- package/dist/types/Primitive/date/Registry.d.ts +47 -11
- package/dist/types/Primitive/date/Schema.d.ts +10 -1
- package/dist/types/Primitive/date/Types.d.ts +53 -4
- package/dist/types/Primitive/enum/Fabricator.d.ts +3 -1
- package/dist/types/Primitive/enum/Registry.d.ts +20 -0
- package/dist/types/Primitive/enum/Types.d.ts +5 -0
- package/dist/types/Primitive/index.d.ts +1 -0
- package/dist/types/Primitive/number/Admits.d.ts +9 -0
- package/dist/types/Primitive/number/Fabricator.d.ts +3 -1
- package/dist/types/Primitive/number/Registry.d.ts +42 -1
- package/dist/types/Primitive/number/Schema.d.ts +10 -2
- package/dist/types/Primitive/number/Types.d.ts +32 -10
- package/dist/types/Primitive/record/Admits.d.ts +18 -0
- package/dist/types/Primitive/record/Fabricator.d.ts +2 -2
- package/dist/types/Primitive/record/Registry.d.ts +18 -1
- package/dist/types/Primitive/record/Schema.d.ts +8 -1
- package/dist/types/Primitive/record/Types.d.ts +60 -6
- package/dist/types/Primitive/recursive/Terminate.d.ts +7 -0
- package/dist/types/Primitive/string/Admits.d.ts +8 -0
- package/dist/types/Primitive/string/Capacity.d.ts +36 -0
- package/dist/types/Primitive/string/Fabricator.d.ts +3 -1
- package/dist/types/Primitive/string/Registry.d.ts +26 -5
- package/dist/types/Primitive/string/Schema.d.ts +28 -2
- package/dist/types/Primitive/string/Sources.d.ts +16 -0
- package/dist/types/Primitive/string/Types.d.ts +35 -15
- package/dist/types/Primitive/unique/Registry.d.ts +39 -0
- package/dist/types/Primitive/unique/index.d.ts +2 -0
- package/dist/types/Types.d.ts +9 -0
- package/dist/types/Unique/Capacity.d.ts +29 -0
- package/dist/types/Unique/Distinct.d.ts +95 -0
- package/dist/types/Unique/Key.d.ts +29 -0
- package/dist/types/Unique/Override.d.ts +16 -0
- package/dist/types/Unique/Strategy.d.ts +135 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/types/internal.d.ts +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { bigintPermutation, isDrawable, permutation, withoutReplacement } from "../Distribution/index.js";
|
|
2
|
+
import { FabricatorError } from "../Error/index.js";
|
|
3
|
+
import { Claim } from "../Types.js";
|
|
4
|
+
import { toKeyer } from "./Key.js";
|
|
5
|
+
const FLOOR = 100;
|
|
6
|
+
const FACTOR = 20;
|
|
7
|
+
function defaultRedraws(produced) {
|
|
8
|
+
return Math.max(FLOOR, FACTOR * produced);
|
|
9
|
+
}
|
|
10
|
+
function toRedraws(label, redraws) {
|
|
11
|
+
if (void 0 === redraws) return;
|
|
12
|
+
if (Number.isSafeInteger(redraws) && redraws >= 0) return redraws;
|
|
13
|
+
throw new FabricatorError.InvalidRedrawsError(label, redraws);
|
|
14
|
+
}
|
|
15
|
+
function exactValues(kind, entries, stream) {
|
|
16
|
+
const { keyer, values } = toDistinctValues(entries);
|
|
17
|
+
const capacity = BigInt(values.length);
|
|
18
|
+
return {
|
|
19
|
+
capacity,
|
|
20
|
+
open: ()=>{
|
|
21
|
+
const pool = withoutReplacement(values, stream);
|
|
22
|
+
return {
|
|
23
|
+
draw: ()=>{
|
|
24
|
+
if (0 === pool.remaining) throw new FabricatorError.UniqueExhaustedError(kind, capacity);
|
|
25
|
+
return pool.draw().value;
|
|
26
|
+
},
|
|
27
|
+
claim: (value)=>{
|
|
28
|
+
const key = keyer(value);
|
|
29
|
+
pool.discard((entry)=>entry.key === key);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function toDistinctValues(entries) {
|
|
36
|
+
const keyer = toKeyer();
|
|
37
|
+
const merged = new Map();
|
|
38
|
+
for (const [weight, value] of entries){
|
|
39
|
+
if (!isDrawable(weight)) continue;
|
|
40
|
+
const key = keyer(value);
|
|
41
|
+
const known = merged.get(key);
|
|
42
|
+
if (known) known[0] += weight;
|
|
43
|
+
else merged.set(key, [
|
|
44
|
+
weight,
|
|
45
|
+
{
|
|
46
|
+
key,
|
|
47
|
+
value
|
|
48
|
+
}
|
|
49
|
+
]);
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
keyer,
|
|
53
|
+
values: [
|
|
54
|
+
...merged.values()
|
|
55
|
+
]
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function exactRange(kind, range, stream) {
|
|
59
|
+
const capacity = BigInt(range.max - range.min + 1);
|
|
60
|
+
return {
|
|
61
|
+
capacity,
|
|
62
|
+
open: ()=>{
|
|
63
|
+
const order = permutation(range, stream);
|
|
64
|
+
const claimed = new Set();
|
|
65
|
+
return {
|
|
66
|
+
draw: ()=>{
|
|
67
|
+
while(order.remaining > 0){
|
|
68
|
+
const value = order.next();
|
|
69
|
+
if (!claimed.delete(value)) return value;
|
|
70
|
+
}
|
|
71
|
+
throw new FabricatorError.UniqueExhaustedError(kind, capacity);
|
|
72
|
+
},
|
|
73
|
+
claim: (value)=>{
|
|
74
|
+
if ("number" == typeof value && Number.isInteger(value) && value >= range.min && value <= range.max) claimed.add(value);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function exactBigintRange(kind, range, stream) {
|
|
81
|
+
const capacity = range.max - range.min + BigInt(1);
|
|
82
|
+
return {
|
|
83
|
+
capacity,
|
|
84
|
+
open: ()=>{
|
|
85
|
+
const order = bigintPermutation(range, stream);
|
|
86
|
+
const claimed = new Set();
|
|
87
|
+
return {
|
|
88
|
+
draw: ()=>{
|
|
89
|
+
while(order.remaining > BigInt(0)){
|
|
90
|
+
const value = order.next();
|
|
91
|
+
if (!claimed.delete(value)) return value;
|
|
92
|
+
}
|
|
93
|
+
throw new FabricatorError.UniqueExhaustedError(kind, capacity);
|
|
94
|
+
},
|
|
95
|
+
claim: (value)=>{
|
|
96
|
+
if ("bigint" == typeof value && value >= range.min && value <= range.max) claimed.add(value);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function tracked(options) {
|
|
103
|
+
const { label, kind, draw, key, capacity, admits, redraws } = options;
|
|
104
|
+
return {
|
|
105
|
+
capacity,
|
|
106
|
+
open: ()=>{
|
|
107
|
+
const seen = new Set();
|
|
108
|
+
return {
|
|
109
|
+
draw: ()=>{
|
|
110
|
+
if (void 0 !== capacity && BigInt(seen.size) >= capacity) throw new FabricatorError.UniqueExhaustedError(kind, capacity);
|
|
111
|
+
const limit = null != redraws ? redraws : defaultRedraws(seen.size);
|
|
112
|
+
for(let redraw = 0; redraw <= limit; redraw++){
|
|
113
|
+
const value = draw();
|
|
114
|
+
const k = key(value);
|
|
115
|
+
if (!seen.has(k)) {
|
|
116
|
+
seen.add(k);
|
|
117
|
+
return value;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
throw new FabricatorError.UniqueRedrawsError(label, limit, seen.size);
|
|
121
|
+
},
|
|
122
|
+
claim: (value)=>{
|
|
123
|
+
if (admits && !admits(value)) return;
|
|
124
|
+
seen.add(key(value));
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function trackedByKey(options) {
|
|
131
|
+
const { by, ...rest } = options;
|
|
132
|
+
const keyer = toKeyer();
|
|
133
|
+
return tracked({
|
|
134
|
+
...rest,
|
|
135
|
+
key: by ? (value)=>keyer(by(value)) : keyer
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function withinCollection(collection, member) {
|
|
139
|
+
const fabricate = ()=>member.fabricate();
|
|
140
|
+
if (!(Claim in member)) return fabricate;
|
|
141
|
+
return ()=>{
|
|
142
|
+
try {
|
|
143
|
+
return fabricate();
|
|
144
|
+
} catch (error) {
|
|
145
|
+
throw toWithin(error, collection);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function toWithin(error, collection) {
|
|
150
|
+
if (error instanceof FabricatorError.UniqueExhaustedError) return new FabricatorError.UniqueExhaustedError(error.kind, error.capacity, collection);
|
|
151
|
+
if (error instanceof FabricatorError.UniqueRedrawsError) return new FabricatorError.UniqueRedrawsError(error.label, error.redraws, error.produced, collection);
|
|
152
|
+
return error;
|
|
153
|
+
}
|
|
154
|
+
function toLifetimeScope(strategy, exhaustion) {
|
|
155
|
+
if ("throw" === exhaustion) return strategy.open();
|
|
156
|
+
let scope = strategy.open();
|
|
157
|
+
return {
|
|
158
|
+
draw: ()=>{
|
|
159
|
+
try {
|
|
160
|
+
return scope.draw();
|
|
161
|
+
} catch (error) {
|
|
162
|
+
if (!isExhaustion(error)) throw error;
|
|
163
|
+
scope = strategy.open();
|
|
164
|
+
return scope.draw();
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
claim: (value)=>scope.claim(value)
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
function isExhaustion(error) {
|
|
171
|
+
return error instanceof FabricatorError.UniqueExhaustedError || error instanceof FabricatorError.UniqueRedrawsError;
|
|
172
|
+
}
|
|
173
|
+
export { defaultRedraws, exactBigintRange, exactRange, exactValues, toDistinctValues, toLifetimeScope, toRedraws, tracked, trackedByKey, withinCollection };
|
package/dist/esm/internal.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { Children, Kind, Meta, Produces } from "./Types.js";
|
|
2
2
|
export { isArray, isPlainObject } from "./Utility/Core.js";
|
|
3
3
|
export { isBuildable, isSchema } from "./Schema/Core.js";
|
|
4
|
+
export { toKeyer } from "./Unique/Key.js";
|
|
4
5
|
export { plan, resolve } from "./Enumeration/Plan.js";
|
|
5
6
|
export { defaultAlgorithm, encode, randomSalt, toRandomSource, toStream, toStreamFromTrace } from "./Random/index.js";
|
|
6
7
|
export { normalCdf, normalInv, weighted } from "./Distribution/index.js";
|
package/dist/types/Bound.d.ts
CHANGED
|
@@ -55,6 +55,21 @@ export declare function towardInterior(value: number, toward: number): number;
|
|
|
55
55
|
* distribution's mass on the bound would otherwise loop.
|
|
56
56
|
*/
|
|
57
57
|
export declare function constrainContinuous(next: () => number, min: Bound<number>, max: Bound<number>): () => number;
|
|
58
|
+
/**
|
|
59
|
+
* A `bigint` end that may be written as a `number`, so a call site needs no
|
|
60
|
+
* `BigInt(...)` where `5n` does not compile. A `number` must be a safe integer:
|
|
61
|
+
* beyond ±2^53 a literal has already rounded before it arrives, so the exact
|
|
62
|
+
* value is only expressible as a `bigint`.
|
|
63
|
+
*/
|
|
64
|
+
export declare function toBigintBound(label: string, end: "min" | "max", input: InputBound<bigint | number>): Bound<bigint>;
|
|
65
|
+
/**
|
|
66
|
+
* A `Date` end that may be written as epoch milliseconds, as `initialize({
|
|
67
|
+
* clock })` accepts. A `number` must be one a `Date` holds unchanged — the test
|
|
68
|
+
* `Instance/Core.ts`'s `resolveInstant` applies — so a fractional, non-finite,
|
|
69
|
+
* or out-of-range number throws rather than being adjusted the way `Date`'s
|
|
70
|
+
* `TimeClip` would.
|
|
71
|
+
*/
|
|
72
|
+
export declare function toDateBound(label: string, end: "min" | "max", input: InputBound<Date | number>): Bound<Date>;
|
|
58
73
|
export declare function epochBound(bound: Bound<Date>): Bound<number>;
|
|
59
74
|
/**
|
|
60
75
|
* Call-site `length` for `string` and `array`: a bare number is an exact count,
|
|
@@ -82,3 +97,54 @@ export type Length = {
|
|
|
82
97
|
* matching `date/Registry.ts`'s `toWhereby`, so stored meta stays minimal.
|
|
83
98
|
*/
|
|
84
99
|
export declare function toLengthRange(length: InputLength, label: string): Length;
|
|
100
|
+
/**
|
|
101
|
+
* The ends an adapter states for a schema, as `.admits(...)` stores them. An
|
|
102
|
+
* absent end states no limit. Also the shape of a schema's fabricable range,
|
|
103
|
+
* where an absent end is one drawn against the kind's unbounded cap.
|
|
104
|
+
*/
|
|
105
|
+
export type AdmittedEnds<$T> = {
|
|
106
|
+
min?: Bound<$T> | undefined;
|
|
107
|
+
max?: Bound<$T> | undefined;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* What `.admits({ min, max })` accepts: an end as `whereby` takes it, or
|
|
111
|
+
* `-Infinity`/`Infinity` (bare, or as a `Bound`'s value) for no limit. An
|
|
112
|
+
* omitted end keeps what is admitted already.
|
|
113
|
+
*/
|
|
114
|
+
export type InputAdmittedEnds<$T> = {
|
|
115
|
+
min?: InputBound<$T> | undefined;
|
|
116
|
+
max?: InputBound<$T> | undefined;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Whether every value `inner` admits at one end is one `outer` admits too. Each
|
|
120
|
+
* kind's notion of "admits" differs — a whole-number end excludes by `±1`, a
|
|
121
|
+
* continuous end by its endpoint alone — so each passes its own.
|
|
122
|
+
*/
|
|
123
|
+
export type Within<$T> = (inner: Bound<$T>, outer: Bound<$T>, end: "min" | "max") => boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Continuous ends: a farther value, or the same value with `inner` no less
|
|
126
|
+
* exclusive than `outer`.
|
|
127
|
+
*/
|
|
128
|
+
export declare const continuousWithin: Within<number>;
|
|
129
|
+
/** Whole-number ends, compared as the inclusive integers they leave. */
|
|
130
|
+
export declare const discreteWithin: Within<number>;
|
|
131
|
+
export declare const bigintWithin: Within<bigint>;
|
|
132
|
+
/** `Date` holds whole milliseconds, so its ends compare as discrete epoch ms. */
|
|
133
|
+
export declare const dateWithin: Within<Date>;
|
|
134
|
+
/** A `number` end for `.admits`, where a finite value is required. */
|
|
135
|
+
export declare function toFiniteBound(label: string, end: "min" | "max", input: InputBound<number>): Bound<number>;
|
|
136
|
+
/**
|
|
137
|
+
* Resolve one `.admits(...)` group of ends — a length, a size, or a kind's own
|
|
138
|
+
* `min`/`max`. Each end the caller names replaces the current one, `±Infinity`
|
|
139
|
+
* dropping it; each end left out keeps `current`, which is what was admitted
|
|
140
|
+
* before, or the kind's default. Successive calls therefore merge per end.
|
|
141
|
+
*
|
|
142
|
+
* Admitting may only widen what is fabricated, or a fabricated value would fail
|
|
143
|
+
* the validation an adapter builds from these ends: each resolved end must
|
|
144
|
+
* admit every value of `fabricable` at that side, an absent fabricable end
|
|
145
|
+
* (drawn against the kind's unbounded cap) admitting only no limit. A
|
|
146
|
+
* `fabricable` of `undefined` skips the check — under `.as(produce)` there is
|
|
147
|
+
* nothing drawn to compare against. The result is complete and minimal: an end
|
|
148
|
+
* with no limit is left off.
|
|
149
|
+
*/
|
|
150
|
+
export declare function toAdmittedEnds<$T, $Input>(label: string, path: string | undefined, current: AdmittedEnds<$T>, fabricable: AdmittedEnds<$T> | undefined, input: InputAdmittedEnds<$Input> | undefined, convert: (end: "min" | "max", input: InputBound<$Input>) => Bound<$T>, within: Within<$T>): AdmittedEnds<$T>;
|
|
@@ -132,6 +132,88 @@ export declare function sample<$T>(list: ReadonlyArray<$T>, stream: Stream): $T;
|
|
|
132
132
|
* `items`, matching `sample`/`weighted`'s read-only convention.
|
|
133
133
|
*/
|
|
134
134
|
export declare function shuffle<$T>(items: ReadonlyArray<$T>, stream: Stream): $T[];
|
|
135
|
+
/**
|
|
136
|
+
* A weighted draw that never returns the same entry twice — each pick leaves
|
|
137
|
+
* the pool. The first pick follows `entries`' weights exactly as `weighted()`
|
|
138
|
+
* would; every later one follows the weights of what remains, which is the same
|
|
139
|
+
* distribution conditioned on not repeating.
|
|
140
|
+
*/
|
|
141
|
+
export type Pool<$Item> = {
|
|
142
|
+
/** How many entries are left to draw. */
|
|
143
|
+
readonly remaining: number;
|
|
144
|
+
/**
|
|
145
|
+
* Remove and return one entry. The caller checks `remaining` first: an empty
|
|
146
|
+
* pool has nothing to return.
|
|
147
|
+
*/
|
|
148
|
+
draw: () => $Item;
|
|
149
|
+
/**
|
|
150
|
+
* Remove the first remaining entry `matches` accepts, without drawing — for
|
|
151
|
+
* an entry produced some other way. A no-op when none matches.
|
|
152
|
+
*/
|
|
153
|
+
discard: (matches: (item: $Item) => boolean) => void;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* A {@link Pool} over `entries`' drawable members (`isDrawable`; a zero weight
|
|
157
|
+
* stays out, as in `weighted()`). Linear in the pool's size per draw, which
|
|
158
|
+
* suits the small, caller-written member lists it is built from (`enum`'s
|
|
159
|
+
* members, `boolean`'s two outcomes).
|
|
160
|
+
*
|
|
161
|
+
* Equal weights stay equal as entries leave, so that case is fixed once: the
|
|
162
|
+
* pick is `discreteSampler`'s uniform index, one `stream.next()` per draw.
|
|
163
|
+
* Unequal weights take one `stream.next()` scaled over the remaining total,
|
|
164
|
+
* summed afresh each draw so removals never accumulate float drift. `splice`,
|
|
165
|
+
* not swap-remove, so the remaining order stays the caller's.
|
|
166
|
+
*/
|
|
167
|
+
export declare function withoutReplacement<$Item>(entries: ReadonlyArray<readonly [number, $Item]>, stream: Stream): Pool<$Item>;
|
|
168
|
+
/**
|
|
169
|
+
* Every integer of an inclusive range, in a uniformly random order, one per
|
|
170
|
+
* `next()` — so no value repeats until all of them have been returned.
|
|
171
|
+
*/
|
|
172
|
+
export type Permutation<$T extends number | bigint> = {
|
|
173
|
+
/** How many values are left before the range is used up. */
|
|
174
|
+
readonly remaining: $T;
|
|
175
|
+
/**
|
|
176
|
+
* The next value. The caller checks `remaining` first: a used-up range has
|
|
177
|
+
* nothing to return.
|
|
178
|
+
*/
|
|
179
|
+
next: () => $T;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* A sparse Fisher–Yates shuffle of `[min, max]`: step `i` swaps position `i`
|
|
183
|
+
* with a uniform position `j` in `[i, size)` and returns what was at `j`. Only
|
|
184
|
+
* positions a swap has touched live in `displaced`, so memory grows with the
|
|
185
|
+
* values drawn, never with the range — `[1, 2^53]` costs nothing up front.
|
|
186
|
+
* Position `i` is never read again after its step, so its entry is deleted.
|
|
187
|
+
*
|
|
188
|
+
* The index draw is `discreteSampler`'s, never an inlined `floor(next() * n)`
|
|
189
|
+
* (see AGENTS.md's "Traps in the length/size draw"). Precondition: `max - min +
|
|
190
|
+
* 1 <= Number.MAX_SAFE_INTEGER`, so every position is an exact integer.
|
|
191
|
+
*/
|
|
192
|
+
export declare function permutation(range: Range, stream: Stream): Permutation<number>;
|
|
193
|
+
/**
|
|
194
|
+
* {@link permutation} over a bigint range, with {@link bigintSampler} drawing
|
|
195
|
+
* each step's position. No size precondition: bigint positions are exact at any
|
|
196
|
+
* width.
|
|
197
|
+
*/
|
|
198
|
+
export declare function bigintPermutation(range: {
|
|
199
|
+
min: bigint;
|
|
200
|
+
max: bigint;
|
|
201
|
+
}, stream: Stream): Permutation<bigint>;
|
|
202
|
+
/**
|
|
203
|
+
* A uniform bigint draw over the inclusive `[min, max]`.
|
|
204
|
+
*
|
|
205
|
+
* Fills a buffer one byte at a time from the seeded stream — the top 8 bits of
|
|
206
|
+
* each draw — so bigint generation replays under a salt like every other
|
|
207
|
+
* primitive, rather than drawing unseedable bytes from
|
|
208
|
+
* `crypto.getRandomValues`. Then rejection: keep the low `bitWidth` bits and
|
|
209
|
+
* retry any candidate past the span, which avoids the modulo bias of `random %
|
|
210
|
+
* span`. The mask keeps every candidate within one bit of the span, so the loop
|
|
211
|
+
* expects fewer than two iterations.
|
|
212
|
+
*/
|
|
213
|
+
export declare function bigintSampler(range: {
|
|
214
|
+
min: bigint;
|
|
215
|
+
max: bigint;
|
|
216
|
+
}, stream: Stream): () => bigint;
|
|
135
217
|
/**
|
|
136
218
|
* `weighted()`'s own inclusion rule: whether this entry stays in the draw
|
|
137
219
|
* table. `0` is valid and disables the outcome; negative/`NaN` are rejected
|
|
@@ -73,7 +73,16 @@ export type Limits = {
|
|
|
73
73
|
* Fabricator: `AnySchema` excludes `Fabricator<any>` (which carries no
|
|
74
74
|
* `[Produces]`), so `Fabricated` can't silently degrade to `unknown`.
|
|
75
75
|
*/
|
|
76
|
-
export type Enumerable = <const $Schema extends AnySchema>(schema: $Schema) =>
|
|
76
|
+
export type Enumerable = <const $Schema extends AnySchema>(schema: $Schema) => Enumeration<Fabricated<$Schema>>;
|
|
77
|
+
/**
|
|
78
|
+
* What `combinatorial`/`coverage` return: the lazy instances, plus `size`, how
|
|
79
|
+
* many there are — known before any is produced. When compared against
|
|
80
|
+
* `capacity(schema)`, it says up front whether every instance can hold distinct
|
|
81
|
+
* unique values, or whether some leaf will start a fresh round.
|
|
82
|
+
*/
|
|
83
|
+
export type Enumeration<$T> = Iterable<$T> & {
|
|
84
|
+
readonly size: bigint;
|
|
85
|
+
};
|
|
77
86
|
export type Resolvable = {
|
|
78
87
|
[Kind]: SchemaKind;
|
|
79
88
|
[Meta]: PlainObject<any>;
|
|
@@ -12,6 +12,13 @@
|
|
|
12
12
|
* stands on its own: a consumer reading only `.message` should not need a field
|
|
13
13
|
* to understand the failure. Fields are for programmatic access, and for detail
|
|
14
14
|
* too bulky to inline.
|
|
15
|
+
*
|
|
16
|
+
* A `name` is the package and the class,
|
|
17
|
+
* `"@ghostry/fabricator:UnknownFieldError"`, so it identifies an error where
|
|
18
|
+
* `instanceof` cannot: across two installed copies of this package, or after
|
|
19
|
+
* the error is serialized. `:`, not `/`, which would read as a subpath export.
|
|
20
|
+
* A package extending {@link FabricatorError} prefixes its own errors with its
|
|
21
|
+
* own name.
|
|
15
22
|
*/
|
|
16
23
|
/**
|
|
17
24
|
* Base error class from which more specific errors inherit.
|
|
@@ -101,6 +108,46 @@ export declare namespace FabricatorError {
|
|
|
101
108
|
*/
|
|
102
109
|
path: ReadonlyArray<string>);
|
|
103
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* `T.recursive(body).whereby({ depth })` with no `terminal`, when the derived
|
|
113
|
+
* terminal must empty a collection of `self` that states a minimum above 0.
|
|
114
|
+
* Every branch ends at the depth limit with that collection empty, and an
|
|
115
|
+
* adapter states the minimum as written, so no finite tree could pass the
|
|
116
|
+
* validation it builds. Thrown at `.whereby()`. `.admits({ length: { min: 0 }
|
|
117
|
+
* })` on the collection keeps drawing at least the minimum below the limit
|
|
118
|
+
* while stating none for the adapter.
|
|
119
|
+
*/
|
|
120
|
+
class UnsatisfiableRecursiveBoundError extends FabricatorError {
|
|
121
|
+
/**
|
|
122
|
+
* Structural path from the recursive body to the collection, as field
|
|
123
|
+
* names / slot indices.
|
|
124
|
+
*/
|
|
125
|
+
readonly path: ReadonlyArray<string>;
|
|
126
|
+
/**
|
|
127
|
+
* The collection's bound — `"length"` for an array, `"size"` for a
|
|
128
|
+
* record.
|
|
129
|
+
*/
|
|
130
|
+
readonly key: "length" | "size";
|
|
131
|
+
/**
|
|
132
|
+
* The smallest count the collection states, as an inclusive integer.
|
|
133
|
+
*/
|
|
134
|
+
readonly min: number;
|
|
135
|
+
constructor(
|
|
136
|
+
/**
|
|
137
|
+
* Structural path from the recursive body to the collection, as field
|
|
138
|
+
* names / slot indices.
|
|
139
|
+
*/
|
|
140
|
+
path: ReadonlyArray<string>,
|
|
141
|
+
/**
|
|
142
|
+
* The collection's bound — `"length"` for an array, `"size"` for a
|
|
143
|
+
* record.
|
|
144
|
+
*/
|
|
145
|
+
key: "length" | "size",
|
|
146
|
+
/**
|
|
147
|
+
* The smallest count the collection states, as an inclusive integer.
|
|
148
|
+
*/
|
|
149
|
+
min: number);
|
|
150
|
+
}
|
|
104
151
|
/**
|
|
105
152
|
* A walk reached a node whose `[Kind]` it has no case for — a Schema built by
|
|
106
153
|
* hand or cast past the type system, or a kind added to `Primitive/index.ts`
|
|
@@ -469,6 +516,102 @@ export declare namespace FabricatorError {
|
|
|
469
516
|
readonly exclusive: boolean;
|
|
470
517
|
});
|
|
471
518
|
}
|
|
519
|
+
/**
|
|
520
|
+
* A bound given as a `number` that does not convert exactly to its kind's
|
|
521
|
+
* type — a fractional or unsafe integer for a `bigint`, an instant `Date`
|
|
522
|
+
* cannot hold — or a non-finite value where an end must be finite.
|
|
523
|
+
*/
|
|
524
|
+
class InvalidBoundError extends FabricatorError {
|
|
525
|
+
/**
|
|
526
|
+
* What was called, e.g. `"T.bigint.whereby"` or `".admits on a bigint
|
|
527
|
+
* schema"`.
|
|
528
|
+
*/
|
|
529
|
+
readonly label: string;
|
|
530
|
+
/**
|
|
531
|
+
* Which end was rejected.
|
|
532
|
+
*/
|
|
533
|
+
readonly end: "min" | "max";
|
|
534
|
+
/**
|
|
535
|
+
* The rejected value, as given.
|
|
536
|
+
*/
|
|
537
|
+
readonly value: number;
|
|
538
|
+
/**
|
|
539
|
+
* What the end accepts, in words — e.g. `"a safe integer or a bigint"`.
|
|
540
|
+
*/
|
|
541
|
+
readonly expected: string;
|
|
542
|
+
constructor(
|
|
543
|
+
/**
|
|
544
|
+
* What was called, e.g. `"T.bigint.whereby"` or `".admits on a bigint
|
|
545
|
+
* schema"`.
|
|
546
|
+
*/
|
|
547
|
+
label: string,
|
|
548
|
+
/**
|
|
549
|
+
* Which end was rejected.
|
|
550
|
+
*/
|
|
551
|
+
end: "min" | "max",
|
|
552
|
+
/**
|
|
553
|
+
* The rejected value, as given.
|
|
554
|
+
*/
|
|
555
|
+
value: number,
|
|
556
|
+
/**
|
|
557
|
+
* What the end accepts, in words — e.g. `"a safe integer or a bigint"`.
|
|
558
|
+
*/
|
|
559
|
+
expected: string);
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* `.admits(...)` names an end that excludes values the schema fabricates.
|
|
563
|
+
* Admitted ends may only widen the fabricated range — a fabricated value must
|
|
564
|
+
* pass the validation an adapter builds from them.
|
|
565
|
+
*/
|
|
566
|
+
class AdmitsExcludesDrawError extends FabricatorError {
|
|
567
|
+
/**
|
|
568
|
+
* What was called, e.g. `".admits on a string schema"`.
|
|
569
|
+
*/
|
|
570
|
+
readonly label: string;
|
|
571
|
+
/**
|
|
572
|
+
* The rejected end's path, e.g. `"length.max"` or `"min"`.
|
|
573
|
+
*/
|
|
574
|
+
readonly end: string;
|
|
575
|
+
/**
|
|
576
|
+
* The admitted end, canonical as stored.
|
|
577
|
+
*/
|
|
578
|
+
readonly admitted: {
|
|
579
|
+
readonly value: unknown;
|
|
580
|
+
readonly exclusive: boolean;
|
|
581
|
+
};
|
|
582
|
+
/**
|
|
583
|
+
* The fabricated end it excludes values from; absent when that end is
|
|
584
|
+
* drawn against the kind's unbounded cap, which only no limit admits.
|
|
585
|
+
*/
|
|
586
|
+
readonly fabricated: {
|
|
587
|
+
readonly value: unknown;
|
|
588
|
+
readonly exclusive: boolean;
|
|
589
|
+
} | undefined;
|
|
590
|
+
constructor(
|
|
591
|
+
/**
|
|
592
|
+
* What was called, e.g. `".admits on a string schema"`.
|
|
593
|
+
*/
|
|
594
|
+
label: string,
|
|
595
|
+
/**
|
|
596
|
+
* The rejected end's path, e.g. `"length.max"` or `"min"`.
|
|
597
|
+
*/
|
|
598
|
+
end: string,
|
|
599
|
+
/**
|
|
600
|
+
* The admitted end, canonical as stored.
|
|
601
|
+
*/
|
|
602
|
+
admitted: {
|
|
603
|
+
readonly value: unknown;
|
|
604
|
+
readonly exclusive: boolean;
|
|
605
|
+
},
|
|
606
|
+
/**
|
|
607
|
+
* The fabricated end it excludes values from; absent when that end is
|
|
608
|
+
* drawn against the kind's unbounded cap, which only no limit admits.
|
|
609
|
+
*/
|
|
610
|
+
fabricated: {
|
|
611
|
+
readonly value: unknown;
|
|
612
|
+
readonly exclusive: boolean;
|
|
613
|
+
} | undefined);
|
|
614
|
+
}
|
|
472
615
|
/**
|
|
473
616
|
* A distribution's bounds fall outside its own domain — a `logarithmic`
|
|
474
617
|
* distribution's density is proportional to `1/x`, so it is undefined at or
|
|
@@ -648,4 +791,144 @@ export declare namespace FabricatorError {
|
|
|
648
791
|
class HarnessingProviderError extends FabricatorError {
|
|
649
792
|
constructor();
|
|
650
793
|
}
|
|
794
|
+
/**
|
|
795
|
+
* Where a unique leaf that ran out sits directly inside a plain collection —
|
|
796
|
+
* the element of a `T.array` or the key of a `T.record`. Such a leaf stays
|
|
797
|
+
* distinct across every value the collection makes, which is easy to mistake
|
|
798
|
+
* for distinct within one, so the message names `T.unique.array`/
|
|
799
|
+
* `T.unique.record`.
|
|
800
|
+
*/
|
|
801
|
+
type UniqueWithin = "array" | "record";
|
|
802
|
+
/**
|
|
803
|
+
* A `T.unique.*` leaf asked for one more value than its domain holds. Thrown
|
|
804
|
+
* on draw n + 1 of an n-value domain whatever the salt, since a domain with a
|
|
805
|
+
* known size is counted rather than searched (`Unique/Distinct.ts`).
|
|
806
|
+
*/
|
|
807
|
+
class UniqueExhaustedError extends FabricatorError {
|
|
808
|
+
/**
|
|
809
|
+
* The `[Kind]` of the exhausted schema.
|
|
810
|
+
*/
|
|
811
|
+
readonly kind: string;
|
|
812
|
+
/**
|
|
813
|
+
* How many distinct values the domain holds — every one of them already
|
|
814
|
+
* produced or claimed.
|
|
815
|
+
*/
|
|
816
|
+
readonly capacity: bigint;
|
|
817
|
+
/**
|
|
818
|
+
* The plain collection the leaf sits directly inside, when it does.
|
|
819
|
+
*/
|
|
820
|
+
readonly collection?: UniqueWithin | undefined;
|
|
821
|
+
constructor(
|
|
822
|
+
/**
|
|
823
|
+
* The `[Kind]` of the exhausted schema.
|
|
824
|
+
*/
|
|
825
|
+
kind: string,
|
|
826
|
+
/**
|
|
827
|
+
* How many distinct values the domain holds — every one of them already
|
|
828
|
+
* produced or claimed.
|
|
829
|
+
*/
|
|
830
|
+
capacity: bigint,
|
|
831
|
+
/**
|
|
832
|
+
* The plain collection the leaf sits directly inside, when it does.
|
|
833
|
+
*/
|
|
834
|
+
collection?: UniqueWithin | undefined);
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* A unique draw over a domain of unknown size kept landing on values it had
|
|
838
|
+
* already produced, and forfeited after its redraws ran out rather than loop
|
|
839
|
+
* forever. Either the domain is used up, or it is skewed enough that its
|
|
840
|
+
* remaining values are rare — a higher `redraws` is for the second case.
|
|
841
|
+
*/
|
|
842
|
+
class UniqueRedrawsError extends FabricatorError {
|
|
843
|
+
/**
|
|
844
|
+
* The registry entry or call site, e.g. `"T.unique.array"`.
|
|
845
|
+
*/
|
|
846
|
+
readonly label: string;
|
|
847
|
+
/**
|
|
848
|
+
* How many redraws the draw was allowed after its first, each of which
|
|
849
|
+
* also repeated a value already produced.
|
|
850
|
+
*/
|
|
851
|
+
readonly redraws: number;
|
|
852
|
+
/**
|
|
853
|
+
* How many distinct values the scope had produced when it gave up.
|
|
854
|
+
*/
|
|
855
|
+
readonly produced: number;
|
|
856
|
+
/**
|
|
857
|
+
* The plain collection a unique leaf sits directly inside, when it does.
|
|
858
|
+
*/
|
|
859
|
+
readonly collection?: UniqueWithin | undefined;
|
|
860
|
+
constructor(
|
|
861
|
+
/**
|
|
862
|
+
* The registry entry or call site, e.g. `"T.unique.array"`.
|
|
863
|
+
*/
|
|
864
|
+
label: string,
|
|
865
|
+
/**
|
|
866
|
+
* How many redraws the draw was allowed after its first, each of which
|
|
867
|
+
* also repeated a value already produced.
|
|
868
|
+
*/
|
|
869
|
+
redraws: number,
|
|
870
|
+
/**
|
|
871
|
+
* How many distinct values the scope had produced when it gave up.
|
|
872
|
+
*/
|
|
873
|
+
produced: number,
|
|
874
|
+
/**
|
|
875
|
+
* The plain collection a unique leaf sits directly inside, when it does.
|
|
876
|
+
*/
|
|
877
|
+
collection?: UniqueWithin | undefined);
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
880
|
+
* A unique collection could draw more members than its member schema has
|
|
881
|
+
* distinct values. Thrown at construction, so the draw that would run out
|
|
882
|
+
* never depends on the salt.
|
|
883
|
+
*/
|
|
884
|
+
class UniqueCapacityError extends FabricatorError {
|
|
885
|
+
/**
|
|
886
|
+
* The registry entry, e.g. `"T.unique.array"`.
|
|
887
|
+
*/
|
|
888
|
+
readonly label: string;
|
|
889
|
+
/**
|
|
890
|
+
* How many distinct values the member schema has.
|
|
891
|
+
*/
|
|
892
|
+
readonly capacity: bigint;
|
|
893
|
+
/**
|
|
894
|
+
* The largest member count the collection may draw.
|
|
895
|
+
*/
|
|
896
|
+
readonly requested: number;
|
|
897
|
+
constructor(
|
|
898
|
+
/**
|
|
899
|
+
* The registry entry, e.g. `"T.unique.array"`.
|
|
900
|
+
*/
|
|
901
|
+
label: string,
|
|
902
|
+
/**
|
|
903
|
+
* How many distinct values the member schema has.
|
|
904
|
+
*/
|
|
905
|
+
capacity: bigint,
|
|
906
|
+
/**
|
|
907
|
+
* The largest member count the collection may draw.
|
|
908
|
+
*/
|
|
909
|
+
requested: number);
|
|
910
|
+
}
|
|
911
|
+
/**
|
|
912
|
+
* `redraws` on a unique builder is not a whole number from `0` up, so it
|
|
913
|
+
* could never be a count of retries.
|
|
914
|
+
*/
|
|
915
|
+
class InvalidRedrawsError extends FabricatorError {
|
|
916
|
+
/**
|
|
917
|
+
* The registry entry, e.g. `"T.unique.array.whereby"`.
|
|
918
|
+
*/
|
|
919
|
+
readonly label: string;
|
|
920
|
+
/**
|
|
921
|
+
* The rejected value.
|
|
922
|
+
*/
|
|
923
|
+
readonly redraws: number;
|
|
924
|
+
constructor(
|
|
925
|
+
/**
|
|
926
|
+
* The registry entry, e.g. `"T.unique.array.whereby"`.
|
|
927
|
+
*/
|
|
928
|
+
label: string,
|
|
929
|
+
/**
|
|
930
|
+
* The rejected value.
|
|
931
|
+
*/
|
|
932
|
+
redraws: number);
|
|
933
|
+
}
|
|
651
934
|
}
|