@ghostry/fabricator 0.0.12 → 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/README.md +1 -1
- 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/Fabricator/Fabricate.js +1 -6
- 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/boolean/Registry.js +10 -8
- 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 +111 -38
- 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/null/Registry.js +6 -4
- 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 +86 -29
- 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/symbol/Registry.js +15 -13
- package/dist/esm/Primitive/undefined/Registry.js +6 -4
- package/dist/esm/Primitive/unique/Registry.js +22 -0
- package/dist/esm/Primitive/unique/index.js +2 -0
- package/dist/esm/Schema/Core.js +9 -1
- package/dist/esm/Schema/Registry.js +27 -7
- 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/Utility/DeepMerge.js +47 -6
- package/dist/esm/internal.js +2 -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/Fabricate.d.ts +4 -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/boolean/Registry.d.ts +6 -1
- 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 +54 -12
- 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/null/Registry.d.ts +6 -1
- 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 +50 -2
- 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/symbol/Registry.d.ts +6 -1
- package/dist/types/Primitive/undefined/Registry.d.ts +6 -1
- package/dist/types/Primitive/unique/Registry.d.ts +39 -0
- package/dist/types/Primitive/unique/index.d.ts +2 -0
- package/dist/types/Schema/Core.d.ts +16 -1
- package/dist/types/Schema/Registry.d.ts +7 -4
- 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/Utility/DeepMerge.d.ts +11 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/types/internal.d.ts +9 -0
- package/package.json +3 -3
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { effectiveDiscrete, effectiveDiscreteBigint } from "../Bound.js";
|
|
2
|
+
import { discreteSampler } from "../Distribution/index.js";
|
|
3
|
+
import { FabricatorError } from "../Error/index.js";
|
|
4
|
+
import { toEpochRange } from "../Primitive/date/Defaults.js";
|
|
5
|
+
import { toStringDomain } from "../Primitive/string/Capacity.js";
|
|
6
|
+
import { unboundedContinuous, unboundedDiscrete } from "../Primitive/number/Defaults.js";
|
|
7
|
+
import { Kind, Meta } from "../Types.js";
|
|
8
|
+
import { exactBigintRange, exactRange, exactValues, tracked, trackedByKey } from "./Strategy.js";
|
|
9
|
+
function toNumberCapacity(meta) {
|
|
10
|
+
var _ref, _ref1;
|
|
11
|
+
var _meta_whereby, _meta_whereby1;
|
|
12
|
+
if (meta.integer) {
|
|
13
|
+
var _ref2, _ref3;
|
|
14
|
+
var _meta_whereby2, _meta_whereby3;
|
|
15
|
+
return toRangeCapacity(effectiveDiscrete(null != (_ref2 = null == (_meta_whereby2 = meta.whereby) ? void 0 : _meta_whereby2.min) ? _ref2 : unboundedDiscrete.min, null != (_ref3 = null == (_meta_whereby3 = meta.whereby) ? void 0 : _meta_whereby3.max) ? _ref3 : unboundedDiscrete.max));
|
|
16
|
+
}
|
|
17
|
+
const min = null != (_ref = null == (_meta_whereby = meta.whereby) ? void 0 : _meta_whereby.min) ? _ref : unboundedContinuous.min;
|
|
18
|
+
const max = null != (_ref1 = null == (_meta_whereby1 = meta.whereby) ? void 0 : _meta_whereby1.max) ? _ref1 : unboundedContinuous.max;
|
|
19
|
+
return min.value === max.value ? BigInt(1) : void 0;
|
|
20
|
+
}
|
|
21
|
+
function toRangeCapacity(range) {
|
|
22
|
+
return BigInt(range.max) - BigInt(range.min) + BigInt(1);
|
|
23
|
+
}
|
|
24
|
+
function toNumberStrategy(options) {
|
|
25
|
+
var _options_redraws, _ref, _ref1;
|
|
26
|
+
var _meta_whereby, _meta_whereby1, _meta_whereby2;
|
|
27
|
+
const { label, meta, stream, draw } = options;
|
|
28
|
+
const redraws = null != (_options_redraws = options.redraws) ? _options_redraws : null == (_meta_whereby = meta.whereby) ? void 0 : _meta_whereby.redraws;
|
|
29
|
+
const key = (value)=>value;
|
|
30
|
+
if (meta.integer) {
|
|
31
|
+
var _ref2, _ref3;
|
|
32
|
+
var _meta_whereby3, _meta_whereby4, _meta_whereby5;
|
|
33
|
+
const range = effectiveDiscrete(null != (_ref2 = null == (_meta_whereby3 = meta.whereby) ? void 0 : _meta_whereby3.min) ? _ref2 : unboundedDiscrete.min, null != (_ref3 = null == (_meta_whereby4 = meta.whereby) ? void 0 : _meta_whereby4.max) ? _ref3 : unboundedDiscrete.max);
|
|
34
|
+
const distribution = null == (_meta_whereby5 = meta.whereby) ? void 0 : _meta_whereby5.distribution;
|
|
35
|
+
const uniform = void 0 === distribution || "uniform" === distribution.kind;
|
|
36
|
+
if (uniform && range.max - range.min + 1 <= Number.MAX_SAFE_INTEGER) return exactRange("number", range, stream);
|
|
37
|
+
return tracked({
|
|
38
|
+
label,
|
|
39
|
+
kind: "number",
|
|
40
|
+
draw,
|
|
41
|
+
key,
|
|
42
|
+
redraws,
|
|
43
|
+
capacity: toNumberCapacity(meta),
|
|
44
|
+
admits: (value)=>"number" == typeof value && Number.isInteger(value) && value >= range.min && value <= range.max
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const min = null != (_ref = null == (_meta_whereby1 = meta.whereby) ? void 0 : _meta_whereby1.min) ? _ref : unboundedContinuous.min;
|
|
48
|
+
const max = null != (_ref1 = null == (_meta_whereby2 = meta.whereby) ? void 0 : _meta_whereby2.max) ? _ref1 : unboundedContinuous.max;
|
|
49
|
+
return tracked({
|
|
50
|
+
label,
|
|
51
|
+
kind: "number",
|
|
52
|
+
draw,
|
|
53
|
+
key,
|
|
54
|
+
redraws,
|
|
55
|
+
capacity: toNumberCapacity(meta),
|
|
56
|
+
admits: (value)=>"number" == typeof value && value >= min.value && value <= max.value
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
function toDateStrategy(options) {
|
|
60
|
+
const { label, range, distribution, stream, redraws } = options;
|
|
61
|
+
const uniform = void 0 === distribution || "uniform" === distribution.kind;
|
|
62
|
+
const admits = (value)=>value instanceof Date && value.getTime() >= range.min && value.getTime() <= range.max;
|
|
63
|
+
if (uniform && range.max - range.min + 1 <= Number.MAX_SAFE_INTEGER) {
|
|
64
|
+
const exact = exactRange("date", range, stream);
|
|
65
|
+
return {
|
|
66
|
+
capacity: exact.capacity,
|
|
67
|
+
open: ()=>{
|
|
68
|
+
const scope = exact.open();
|
|
69
|
+
return {
|
|
70
|
+
draw: ()=>new Date(scope.draw()),
|
|
71
|
+
claim: (value)=>{
|
|
72
|
+
if (admits(value)) scope.claim(value.getTime());
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const next = discreteSampler(distribution, range, stream);
|
|
79
|
+
return tracked({
|
|
80
|
+
label,
|
|
81
|
+
kind: "date",
|
|
82
|
+
draw: ()=>new Date(next()),
|
|
83
|
+
key: (value)=>value.getTime(),
|
|
84
|
+
redraws,
|
|
85
|
+
capacity: toRangeCapacity(range),
|
|
86
|
+
admits
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function toBigintStrategy(whereby, stream) {
|
|
90
|
+
return exactBigintRange("bigint", effectiveDiscreteBigint(whereby.min, whereby.max), stream);
|
|
91
|
+
}
|
|
92
|
+
function toDistinct(member, stream, options) {
|
|
93
|
+
const { label, by, redraws } = options;
|
|
94
|
+
const kind = member[Kind];
|
|
95
|
+
const meta = member[Meta];
|
|
96
|
+
const draw = ()=>member.fabricate();
|
|
97
|
+
if (by || (null == meta ? void 0 : meta.produce) || (null == meta ? void 0 : meta.unique)) return trackedByKey({
|
|
98
|
+
label,
|
|
99
|
+
kind,
|
|
100
|
+
draw,
|
|
101
|
+
by,
|
|
102
|
+
redraws
|
|
103
|
+
});
|
|
104
|
+
switch(kind){
|
|
105
|
+
case "always":
|
|
106
|
+
return exactValues(kind, [
|
|
107
|
+
[
|
|
108
|
+
1,
|
|
109
|
+
meta.value
|
|
110
|
+
]
|
|
111
|
+
], stream);
|
|
112
|
+
case "null":
|
|
113
|
+
return exactValues(kind, [
|
|
114
|
+
[
|
|
115
|
+
1,
|
|
116
|
+
null
|
|
117
|
+
]
|
|
118
|
+
], stream);
|
|
119
|
+
case "undefined":
|
|
120
|
+
return exactValues(kind, [
|
|
121
|
+
[
|
|
122
|
+
1,
|
|
123
|
+
void 0
|
|
124
|
+
]
|
|
125
|
+
], stream);
|
|
126
|
+
case "boolean":
|
|
127
|
+
var _ref, _ref1;
|
|
128
|
+
var _meta_weights, _meta_weights1;
|
|
129
|
+
return exactValues(kind, [
|
|
130
|
+
[
|
|
131
|
+
null != (_ref = null == (_meta_weights = meta.weights) ? void 0 : _meta_weights.true) ? _ref : 1,
|
|
132
|
+
true
|
|
133
|
+
],
|
|
134
|
+
[
|
|
135
|
+
null != (_ref1 = null == (_meta_weights1 = meta.weights) ? void 0 : _meta_weights1.false) ? _ref1 : 1,
|
|
136
|
+
false
|
|
137
|
+
]
|
|
138
|
+
], stream);
|
|
139
|
+
case "enum":
|
|
140
|
+
return exactValues(kind, meta.items, stream);
|
|
141
|
+
case "number":
|
|
142
|
+
if (meta.sequence) return trackedByKey({
|
|
143
|
+
label,
|
|
144
|
+
kind,
|
|
145
|
+
draw,
|
|
146
|
+
redraws
|
|
147
|
+
});
|
|
148
|
+
return toNumberStrategy({
|
|
149
|
+
label,
|
|
150
|
+
meta,
|
|
151
|
+
stream,
|
|
152
|
+
draw: draw,
|
|
153
|
+
redraws
|
|
154
|
+
});
|
|
155
|
+
case "bigint":
|
|
156
|
+
return toBigintStrategy(meta.whereby, stream);
|
|
157
|
+
case "date":
|
|
158
|
+
var _meta_whereby;
|
|
159
|
+
return toDateStrategy({
|
|
160
|
+
label,
|
|
161
|
+
range: toEpochRange(meta, member.trace.clock),
|
|
162
|
+
distribution: null == (_meta_whereby = meta.whereby) ? void 0 : _meta_whereby.distribution,
|
|
163
|
+
stream,
|
|
164
|
+
redraws
|
|
165
|
+
});
|
|
166
|
+
case "string":
|
|
167
|
+
return trackedByKey({
|
|
168
|
+
label,
|
|
169
|
+
kind,
|
|
170
|
+
draw,
|
|
171
|
+
redraws,
|
|
172
|
+
...toStringDomain(meta.whereby)
|
|
173
|
+
});
|
|
174
|
+
default:
|
|
175
|
+
return trackedByKey({
|
|
176
|
+
label,
|
|
177
|
+
kind,
|
|
178
|
+
draw,
|
|
179
|
+
redraws
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function assertCapacity(label, strategy, requested) {
|
|
184
|
+
const { capacity } = strategy;
|
|
185
|
+
if (void 0 !== capacity && BigInt(requested) > capacity) throw new FabricatorError.UniqueCapacityError(label, capacity, requested);
|
|
186
|
+
}
|
|
187
|
+
export { assertCapacity, toBigintStrategy, toDateStrategy, toDistinct, toNumberCapacity, toNumberStrategy, toRangeCapacity };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { cyrb128 } from "../Utility/Digest.js";
|
|
2
|
+
import { isPlainObject } from "../Utility/Core.js";
|
|
3
|
+
function toKeyer() {
|
|
4
|
+
const objects = new WeakMap();
|
|
5
|
+
const symbols = new Map();
|
|
6
|
+
let next = 0;
|
|
7
|
+
function identity(value) {
|
|
8
|
+
if ("symbol" == typeof value) {
|
|
9
|
+
const known = symbols.get(value);
|
|
10
|
+
if (void 0 !== known) return known;
|
|
11
|
+
symbols.set(value, next);
|
|
12
|
+
return next++;
|
|
13
|
+
}
|
|
14
|
+
const known = objects.get(value);
|
|
15
|
+
if (void 0 !== known) return known;
|
|
16
|
+
objects.set(value, next);
|
|
17
|
+
return next++;
|
|
18
|
+
}
|
|
19
|
+
function encode(value, active) {
|
|
20
|
+
switch(typeof value){
|
|
21
|
+
case "string":
|
|
22
|
+
return JSON.stringify(value);
|
|
23
|
+
case "number":
|
|
24
|
+
return `n${value}`;
|
|
25
|
+
case "bigint":
|
|
26
|
+
return `b${value}`;
|
|
27
|
+
case "boolean":
|
|
28
|
+
return value ? "t" : "f";
|
|
29
|
+
case "undefined":
|
|
30
|
+
return "u";
|
|
31
|
+
case "symbol":
|
|
32
|
+
return `y${identity(value)}`;
|
|
33
|
+
case "function":
|
|
34
|
+
return `o${identity(value)}`;
|
|
35
|
+
case "object":
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
if (null === value) return "N";
|
|
39
|
+
if (value instanceof Date) return `d${value.getTime()}`;
|
|
40
|
+
const composite = value;
|
|
41
|
+
if (active.has(composite)) return `o${identity(composite)}`;
|
|
42
|
+
if (Array.isArray(composite)) {
|
|
43
|
+
active.add(composite);
|
|
44
|
+
const parts = [];
|
|
45
|
+
for(let i = 0; i < composite.length; i++)parts.push(encode(composite[i], active));
|
|
46
|
+
active.delete(composite);
|
|
47
|
+
return `[${parts.join(",")}]`;
|
|
48
|
+
}
|
|
49
|
+
if (isPlainObject(composite)) {
|
|
50
|
+
active.add(composite);
|
|
51
|
+
const record = composite;
|
|
52
|
+
const parts = Object.keys(record).sort().map((key)=>`${JSON.stringify(key)}:${encode(record[key], active)}`);
|
|
53
|
+
const symbolic = Object.getOwnPropertySymbols(record).filter((key)=>Object.prototype.propertyIsEnumerable.call(record, key)).map((key)=>[
|
|
54
|
+
identity(key),
|
|
55
|
+
record[key]
|
|
56
|
+
]).sort(([a], [b])=>a - b).map(([id, entry])=>`y${id}:${encode(entry, active)}`);
|
|
57
|
+
active.delete(composite);
|
|
58
|
+
return `{${[
|
|
59
|
+
...parts,
|
|
60
|
+
...symbolic
|
|
61
|
+
].join(",")}}`;
|
|
62
|
+
}
|
|
63
|
+
return `o${identity(composite)}`;
|
|
64
|
+
}
|
|
65
|
+
return (value)=>{
|
|
66
|
+
const [high, low] = cyrb128(encode(value, new Set()));
|
|
67
|
+
return (0x1fffff & high) * 0x100000000 + low;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export { toKeyer };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Children, Claim, Kind, Omitted } from "../Types.js";
|
|
2
|
+
function recordOverride(field, value) {
|
|
3
|
+
if (Claim in field) return void field[Claim](value);
|
|
4
|
+
if (stopsAt(field[Kind], value)) return;
|
|
5
|
+
recordOverride(field[Children], value);
|
|
6
|
+
}
|
|
7
|
+
function stopsAt(kind, value) {
|
|
8
|
+
switch(kind){
|
|
9
|
+
case "nullable":
|
|
10
|
+
return null === value;
|
|
11
|
+
case "nullish":
|
|
12
|
+
return null == value;
|
|
13
|
+
case "undefinable":
|
|
14
|
+
return void 0 === value;
|
|
15
|
+
case "object.omittable":
|
|
16
|
+
return value === Omitted;
|
|
17
|
+
case "object.optional":
|
|
18
|
+
return value === Omitted || void 0 === value;
|
|
19
|
+
default:
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export { recordOverride };
|
|
@@ -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 };
|
|
@@ -3,16 +3,57 @@ import { isPlainObject, mergeableKeys } from "./Core.js";
|
|
|
3
3
|
function deepMerge(a, b) {
|
|
4
4
|
const result = {};
|
|
5
5
|
if (Replace in b && true === b[Replace]) {
|
|
6
|
-
for (const key of mergeableKeys(b))result
|
|
6
|
+
for (const key of mergeableKeys(b))copy(result, b, key);
|
|
7
7
|
return result;
|
|
8
8
|
}
|
|
9
|
-
for (const key of mergeableKeys(a))result
|
|
9
|
+
for (const key of mergeableKeys(a))copy(result, a, key);
|
|
10
10
|
for (const key of mergeableKeys(b)){
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
else result[key] = bValue;
|
|
11
|
+
const getter = isGetter(result, key) || isGetter(b, key);
|
|
12
|
+
const value = mergeValue(result[key], b[key]);
|
|
13
|
+
Object.defineProperty(result, key, property(value, getter));
|
|
15
14
|
}
|
|
16
15
|
return result;
|
|
17
16
|
}
|
|
17
|
+
function mergeValue(left, right) {
|
|
18
|
+
if (isPlainObject(left) && isPlainObject(right)) return deepMerge(left, right);
|
|
19
|
+
return right;
|
|
20
|
+
}
|
|
21
|
+
function property(value, getter) {
|
|
22
|
+
if (getter && isPlainObject(value)) return {
|
|
23
|
+
get: ()=>rebuilt(value),
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true
|
|
26
|
+
};
|
|
27
|
+
return own({
|
|
28
|
+
value
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function rebuilt(value) {
|
|
32
|
+
return Object.defineProperties({}, Object.getOwnPropertyDescriptors(value));
|
|
33
|
+
}
|
|
34
|
+
function copy(target, source, key) {
|
|
35
|
+
Object.defineProperty(target, key, own(descriptorOf(source, key)));
|
|
36
|
+
}
|
|
37
|
+
function own(descriptor) {
|
|
38
|
+
const normalized = {
|
|
39
|
+
...descriptor,
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true
|
|
42
|
+
};
|
|
43
|
+
if (isAccessor(descriptor)) return normalized;
|
|
44
|
+
return {
|
|
45
|
+
...normalized,
|
|
46
|
+
writable: true
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function isAccessor(descriptor) {
|
|
50
|
+
return "get" in descriptor || "set" in descriptor;
|
|
51
|
+
}
|
|
52
|
+
function isGetter(source, key) {
|
|
53
|
+
var _Object_getOwnPropertyDescriptor;
|
|
54
|
+
return (null == (_Object_getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(source, key)) ? void 0 : _Object_getOwnPropertyDescriptor.get) !== void 0;
|
|
55
|
+
}
|
|
56
|
+
function descriptorOf(source, key) {
|
|
57
|
+
return Object.getOwnPropertyDescriptor(source, key);
|
|
58
|
+
}
|
|
18
59
|
export { deepMerge };
|
package/dist/esm/internal.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { Children, Kind, Meta, Produces } from "./Types.js";
|
|
2
2
|
export { isArray, isPlainObject } from "./Utility/Core.js";
|
|
3
|
+
export { isBuildable, isSchema } from "./Schema/Core.js";
|
|
4
|
+
export { toKeyer } from "./Unique/Key.js";
|
|
3
5
|
export { plan, resolve } from "./Enumeration/Plan.js";
|
|
4
6
|
export { defaultAlgorithm, encode, randomSalt, toRandomSource, toStream, toStreamFromTrace } from "./Random/index.js";
|
|
5
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>;
|