@ghostry/fabricator 0.0.11 → 0.0.13
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/Enumeration/Enumerate.js +2 -4
- package/dist/esm/Error/index.js +34 -6
- package/dist/esm/Fabricator/Constructor.js +40 -33
- package/dist/esm/Fabricator/Fabricate.js +23 -0
- package/dist/esm/Instance/Core.js +14 -2
- package/dist/esm/Instance/Stack/Visible.js +6 -1
- package/dist/esm/Primitive/boolean/Registry.js +10 -8
- package/dist/esm/Primitive/date/Defaults.js +12 -0
- package/dist/esm/Primitive/date/Fabricator.js +5 -10
- package/dist/esm/Primitive/date/Registry.js +49 -39
- package/dist/esm/Primitive/index.js +2 -0
- package/dist/esm/Primitive/null/Registry.js +6 -4
- package/dist/esm/Primitive/number/Fabricator.js +1 -1
- package/dist/esm/Primitive/number/Registry.js +36 -30
- package/dist/esm/Primitive/object/Fabricator.js +33 -11
- package/dist/esm/Primitive/object/Registry.js +90 -10
- package/dist/esm/Primitive/object/compute/Fabricator.js +10 -3
- package/dist/esm/Primitive/object/compute/Registry.js +20 -3
- package/dist/esm/Primitive/object/omit/Registry.js +5 -0
- package/dist/esm/Primitive/object/omit/index.js +2 -0
- package/dist/esm/Primitive/object/pick/Registry.js +5 -0
- package/dist/esm/Primitive/object/pick/index.js +2 -0
- package/dist/esm/Primitive/recursive/Fabricator.js +1 -0
- package/dist/esm/Primitive/recursive/Terminate.js +21 -9
- package/dist/esm/Primitive/symbol/Registry.js +15 -13
- package/dist/esm/Primitive/undefined/Registry.js +6 -4
- package/dist/esm/Schema/Core.js +9 -1
- package/dist/esm/Schema/Registry.js +27 -7
- package/dist/esm/Utility/DeepMerge.js +47 -6
- package/dist/esm/internal.js +1 -0
- package/dist/types/Error/index.d.ts +71 -6
- package/dist/types/Fabricator/Fabricate.d.ts +63 -0
- package/dist/types/Fabricator/Types.d.ts +13 -3
- package/dist/types/Harnessing/Core.d.ts +7 -7
- package/dist/types/Instance/Core.d.ts +15 -8
- package/dist/types/Instance/Stack/Visible.d.ts +10 -0
- package/dist/types/Instance/Types.d.ts +30 -15
- package/dist/types/Primitive/boolean/Registry.d.ts +6 -1
- package/dist/types/Primitive/date/Defaults.d.ts +11 -0
- package/dist/types/Primitive/date/Registry.d.ts +13 -9
- package/dist/types/Primitive/date/Types.d.ts +10 -0
- package/dist/types/Primitive/index.d.ts +2 -0
- package/dist/types/Primitive/null/Registry.d.ts +6 -1
- package/dist/types/Primitive/number/Registry.d.ts +8 -1
- package/dist/types/Primitive/object/Fabricator.d.ts +13 -5
- package/dist/types/Primitive/object/Registry.d.ts +17 -1
- package/dist/types/Primitive/object/Types.d.ts +19 -0
- package/dist/types/Primitive/object/compute/Fabricator.d.ts +9 -8
- package/dist/types/Primitive/object/compute/Registry.d.ts +7 -1
- package/dist/types/Primitive/object/compute/Schema.d.ts +20 -0
- package/dist/types/Primitive/object/compute/Types.d.ts +10 -0
- package/dist/types/Primitive/object/omit/Registry.d.ts +7 -0
- package/dist/types/Primitive/object/omit/index.d.ts +2 -0
- package/dist/types/Primitive/object/pick/Registry.d.ts +8 -0
- package/dist/types/Primitive/object/pick/index.d.ts +2 -0
- package/dist/types/Primitive/recursive/Fabricator.d.ts +9 -7
- package/dist/types/Primitive/symbol/Registry.d.ts +6 -1
- package/dist/types/Primitive/undefined/Registry.d.ts +6 -1
- package/dist/types/Schema/Core.d.ts +16 -1
- package/dist/types/Schema/Registry.d.ts +7 -4
- package/dist/types/Utility/DeepMerge.d.ts +11 -0
- package/dist/types/index.d.ts +23 -10
- package/dist/types/internal.d.ts +8 -0
- package/package.json +4 -4
- /package/dist/esm/Primitive/number/{defaults.js → Defaults.js} +0 -0
- /package/dist/types/Primitive/number/{defaults.d.ts → Defaults.d.ts} +0 -0
|
@@ -8,7 +8,7 @@ import { isObjectOptionalFabricator } from "./optional/Fabricator.js";
|
|
|
8
8
|
function isObjectFabricator(candidate) {
|
|
9
9
|
return "object" === candidate[Kind];
|
|
10
10
|
}
|
|
11
|
-
function Fabricator(context, fields) {
|
|
11
|
+
function Fabricator(context, fields, hidden = {}) {
|
|
12
12
|
const { schema, algorithm, trace } = context;
|
|
13
13
|
const meta = schema[Meta];
|
|
14
14
|
if (meta.produce) {
|
|
@@ -40,10 +40,22 @@ function Fabricator(context, fields) {
|
|
|
40
40
|
const fabricate = (overrides, config)=>{
|
|
41
41
|
if (overrides && (null == config ? void 0 : config.validate) !== false) schema.override(overrides);
|
|
42
42
|
const fabricated = {};
|
|
43
|
-
const deferred =
|
|
44
|
-
|
|
43
|
+
const deferred = new Map();
|
|
44
|
+
const entries = [
|
|
45
|
+
...Object.entries(hidden).map(([k, v])=>[
|
|
46
|
+
k,
|
|
47
|
+
v,
|
|
48
|
+
false
|
|
49
|
+
]),
|
|
50
|
+
...Object.entries(fields).map(([k, v])=>[
|
|
51
|
+
k,
|
|
52
|
+
v,
|
|
53
|
+
true
|
|
54
|
+
])
|
|
55
|
+
];
|
|
56
|
+
for (const [k, v, visible] of entries){
|
|
45
57
|
if (isPollutionKey(k)) throw new FabricatorError.PrototypePollutionError(k);
|
|
46
|
-
if (overrides && k in overrides) {
|
|
58
|
+
if (visible && overrides && k in overrides) {
|
|
47
59
|
const value = overrides[k];
|
|
48
60
|
if (value === Omitted) continue;
|
|
49
61
|
fabricated[k] = isObjectFabricator(v) ? v.fabricate(value, {
|
|
@@ -54,15 +66,11 @@ function Fabricator(context, fields) {
|
|
|
54
66
|
if (isObjectOmittableFabricator(v) || isObjectOptionalFabricator(v)) {
|
|
55
67
|
const value = v.fabricate();
|
|
56
68
|
if (value !== Omitted) fabricated[k] = value;
|
|
57
|
-
} else if (isObjectComputeFabricator(v)) deferred.
|
|
58
|
-
k,
|
|
59
|
-
v
|
|
60
|
-
]);
|
|
69
|
+
} else if (isObjectComputeFabricator(v)) deferred.set(k, v);
|
|
61
70
|
else fabricated[k] = v.fabricate();
|
|
62
71
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
});
|
|
72
|
+
resolveComputed(deferred, fabricated);
|
|
73
|
+
for (const k of Object.keys(hidden))delete fabricated[k];
|
|
66
74
|
return fabricated;
|
|
67
75
|
};
|
|
68
76
|
return {
|
|
@@ -74,4 +82,18 @@ function Fabricator(context, fields) {
|
|
|
74
82
|
schema
|
|
75
83
|
};
|
|
76
84
|
}
|
|
85
|
+
function resolveComputed(deferred, fabricated) {
|
|
86
|
+
const resolved = new Set();
|
|
87
|
+
const resolve = (key)=>{
|
|
88
|
+
if (resolved.has(key)) return;
|
|
89
|
+
const field = deferred.get(key);
|
|
90
|
+
if (!field) return;
|
|
91
|
+
for (const dependency of field[Meta].dependencies)resolve(dependency);
|
|
92
|
+
fabricated[key] = field.fabricate({
|
|
93
|
+
fabricated
|
|
94
|
+
});
|
|
95
|
+
resolved.add(key);
|
|
96
|
+
};
|
|
97
|
+
for (const key of deferred.keys())resolve(key);
|
|
98
|
+
}
|
|
77
99
|
export { Fabricator, isObjectFabricator };
|
|
@@ -8,6 +8,7 @@ import { isNullableSchema } from "../nullable/Schema.js";
|
|
|
8
8
|
import { isNullishSchema } from "../nullish/Schema.js";
|
|
9
9
|
import { isUndefinableSchema } from "../undefinable/Schema.js";
|
|
10
10
|
import compute_0 from "./compute/index.js";
|
|
11
|
+
import { isObjectComputeSchema } from "./compute/Schema.js";
|
|
11
12
|
import { isObjectOmittableSchema } from "./omittable/Schema.js";
|
|
12
13
|
import { isObjectOptionalSchema } from "./optional/Schema.js";
|
|
13
14
|
import { Schema, isObjectSchema } from "./Schema.js";
|
|
@@ -17,10 +18,23 @@ function normalizeDefinition(definition) {
|
|
|
17
18
|
toSchema(v)
|
|
18
19
|
]));
|
|
19
20
|
}
|
|
20
|
-
function
|
|
21
|
+
function unhide(hidden, added) {
|
|
22
|
+
if (!hidden) return;
|
|
23
|
+
const remaining = Object.fromEntries(Object.entries(hidden).filter(([key])=>!(key in added)));
|
|
24
|
+
return Object.keys(remaining).length > 0 ? remaining : void 0;
|
|
25
|
+
}
|
|
26
|
+
function make(definition, refinements, adaptations, hidden) {
|
|
27
|
+
assertAcyclic({
|
|
28
|
+
...hidden,
|
|
29
|
+
...definition
|
|
30
|
+
});
|
|
21
31
|
const schema = Schema({
|
|
22
32
|
[Kind]: "object",
|
|
23
|
-
[Meta]: {
|
|
33
|
+
[Meta]: hidden ? {
|
|
34
|
+
definition,
|
|
35
|
+
refinements,
|
|
36
|
+
hidden
|
|
37
|
+
} : {
|
|
24
38
|
definition,
|
|
25
39
|
refinements
|
|
26
40
|
},
|
|
@@ -32,10 +46,10 @@ function make(definition, refinements, adaptations) {
|
|
|
32
46
|
base: definition
|
|
33
47
|
}));
|
|
34
48
|
const merged = shallowMerge(definition, extension);
|
|
35
|
-
return make(merged, refinements, adaptations);
|
|
49
|
+
return make(merged, refinements, adaptations, unhide(hidden, extension));
|
|
36
50
|
},
|
|
37
51
|
refine: (refiner)=>{
|
|
38
|
-
const compute = compute_0();
|
|
52
|
+
const compute = compute_0(Object.keys(definition));
|
|
39
53
|
const refinement = normalizeDefinition(refiner({
|
|
40
54
|
base: schema,
|
|
41
55
|
compute
|
|
@@ -43,16 +57,16 @@ function make(definition, refinements, adaptations) {
|
|
|
43
57
|
return make(shallowMerge(definition, refinement), [
|
|
44
58
|
...refinements,
|
|
45
59
|
refinement
|
|
46
|
-
], adaptations);
|
|
60
|
+
], adaptations, unhide(hidden, refinement));
|
|
47
61
|
},
|
|
48
|
-
adapt: (adapter, produce)=>make(definition, refinements, mergeAdaptations(adaptations, adapter, produce)),
|
|
62
|
+
adapt: (adapter, produce)=>make(definition, refinements, mergeAdaptations(adaptations, adapter, produce), hidden),
|
|
49
63
|
override: (override)=>{
|
|
50
64
|
const overridden = {
|
|
51
65
|
...definition
|
|
52
66
|
};
|
|
53
67
|
for (const key of Object.keys(override)){
|
|
54
68
|
const fieldSchema = definition[key];
|
|
55
|
-
if (!fieldSchema) throw new FabricatorError.
|
|
69
|
+
if (!fieldSchema) throw new FabricatorError.UnknownFieldError(key, Object.keys(definition), "override");
|
|
56
70
|
const value = override[key];
|
|
57
71
|
if (isObjectSchema(fieldSchema)) {
|
|
58
72
|
if (!isPlainObject(value)) throw new FabricatorError.InvalidOverrideValueError(key, "object", value);
|
|
@@ -132,15 +146,81 @@ function make(definition, refinements, adaptations) {
|
|
|
132
146
|
[Fixed]: value
|
|
133
147
|
};
|
|
134
148
|
}
|
|
135
|
-
return make(overridden, refinements, adaptations);
|
|
149
|
+
return make(overridden, refinements, adaptations, hidden);
|
|
136
150
|
}
|
|
137
151
|
});
|
|
138
152
|
return schema;
|
|
139
153
|
}
|
|
140
154
|
function rehydrate(schema) {
|
|
141
|
-
const rehydrated = make(schema[Meta].definition, schema[Meta].refinements, schema[Adaptation]);
|
|
155
|
+
const rehydrated = make(schema[Meta].definition, schema[Meta].refinements, schema[Adaptation], schema[Meta].hidden);
|
|
142
156
|
return schema[Meta].produce ? rehydrated.as(schema[Meta].produce) : rehydrated;
|
|
143
157
|
}
|
|
158
|
+
function dependenciesOf(entry) {
|
|
159
|
+
if (Fixed in entry) return [];
|
|
160
|
+
if (!isObjectComputeSchema(entry)) return [];
|
|
161
|
+
return entry[Meta].dependencies;
|
|
162
|
+
}
|
|
163
|
+
function assertAcyclic(all) {
|
|
164
|
+
const visiting = [];
|
|
165
|
+
const visited = new Set();
|
|
166
|
+
const visit = (key)=>{
|
|
167
|
+
if (visited.has(key)) return;
|
|
168
|
+
const entry = all[key];
|
|
169
|
+
if (!entry) return;
|
|
170
|
+
if (visiting.includes(key)) {
|
|
171
|
+
const loop = visiting.slice(visiting.indexOf(key));
|
|
172
|
+
throw new FabricatorError.ComputeDependencyCycleError([
|
|
173
|
+
...loop,
|
|
174
|
+
key
|
|
175
|
+
]);
|
|
176
|
+
}
|
|
177
|
+
visiting.push(key);
|
|
178
|
+
for (const dependency of dependenciesOf(entry))visit(dependency);
|
|
179
|
+
visiting.pop();
|
|
180
|
+
visited.add(key);
|
|
181
|
+
};
|
|
182
|
+
for (const key of Object.keys(all))visit(key);
|
|
183
|
+
}
|
|
184
|
+
function requiredKeys(kept, all) {
|
|
185
|
+
const needed = new Set(kept);
|
|
186
|
+
const pending = [
|
|
187
|
+
...kept
|
|
188
|
+
];
|
|
189
|
+
while(pending.length > 0){
|
|
190
|
+
const entry = all[pending.pop()];
|
|
191
|
+
if (!entry) continue;
|
|
192
|
+
for (const dependency of dependenciesOf(entry))if (!needed.has(dependency)) {
|
|
193
|
+
needed.add(dependency);
|
|
194
|
+
pending.push(dependency);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return needed;
|
|
198
|
+
}
|
|
199
|
+
function selectEntries(definition, keys) {
|
|
200
|
+
return Object.fromEntries(Object.entries(definition).filter(([key])=>keys.has(key)));
|
|
201
|
+
}
|
|
202
|
+
function subset(source, keys, operation) {
|
|
203
|
+
const { definition, refinements, hidden } = source[Meta];
|
|
204
|
+
const available = Object.keys(definition);
|
|
205
|
+
for (const key of keys)if (!available.includes(key)) throw new FabricatorError.UnknownFieldError(key, available, operation);
|
|
206
|
+
const named = new Set(keys);
|
|
207
|
+
const kept = available.filter((key)=>{
|
|
208
|
+
switch(operation){
|
|
209
|
+
case "pick":
|
|
210
|
+
return named.has(key);
|
|
211
|
+
case "omit":
|
|
212
|
+
return !named.has(key);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
const keptSet = new Set(kept);
|
|
216
|
+
const all = {
|
|
217
|
+
...hidden,
|
|
218
|
+
...definition
|
|
219
|
+
};
|
|
220
|
+
const required = requiredKeys(kept, all);
|
|
221
|
+
const hiddenKeys = new Set(Object.keys(all).filter((key)=>required.has(key) && !keptSet.has(key)));
|
|
222
|
+
return make(selectEntries(definition, keptSet), refinements.map((refinement)=>selectEntries(refinement, keptSet)).filter((refinement)=>Object.keys(refinement).length > 0), void 0, hiddenKeys.size > 0 ? selectEntries(all, hiddenKeys) : void 0);
|
|
223
|
+
}
|
|
144
224
|
const Registry = (definition)=>make(normalizeDefinition(definition), []);
|
|
145
225
|
export default Registry;
|
|
146
|
-
export { rehydrate };
|
|
226
|
+
export { rehydrate, subset };
|
|
@@ -12,13 +12,20 @@ function Fabricator(context) {
|
|
|
12
12
|
trace,
|
|
13
13
|
fabricate: (params)=>{
|
|
14
14
|
if (!params) throw new FabricatorError.DetachedComputeError();
|
|
15
|
-
const
|
|
16
|
-
|
|
15
|
+
const meta = schema[Meta];
|
|
16
|
+
const fabricated = meta.narrowed ? narrow(params.fabricated, meta.dependencies) : params.fabricated;
|
|
17
|
+
const resolved = meta.resolve({
|
|
18
|
+
fabricated
|
|
17
19
|
});
|
|
18
|
-
const sourceKind =
|
|
20
|
+
const sourceKind = meta.source[Kind];
|
|
19
21
|
if (violatesKind(sourceKind, resolved)) throw new FabricatorError.ComputeResultMismatchError(sourceKind, resolved);
|
|
20
22
|
return resolved;
|
|
21
23
|
}
|
|
22
24
|
};
|
|
23
25
|
}
|
|
26
|
+
function narrow(fabricated, dependencies) {
|
|
27
|
+
const narrowed = {};
|
|
28
|
+
for (const key of dependencies)if (key in fabricated) narrowed[key] = fabricated[key];
|
|
29
|
+
return narrowed;
|
|
30
|
+
}
|
|
24
31
|
export { Fabricator, isObjectComputeFabricator };
|
|
@@ -1,14 +1,31 @@
|
|
|
1
|
+
import { FabricatorError } from "../../../Error/index.js";
|
|
1
2
|
import { Kind, Meta } from "../../../Types.js";
|
|
2
3
|
import { Schema } from "./Schema.js";
|
|
3
|
-
function make() {
|
|
4
|
+
function make(base) {
|
|
4
5
|
return (source)=>({
|
|
5
6
|
as: (resolve)=>Schema({
|
|
6
7
|
[Kind]: "object.compute",
|
|
7
8
|
[Meta]: {
|
|
8
9
|
source,
|
|
9
|
-
resolve
|
|
10
|
+
resolve,
|
|
11
|
+
dependencies: base,
|
|
12
|
+
narrowed: false
|
|
10
13
|
}
|
|
11
|
-
})
|
|
14
|
+
}),
|
|
15
|
+
from: (keys)=>{
|
|
16
|
+
for (const key of keys)if (!base.includes(key)) throw new FabricatorError.UnknownFieldError(key, base, "compute from");
|
|
17
|
+
return {
|
|
18
|
+
as: (resolve)=>Schema({
|
|
19
|
+
[Kind]: "object.compute",
|
|
20
|
+
[Meta]: {
|
|
21
|
+
source,
|
|
22
|
+
resolve,
|
|
23
|
+
dependencies: keys,
|
|
24
|
+
narrowed: true
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
};
|
|
28
|
+
}
|
|
12
29
|
});
|
|
13
30
|
}
|
|
14
31
|
const Registry = make;
|
|
@@ -58,15 +58,24 @@ function terminateAt(schema, path) {
|
|
|
58
58
|
case "object":
|
|
59
59
|
{
|
|
60
60
|
const s = schema;
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
const terminateFields = (definition)=>{
|
|
62
|
+
const next = {};
|
|
63
|
+
for (const key of Object.keys(definition))next[key] = terminateAt(definition[key], [
|
|
64
|
+
...path,
|
|
65
|
+
key
|
|
66
|
+
]);
|
|
67
|
+
return next;
|
|
68
|
+
};
|
|
69
|
+
const definition = terminateFields(s[Meta].definition);
|
|
70
|
+
if (!s[Meta].hidden) return withMeta(s, {
|
|
71
|
+
...s[Meta],
|
|
72
|
+
definition
|
|
73
|
+
});
|
|
74
|
+
const hidden = terminateFields(s[Meta].hidden);
|
|
67
75
|
return withMeta(s, {
|
|
68
76
|
...s[Meta],
|
|
69
|
-
definition
|
|
77
|
+
definition,
|
|
78
|
+
hidden
|
|
70
79
|
});
|
|
71
80
|
}
|
|
72
81
|
case "tuple":
|
|
@@ -168,8 +177,11 @@ function containsSelf(schema) {
|
|
|
168
177
|
case "object":
|
|
169
178
|
{
|
|
170
179
|
const s = schema;
|
|
171
|
-
const
|
|
172
|
-
|
|
180
|
+
const all = {
|
|
181
|
+
...s[Meta].hidden,
|
|
182
|
+
...s[Meta].definition
|
|
183
|
+
};
|
|
184
|
+
return Object.keys(all).some((key)=>containsSelf(all[key]));
|
|
173
185
|
}
|
|
174
186
|
case "tuple":
|
|
175
187
|
{
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { Adaptation, Kind, Meta, Produces } from "../../Types.js";
|
|
2
2
|
import { Schema } from "./Schema.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
[Meta]: {},
|
|
7
|
-
[Produces]: void 0,
|
|
8
|
-
[Adaptation]: void 0
|
|
9
|
-
}),
|
|
10
|
-
keyed: (key)=>Schema({
|
|
3
|
+
function registry() {
|
|
4
|
+
return {
|
|
5
|
+
...Schema({
|
|
11
6
|
[Kind]: "symbol",
|
|
12
|
-
[Meta]: {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
})
|
|
16
|
-
|
|
7
|
+
[Meta]: {},
|
|
8
|
+
[Produces]: void 0,
|
|
9
|
+
[Adaptation]: void 0
|
|
10
|
+
}),
|
|
11
|
+
keyed: (key)=>Schema({
|
|
12
|
+
[Kind]: "symbol",
|
|
13
|
+
[Meta]: {
|
|
14
|
+
key
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
};
|
|
18
|
+
}
|
|
17
19
|
const Registry = registry;
|
|
18
20
|
export default Registry;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Kind, Meta } from "../../Types.js";
|
|
2
2
|
import { Schema } from "./Schema.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
function registry() {
|
|
4
|
+
return Schema({
|
|
5
|
+
[Kind]: "undefined",
|
|
6
|
+
[Meta]: {}
|
|
7
|
+
});
|
|
8
|
+
}
|
|
7
9
|
const Registry = registry;
|
|
8
10
|
export default Registry;
|
package/dist/esm/Schema/Core.js
CHANGED
|
@@ -11,6 +11,14 @@ function toSchema(value) {
|
|
|
11
11
|
[Meta]: value[Meta]
|
|
12
12
|
};
|
|
13
13
|
}
|
|
14
|
+
function isBuildable(candidate) {
|
|
15
|
+
if ("object" != typeof candidate || null === candidate) return false;
|
|
16
|
+
if ("string" != typeof candidate[Kind]) return false;
|
|
17
|
+
return Meta in candidate;
|
|
18
|
+
}
|
|
19
|
+
function isSchema(candidate) {
|
|
20
|
+
return isBuildable(candidate) && !("fabricate" in candidate);
|
|
21
|
+
}
|
|
14
22
|
function toValueKind(schema) {
|
|
15
23
|
switch(schema[Kind]){
|
|
16
24
|
case "object.compute":
|
|
@@ -77,4 +85,4 @@ function violatesKind(kind, value) {
|
|
|
77
85
|
return never(kind);
|
|
78
86
|
}
|
|
79
87
|
}
|
|
80
|
-
export { toSchema, toValueKind, violatesKind };
|
|
88
|
+
export { isBuildable, isSchema, toSchema, toValueKind, violatesKind };
|
|
@@ -5,13 +5,33 @@ const Registry = (registrations)=>{
|
|
|
5
5
|
T: registrations,
|
|
6
6
|
augment: deepMerge
|
|
7
7
|
})));
|
|
8
|
-
return {
|
|
9
|
-
...registrations,
|
|
8
|
+
return deepMerge(registrations, {
|
|
10
9
|
extend
|
|
11
|
-
};
|
|
10
|
+
});
|
|
12
11
|
};
|
|
13
|
-
|
|
14
|
-
const { Primitive: _, ...
|
|
15
|
-
return
|
|
16
|
-
|
|
12
|
+
function builders() {
|
|
13
|
+
const { Primitive: _, ...rest } = __rspack_external__Primitive_index_js_8ad84a20;
|
|
14
|
+
return {
|
|
15
|
+
...rest,
|
|
16
|
+
get boolean () {
|
|
17
|
+
return __rspack_external__Primitive_index_js_8ad84a20.boolean();
|
|
18
|
+
},
|
|
19
|
+
get date () {
|
|
20
|
+
return __rspack_external__Primitive_index_js_8ad84a20.date();
|
|
21
|
+
},
|
|
22
|
+
get null () {
|
|
23
|
+
return __rspack_external__Primitive_index_js_8ad84a20["null"]();
|
|
24
|
+
},
|
|
25
|
+
get number () {
|
|
26
|
+
return __rspack_external__Primitive_index_js_8ad84a20.number();
|
|
27
|
+
},
|
|
28
|
+
get symbol () {
|
|
29
|
+
return __rspack_external__Primitive_index_js_8ad84a20.symbol();
|
|
30
|
+
},
|
|
31
|
+
get undefined () {
|
|
32
|
+
return __rspack_external__Primitive_index_js_8ad84a20.undefined();
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
const registry = Registry({}).extend(builders);
|
|
17
37
|
export { registry };
|
|
@@ -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,6 @@
|
|
|
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";
|
|
3
4
|
export { plan, resolve } from "./Enumeration/Plan.js";
|
|
4
5
|
export { defaultAlgorithm, encode, randomSalt, toRandomSource, toStream, toStreamFromTrace } from "./Random/index.js";
|
|
5
6
|
export { normalCdf, normalInv, weighted } from "./Distribution/index.js";
|
|
@@ -45,6 +45,24 @@ export declare namespace FabricatorError {
|
|
|
45
45
|
*/
|
|
46
46
|
limit: unknown);
|
|
47
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* `initialize({ clock })`, `fork({ clock })`, or `wrap({ clock }, …)` given
|
|
50
|
+
* an instant a `Date` cannot hold exactly — a fractional or non-finite
|
|
51
|
+
* number, one past ±8.64e15, or an invalid `Date`. Rejected rather than
|
|
52
|
+
* adjusted the way `Date`'s own `TimeClip` would, so the clock a run records
|
|
53
|
+
* is the clock the caller passed.
|
|
54
|
+
*/
|
|
55
|
+
class InvalidClockError extends FabricatorError {
|
|
56
|
+
/**
|
|
57
|
+
* The rejected clock, as given — a number or a `Date`.
|
|
58
|
+
*/
|
|
59
|
+
readonly clock: unknown;
|
|
60
|
+
constructor(
|
|
61
|
+
/**
|
|
62
|
+
* The rejected clock, as given — a number or a `Date`.
|
|
63
|
+
*/
|
|
64
|
+
clock: unknown);
|
|
65
|
+
}
|
|
48
66
|
/**
|
|
49
67
|
* A `self` placeholder resolved with no `T.recursive` expanding around it —
|
|
50
68
|
* only reachable by holding a `self` reference outside the `T.recursive(...)`
|
|
@@ -213,27 +231,57 @@ export declare namespace FabricatorError {
|
|
|
213
231
|
value: unknown);
|
|
214
232
|
}
|
|
215
233
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
234
|
+
* A key names a field the object schema does not define — most often a typo,
|
|
235
|
+
* which is why the known fields are listed. One failure reached from four
|
|
236
|
+
* places: `.override()`, `T.pick`, `T.omit`, and a compute field's
|
|
237
|
+
* `.from([...])`, told apart by `operation`.
|
|
218
238
|
*/
|
|
219
|
-
class
|
|
239
|
+
class UnknownFieldError extends FabricatorError {
|
|
220
240
|
/**
|
|
221
241
|
* The name that was not found.
|
|
222
242
|
*/
|
|
223
243
|
readonly field: string;
|
|
224
244
|
/**
|
|
225
|
-
* Every field the schema does define, in definition order.
|
|
245
|
+
* Every field the schema does define, in definition order. For `"compute
|
|
246
|
+
* from"`, the fields the enclosing `.refine()` stage can read.
|
|
226
247
|
*/
|
|
227
248
|
readonly available: ReadonlyArray<string>;
|
|
249
|
+
/**
|
|
250
|
+
* What named the unknown field, worded to read as the message's verb.
|
|
251
|
+
*/
|
|
252
|
+
readonly operation: "override" | "pick" | "omit" | "compute from";
|
|
228
253
|
constructor(
|
|
229
254
|
/**
|
|
230
255
|
* The name that was not found.
|
|
231
256
|
*/
|
|
232
257
|
field: string,
|
|
233
258
|
/**
|
|
234
|
-
* Every field the schema does define, in definition order.
|
|
259
|
+
* Every field the schema does define, in definition order. For `"compute
|
|
260
|
+
* from"`, the fields the enclosing `.refine()` stage can read.
|
|
261
|
+
*/
|
|
262
|
+
available: ReadonlyArray<string>,
|
|
263
|
+
/**
|
|
264
|
+
* What named the unknown field, worded to read as the message's verb.
|
|
265
|
+
*/
|
|
266
|
+
operation: "override" | "pick" | "omit" | "compute from");
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Compute fields whose declared dependencies form a loop, so none of them can
|
|
270
|
+
* resolve first. A compute field only names fields that already exist, but a
|
|
271
|
+
* later `.refine()` or `.extend()` can replace one of those with a compute
|
|
272
|
+
* field that reads it back. Thrown by that call, while building the schema,
|
|
273
|
+
* never at fabrication.
|
|
274
|
+
*/
|
|
275
|
+
class ComputeDependencyCycleError extends FabricatorError {
|
|
276
|
+
/**
|
|
277
|
+
* The compute fields on the loop, in the order they were reached.
|
|
235
278
|
*/
|
|
236
|
-
|
|
279
|
+
readonly fields: ReadonlyArray<string>;
|
|
280
|
+
constructor(
|
|
281
|
+
/**
|
|
282
|
+
* The compute fields on the loop, in the order they were reached.
|
|
283
|
+
*/
|
|
284
|
+
fields: ReadonlyArray<string>);
|
|
237
285
|
}
|
|
238
286
|
/**
|
|
239
287
|
* An override value does not fit the field it is meant to replace. Raised for
|
|
@@ -548,6 +596,23 @@ export declare namespace FabricatorError {
|
|
|
548
596
|
*/
|
|
549
597
|
given: string);
|
|
550
598
|
}
|
|
599
|
+
/**
|
|
600
|
+
* `fabricate(...)` handed anything but a Schema: a built Fabricator, a bare
|
|
601
|
+
* builder that still needs configuring (`T.array(x)`, `T.recursive(f)`), or a
|
|
602
|
+
* value that is no schema at all. Its type already rejects each; this is the
|
|
603
|
+
* runtime guard for an untyped caller.
|
|
604
|
+
*/
|
|
605
|
+
class FabricateArgumentError extends FabricatorError {
|
|
606
|
+
/**
|
|
607
|
+
* What `fabricate(...)` was handed.
|
|
608
|
+
*/
|
|
609
|
+
readonly argument: unknown;
|
|
610
|
+
constructor(
|
|
611
|
+
/**
|
|
612
|
+
* What `fabricate(...)` was handed.
|
|
613
|
+
*/
|
|
614
|
+
argument: unknown);
|
|
615
|
+
}
|
|
551
616
|
/**
|
|
552
617
|
* `wrap(overlay, block)` handed an `async` block while the lineage's ambient
|
|
553
618
|
* carrier is the synchronous one (`Instance/Stack/Sync.ts`).
|