@ghostry/fabricator 0.0.11 → 0.0.12

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.
Files changed (49) hide show
  1. package/dist/esm/Enumeration/Enumerate.js +2 -4
  2. package/dist/esm/Error/index.js +34 -6
  3. package/dist/esm/Fabricator/Constructor.js +40 -33
  4. package/dist/esm/Fabricator/Fabricate.js +28 -0
  5. package/dist/esm/Instance/Core.js +14 -2
  6. package/dist/esm/Instance/Stack/Visible.js +6 -1
  7. package/dist/esm/Primitive/date/Defaults.js +12 -0
  8. package/dist/esm/Primitive/date/Fabricator.js +5 -10
  9. package/dist/esm/Primitive/date/Registry.js +8 -4
  10. package/dist/esm/Primitive/index.js +2 -0
  11. package/dist/esm/Primitive/number/Fabricator.js +1 -1
  12. package/dist/esm/Primitive/number/Registry.js +1 -1
  13. package/dist/esm/Primitive/object/Fabricator.js +33 -11
  14. package/dist/esm/Primitive/object/Registry.js +90 -10
  15. package/dist/esm/Primitive/object/compute/Fabricator.js +10 -3
  16. package/dist/esm/Primitive/object/compute/Registry.js +20 -3
  17. package/dist/esm/Primitive/object/omit/Registry.js +5 -0
  18. package/dist/esm/Primitive/object/omit/index.js +2 -0
  19. package/dist/esm/Primitive/object/pick/Registry.js +5 -0
  20. package/dist/esm/Primitive/object/pick/index.js +2 -0
  21. package/dist/esm/Primitive/recursive/Fabricator.js +1 -0
  22. package/dist/esm/Primitive/recursive/Terminate.js +21 -9
  23. package/dist/types/Error/index.d.ts +71 -6
  24. package/dist/types/Fabricator/Fabricate.d.ts +61 -0
  25. package/dist/types/Fabricator/Types.d.ts +13 -3
  26. package/dist/types/Harnessing/Core.d.ts +7 -7
  27. package/dist/types/Instance/Core.d.ts +15 -8
  28. package/dist/types/Instance/Stack/Visible.d.ts +10 -0
  29. package/dist/types/Instance/Types.d.ts +30 -15
  30. package/dist/types/Primitive/date/Defaults.d.ts +11 -0
  31. package/dist/types/Primitive/date/Registry.d.ts +6 -8
  32. package/dist/types/Primitive/date/Types.d.ts +10 -0
  33. package/dist/types/Primitive/index.d.ts +2 -0
  34. package/dist/types/Primitive/object/Fabricator.d.ts +13 -5
  35. package/dist/types/Primitive/object/Registry.d.ts +17 -1
  36. package/dist/types/Primitive/object/Types.d.ts +19 -0
  37. package/dist/types/Primitive/object/compute/Fabricator.d.ts +9 -8
  38. package/dist/types/Primitive/object/compute/Registry.d.ts +7 -1
  39. package/dist/types/Primitive/object/compute/Schema.d.ts +20 -0
  40. package/dist/types/Primitive/object/compute/Types.d.ts +10 -0
  41. package/dist/types/Primitive/object/omit/Registry.d.ts +7 -0
  42. package/dist/types/Primitive/object/omit/index.d.ts +2 -0
  43. package/dist/types/Primitive/object/pick/Registry.d.ts +8 -0
  44. package/dist/types/Primitive/object/pick/index.d.ts +2 -0
  45. package/dist/types/Primitive/recursive/Fabricator.d.ts +9 -7
  46. package/dist/types/index.d.ts +23 -10
  47. package/package.json +3 -3
  48. /package/dist/esm/Primitive/number/{defaults.js → Defaults.js} +0 -0
  49. /package/dist/types/Primitive/number/{defaults.d.ts → Defaults.d.ts} +0 -0
@@ -1,15 +1,13 @@
1
1
  import { shuffle } from "../Distribution/index.js";
2
2
  import { FabricatorError } from "../Error/index.js";
3
3
  import { Constructor } from "../Fabricator/Constructor.js";
4
- import { toInnermostFrame } from "../Instance/Stack/Visible.js";
4
+ import { toEffectiveSource } from "../Instance/Stack/Visible.js";
5
5
  import { toStreamFromTrace } from "../Random/index.js";
6
6
  import { plan, resolve } from "./Plan.js";
7
7
  function enumerables(source, limits, stack, ancestry) {
8
8
  const Fabricator = Constructor(source, stack, ancestry);
9
9
  function effectiveSource() {
10
- var _ref;
11
- var _toInnermostFrame;
12
- return null != (_ref = null == (_toInnermostFrame = toInnermostFrame(stack, ancestry)) ? void 0 : _toInnermostFrame.source) ? _ref : source;
10
+ return toEffectiveSource(source, stack, ancestry);
13
11
  }
14
12
  function iterable(rebuild) {
15
13
  return {
@@ -31,6 +31,14 @@ class Error_FabricatorError extends Error {
31
31
  }
32
32
  }
33
33
  FabricatorError.InvalidCombinatorialLimitError = InvalidCombinatorialLimitError;
34
+ class InvalidClockError extends FabricatorError {
35
+ constructor(clock){
36
+ super(), _define_property(this, "clock", void 0), this.clock = clock;
37
+ this.name = "InvalidClockError";
38
+ this.message = `clock requires a valid Date or an integer number of epoch milliseconds within ±8.64e15; received ${String(clock)}.`;
39
+ }
40
+ }
41
+ FabricatorError.InvalidClockError = InvalidClockError;
34
42
  class DetachedSelfError extends FabricatorError {
35
43
  constructor(during){
36
44
  super(), _define_property(this, "during", void 0), this.during = during;
@@ -96,14 +104,22 @@ class Error_FabricatorError extends Error {
96
104
  }
97
105
  }
98
106
  FabricatorError.DeriveResultMismatchError = DeriveResultMismatchError;
99
- class UnknownOverrideFieldError extends FabricatorError {
100
- constructor(field, available){
101
- super(), _define_property(this, "field", void 0), _define_property(this, "available", void 0), this.field = field, this.available = available;
102
- this.name = "UnknownOverrideFieldError";
103
- this.message = `Cannot override unknown field: "${field}". This object schema defines ` + (0 === available.length ? "no fields." : `${available.map((k)=>`"${k}"`).join(", ")}.`);
107
+ class UnknownFieldError extends FabricatorError {
108
+ constructor(field, available, operation){
109
+ super(), _define_property(this, "field", void 0), _define_property(this, "available", void 0), _define_property(this, "operation", void 0), this.field = field, this.available = available, this.operation = operation;
110
+ this.name = "UnknownFieldError";
111
+ this.message = `Cannot ${operation} unknown field: "${field}". This object schema defines ` + (0 === available.length ? "no fields." : `${available.map((k)=>`"${k}"`).join(", ")}.`);
112
+ }
113
+ }
114
+ FabricatorError.UnknownFieldError = UnknownFieldError;
115
+ class ComputeDependencyCycleError extends FabricatorError {
116
+ constructor(fields){
117
+ super(), _define_property(this, "fields", void 0), this.fields = fields;
118
+ this.name = "ComputeDependencyCycleError";
119
+ this.message = `Computed fields depend on each other in a cycle: ${fields.map((k)=>`"${k}"`).join(" -> ")}.`;
104
120
  }
105
121
  }
106
- FabricatorError.UnknownOverrideFieldError = UnknownOverrideFieldError;
122
+ FabricatorError.ComputeDependencyCycleError = ComputeDependencyCycleError;
107
123
  class InvalidOverrideValueError extends FabricatorError {
108
124
  constructor(field, kind, value){
109
125
  super(), _define_property(this, "field", void 0), _define_property(this, "kind", void 0), _define_property(this, "value", void 0), this.field = field, this.kind = kind, this.value = value;
@@ -197,6 +213,18 @@ class Error_FabricatorError extends Error {
197
213
  }
198
214
  }
199
215
  FabricatorError.TraceKindMismatchError = TraceKindMismatchError;
216
+ class FabricateArgumentError extends FabricatorError {
217
+ constructor(argument){
218
+ super(), _define_property(this, "argument", void 0), this.argument = argument;
219
+ this.name = "FabricateArgumentError";
220
+ this.message = describe(argument);
221
+ function describe(given) {
222
+ if ("object" == typeof given && null !== given && "fabricate" in given) return "fabricate(...) takes a Schema, not a built Fabricator. Call `.fabricate()` on the Fabricator you have, or pass its `.schema` to fabricate(...).";
223
+ return `fabricate(...) takes a Schema, but was handed a ${typeof given}. A builder that still needs configuring, such as \`T.array(x)\`, becomes a Schema once you call \`.whereby(...)\` on it.`;
224
+ }
225
+ }
226
+ }
227
+ FabricatorError.FabricateArgumentError = FabricateArgumentError;
200
228
  class SynchronousStackError extends FabricatorError {
201
229
  constructor(){
202
230
  super();
@@ -1,7 +1,7 @@
1
1
  import { FabricatorError } from "../Error/index.js";
2
- import { toInnermostFrame } from "../Instance/Stack/Visible.js";
2
+ import { toEffectiveSource } from "../Instance/Stack/Visible.js";
3
3
  import { Primitive } from "../Primitive/index.js";
4
- import { isLayered, normalizeSalt } from "../Random/index.js";
4
+ import { isLayered, normalizeSalt, toRandomSource } from "../Random/index.js";
5
5
  import { toSchema } from "../Schema/Core.js";
6
6
  import { Adaptation, Fixed, Kind, Layer, Meta } from "../Types.js";
7
7
  import { inline } from "../Utility/Core.js";
@@ -187,7 +187,7 @@ function Constructor(source, stack, ancestry) {
187
187
  return Primitive.recursive.Fabricator({
188
188
  ...common,
189
189
  schema: s
190
- }, source.fork, make);
190
+ }, context.fork, make);
191
191
  }
192
192
  case "recursive.self":
193
193
  {
@@ -257,35 +257,39 @@ function Constructor(source, stack, ancestry) {
257
257
  case "object":
258
258
  {
259
259
  const s = schema;
260
- const definition = s[Meta].definition;
261
- const fields = {};
262
- for(const key in definition){
263
- const fieldSchema = definition[key];
264
- const fieldPath = [
265
- ...path,
266
- key
267
- ];
268
- fields[key] = inline(()=>{
269
- if (Fixed in fieldSchema) {
270
- const built = make(fieldSchema, fieldPath, context);
271
- const value = fieldSchema[Fixed];
272
- if (Primitive.object.compute.isObjectComputeFabricator(built)) return {
273
- ...built,
274
- [Kind]: built[Meta].source[Kind],
275
- fabricate: ()=>value
276
- };
277
- return {
278
- ...built,
279
- fabricate: ()=>value
280
- };
281
- }
282
- return make(fieldSchema, fieldPath, context);
283
- });
284
- }
260
+ const build = (definition)=>{
261
+ const fields = {};
262
+ for(const key in definition){
263
+ const fieldSchema = definition[key];
264
+ const fieldPath = [
265
+ ...path,
266
+ key
267
+ ];
268
+ fields[key] = inline(()=>{
269
+ if (Fixed in fieldSchema) {
270
+ const built = make(fieldSchema, fieldPath, context);
271
+ const value = fieldSchema[Fixed];
272
+ if (Primitive.object.compute.isObjectComputeFabricator(built)) return {
273
+ ...built,
274
+ [Kind]: built[Meta].source[Kind],
275
+ fabricate: ()=>value
276
+ };
277
+ return {
278
+ ...built,
279
+ fabricate: ()=>value
280
+ };
281
+ }
282
+ return make(fieldSchema, fieldPath, context);
283
+ });
284
+ }
285
+ return fields;
286
+ };
287
+ const fields = build(s[Meta].definition);
288
+ const hidden = s[Meta].hidden ? build(s[Meta].hidden) : {};
285
289
  return Primitive.object.Fabricator({
286
290
  ...common,
287
291
  schema: Primitive.object.rehydrate(s)
288
- }, fields);
292
+ }, fields, hidden);
289
293
  }
290
294
  default:
291
295
  throw new FabricatorError.UnknownKindError(String(kind), "construction");
@@ -312,13 +316,16 @@ function toConstructionContext(source, options, stack, ancestry) {
312
316
  path,
313
317
  kind
314
318
  }),
315
- algorithm: resolved.algorithm
319
+ algorithm: resolved.algorithm,
320
+ fork: (salt)=>toRandomSource({
321
+ salt,
322
+ algorithm: resolved.algorithm,
323
+ clock: trace.clock
324
+ })
316
325
  };
317
326
  }
318
327
  function resolveScope(source, options, stack, ancestry) {
319
- var _ref;
320
- var _toInnermostFrame;
321
- const base = null != (_ref = null == (_toInnermostFrame = toInnermostFrame(stack, ancestry)) ? void 0 : _toInnermostFrame.source) ? _ref : source;
328
+ const base = toEffectiveSource(source, stack, ancestry);
322
329
  const salt = inline(()=>{
323
330
  if (!options.salt) return;
324
331
  if (isLayered(options.salt)) return [
@@ -0,0 +1,28 @@
1
+ import { toEffectiveSource } from "../Instance/Stack/Visible.js";
2
+ import { FabricatorError } from "../Error/index.js";
3
+ import { Kind } from "../Types.js";
4
+ function isSchema(candidate) {
5
+ if ("object" != typeof candidate || null === candidate) return false;
6
+ if ("string" != typeof candidate[Kind]) return false;
7
+ return !("fabricate" in candidate);
8
+ }
9
+ function Fabricate(construct, source, stack, ancestry, fabricatorCache) {
10
+ function cacheFor(base) {
11
+ const existing = fabricatorCache.get(base);
12
+ if (existing) return existing;
13
+ const created = new WeakMap();
14
+ fabricatorCache.set(base, created);
15
+ return created;
16
+ }
17
+ function fabricatorFor(schema) {
18
+ if (!isSchema(schema)) throw new FabricatorError.FabricateArgumentError(schema);
19
+ const cache = cacheFor(toEffectiveSource(source, stack, ancestry));
20
+ const existing = cache.get(schema);
21
+ if (existing) return existing;
22
+ const built = new construct(schema);
23
+ cache.set(schema, built);
24
+ return built;
25
+ }
26
+ return (schema)=>fabricatorFor(schema).fabricate();
27
+ }
28
+ export { Fabricate };
@@ -1,6 +1,7 @@
1
1
  import { enumerables } from "../Enumeration/Enumerate.js";
2
2
  import { FabricatorError } from "../Error/index.js";
3
3
  import { Constructor } from "../Fabricator/Constructor.js";
4
+ import { Fabricate } from "../Fabricator/Fabricate.js";
4
5
  import { defaultAlgorithm, deriveClock, isLayered, normalizeSalt, toRandomSource } from "../Random/index.js";
5
6
  import { registry } from "../Schema/Registry.js";
6
7
  import { Layer } from "../Types.js";
@@ -12,6 +13,12 @@ function resolveCombinatorialLimit(limit) {
12
13
  if (!Number.isSafeInteger(limit) || limit < 1) throw new FabricatorError.InvalidCombinatorialLimitError(limit);
13
14
  return limit;
14
15
  }
16
+ function resolveInstant(clock) {
17
+ const given = clock.valueOf();
18
+ const instant = new Date(given).valueOf();
19
+ if (instant !== given) throw new FabricatorError.InvalidClockError(clock);
20
+ return instant;
21
+ }
15
22
  function resolveClock(config) {
16
23
  return "number" == typeof config.clock ? config.clock : deriveClock(config.algorithm, normalizeSalt(config.salt));
17
24
  }
@@ -32,7 +39,7 @@ function overlay(base, over) {
32
39
  const clock = inline(()=>{
33
40
  var _base_clock;
34
41
  if ("derived" === over.clock) return "derived";
35
- if (void 0 !== over.clock) return over.clock.valueOf();
42
+ if (void 0 !== over.clock) return resolveInstant(over.clock);
36
43
  return null != (_base_clock = base.clock) ? _base_clock : Date.now();
37
44
  });
38
45
  return {
@@ -46,6 +53,7 @@ function overlay(base, over) {
46
53
  };
47
54
  }
48
55
  function instantiate(config, stack, parent) {
56
+ var _ref;
49
57
  const ancestry = void 0 === parent ? [
50
58
  mint()
51
59
  ] : [
@@ -59,12 +67,15 @@ function instantiate(config, stack, parent) {
59
67
  });
60
68
  const Fabricator = Constructor(source, stack, ancestry);
61
69
  const { combinatorial, coverage } = enumerables(source, config.limits, stack, ancestry);
70
+ const fabricatorCache = null != (_ref = null == parent ? void 0 : parent.fabricatorCache) ? _ref : new WeakMap();
71
+ const fabricate = Fabricate(Fabricator, source, stack, ancestry, fabricatorCache);
62
72
  function derive(over) {
63
73
  const derived = overlay(config, over);
64
74
  return {
65
75
  ...instantiate(derived, stack, {
66
76
  ancestry,
67
- root: instance.root
77
+ root: instance.root,
78
+ fabricatorCache
68
79
  }),
69
80
  config: derived
70
81
  };
@@ -122,6 +133,7 @@ function instantiate(config, stack, parent) {
122
133
  return null != (_ref = null == parent ? void 0 : parent.root) ? _ref : instance;
123
134
  },
124
135
  Fabricator,
136
+ fabricate,
125
137
  combinatorial,
126
138
  coverage,
127
139
  salt: source.salt,
@@ -10,4 +10,9 @@ function toInnermostFrame(stack, ancestry) {
10
10
  const frames = stack.visible(ancestry);
11
11
  return frames[frames.length - 1];
12
12
  }
13
- export { toInnermostFrame, toVisible };
13
+ function toEffectiveSource(source, stack, ancestry) {
14
+ var _ref;
15
+ var _toInnermostFrame;
16
+ return null != (_ref = null == (_toInnermostFrame = toInnermostFrame(stack, ancestry)) ? void 0 : _toInnermostFrame.source) ? _ref : source;
17
+ }
18
+ export { toEffectiveSource, toInnermostFrame, toVisible };
@@ -0,0 +1,12 @@
1
+ import { MAX_TIME } from "../../Types.js";
2
+ const unboundedEpoch = {
3
+ min: {
4
+ value: -MAX_TIME,
5
+ exclusive: false
6
+ },
7
+ max: {
8
+ value: MAX_TIME,
9
+ exclusive: false
10
+ }
11
+ };
12
+ export { unboundedEpoch };
@@ -2,6 +2,7 @@ import { assertNonemptyDiscrete, effectiveDiscrete, epochBound } from "../../Bou
2
2
  import { Distribution, sampler } from "../../Distribution/index.js";
3
3
  import { toStreamFromTrace } from "../../Random/index.js";
4
4
  import { Kind, MAX_TIME, Meta } from "../../Types.js";
5
+ import { unboundedEpoch } from "./Defaults.js";
5
6
  import { Schema } from "./Schema.js";
6
7
  function Fabricator(context) {
7
8
  const { schema, algorithm, trace } = context;
@@ -26,10 +27,7 @@ function Fabricator(context) {
26
27
  const whereby = meta.whereby;
27
28
  if ("past" === meta.mode) {
28
29
  const fabricate = whereby ? drawPastOrFuture({
29
- min: whereby.min ? epochBound(whereby.min) : {
30
- value: -MAX_TIME,
31
- exclusive: false
32
- },
30
+ min: whereby.min ? epochBound(whereby.min) : unboundedEpoch.min,
33
31
  max: {
34
32
  value: clock,
35
33
  exclusive: false
@@ -49,10 +47,7 @@ function Fabricator(context) {
49
47
  value: clock,
50
48
  exclusive: false
51
49
  },
52
- max: whereby.max ? epochBound(whereby.max) : {
53
- value: MAX_TIME,
54
- exclusive: false
55
- }
50
+ max: whereby.max ? epochBound(whereby.max) : unboundedEpoch.max
56
51
  }, "T.date.future", whereby.distribution, stream) : ()=>new Date(clock + stream.next() * (MAX_TIME - clock));
57
52
  return {
58
53
  [Kind]: "date",
@@ -63,8 +58,8 @@ function Fabricator(context) {
63
58
  };
64
59
  }
65
60
  const fabricate = whereby ? draw({
66
- min: epochBound(whereby.min),
67
- max: epochBound(whereby.max)
61
+ min: whereby.min ? epochBound(whereby.min) : unboundedEpoch.min,
62
+ max: whereby.max ? epochBound(whereby.max) : unboundedEpoch.max
68
63
  }, whereby.distribution, stream) : ()=>new Date((2 * stream.next() - 1) * MAX_TIME);
69
64
  return {
70
65
  [Kind]: "date",
@@ -1,5 +1,6 @@
1
1
  import { assertNonemptyDiscrete, epochBound, toBound } from "../../Bound.js";
2
2
  import { Adaptation, Kind, Meta, Produces } from "../../Types.js";
3
+ import { unboundedEpoch } from "./Defaults.js";
3
4
  import { Schema } from "./Schema.js";
4
5
  const registry = {
5
6
  ...Schema({
@@ -8,12 +9,16 @@ const registry = {
8
9
  [Produces]: void 0,
9
10
  [Adaptation]: void 0
10
11
  }),
11
- whereby: (whereby)=>Schema({
12
+ whereby: (whereby)=>{
13
+ const converted = toWhereby(whereby);
14
+ assertNonemptyDiscrete("T.date.whereby", converted.min ? epochBound(converted.min) : unboundedEpoch.min, converted.max ? epochBound(converted.max) : unboundedEpoch.max);
15
+ return Schema({
12
16
  [Kind]: "date",
13
17
  [Meta]: {
14
- whereby: toWhereby(whereby)
18
+ whereby: converted
15
19
  }
16
- }),
20
+ });
21
+ },
17
22
  past: {
18
23
  ...Schema({
19
24
  [Kind]: "date",
@@ -49,7 +54,6 @@ const Registry = registry;
49
54
  function toWhereby(input) {
50
55
  const min = void 0 === input.min ? void 0 : toBound(input.min);
51
56
  const max = void 0 === input.max ? void 0 : toBound(input.max);
52
- if (void 0 !== min && void 0 !== max) assertNonemptyDiscrete("T.date.whereby", epochBound(min), epochBound(max));
53
57
  return void 0 === input.distribution ? {
54
58
  min,
55
59
  max
@@ -12,8 +12,10 @@ export { default as nullable } from "./nullable/index.js";
12
12
  export { default as nullish } from "./nullish/index.js";
13
13
  export { default as number } from "./number/index.js";
14
14
  export { default as object } from "./object/index.js";
15
+ export { default as omit } from "./object/omit/index.js";
15
16
  export { default as omittable } from "./object/omittable/index.js";
16
17
  export { default as optional } from "./object/optional/index.js";
18
+ export { default as pick } from "./object/pick/index.js";
17
19
  export { default as opaque } from "./opaque/index.js";
18
20
  export { default as record } from "./record/index.js";
19
21
  export { default as recursive } from "./recursive/index.js";
@@ -2,7 +2,7 @@ import { constrainContinuous, effectiveDiscrete } from "../../Bound.js";
2
2
  import { Distribution, discreteSampler, sampler } from "../../Distribution/index.js";
3
3
  import { toStreamFromTrace } from "../../Random/index.js";
4
4
  import { Kind, Meta } from "../../Types.js";
5
- import { unboundedContinuous, unboundedDiscrete } from "./defaults.js";
5
+ import { unboundedContinuous, unboundedDiscrete } from "./Defaults.js";
6
6
  import { Schema } from "./Schema.js";
7
7
  function Fabricator(context) {
8
8
  var _ref, _ref1, _range_distribution;
@@ -1,7 +1,7 @@
1
1
  import { assertNonemptyContinuous, assertNonemptyDiscrete, toBound } from "../../Bound.js";
2
2
  import { Adaptation, Kind, Meta, Produces } from "../../Types.js";
3
3
  import bigint from "../bigint/index.js";
4
- import { unboundedContinuous, unboundedDiscrete } from "./defaults.js";
4
+ import { unboundedContinuous, unboundedDiscrete } from "./Defaults.js";
5
5
  import { Schema } from "./Schema.js";
6
6
  const registry = {
7
7
  ...Schema({
@@ -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
- for (const [k, v] of Object.entries(fields)){
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.push([
58
- k,
59
- v
60
- ]);
69
+ } else if (isObjectComputeFabricator(v)) deferred.set(k, v);
61
70
  else fabricated[k] = v.fabricate();
62
71
  }
63
- for (const [k, v] of deferred)fabricated[k] = v.fabricate({
64
- fabricated
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 make(definition, refinements, adaptations) {
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.UnknownOverrideFieldError(key, Object.keys(definition));
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 resolved = schema[Meta].resolve({
16
- fabricated: params.fabricated
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 = schema[Meta].source[Kind];
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 };