@ghostry/fabricator 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/dist/esm/Bound.js +74 -1
  2. package/dist/esm/Distribution/index.js +103 -2
  3. package/dist/esm/Enumeration/Enumerate.js +10 -5
  4. package/dist/esm/Enumeration/Plan.js +2 -2
  5. package/dist/esm/Error/index.js +96 -27
  6. package/dist/esm/Fabricator/Constructor.js +7 -5
  7. package/dist/esm/Instance/Core.js +3 -0
  8. package/dist/esm/Primitive/array/Admits.js +18 -0
  9. package/dist/esm/Primitive/array/Fabricator.js +20 -2
  10. package/dist/esm/Primitive/array/Registry.js +32 -0
  11. package/dist/esm/Primitive/array/Schema.js +8 -0
  12. package/dist/esm/Primitive/bigint/Admits.js +11 -0
  13. package/dist/esm/Primitive/bigint/Fabricator.js +16 -23
  14. package/dist/esm/Primitive/bigint/Registry.js +17 -6
  15. package/dist/esm/Primitive/bigint/Schema.js +8 -0
  16. package/dist/esm/Primitive/date/Admits.js +22 -0
  17. package/dist/esm/Primitive/date/Defaults.js +27 -1
  18. package/dist/esm/Primitive/date/Fabricator.js +32 -34
  19. package/dist/esm/Primitive/date/Registry.js +80 -13
  20. package/dist/esm/Primitive/date/Schema.js +8 -0
  21. package/dist/esm/Primitive/enum/Fabricator.js +14 -2
  22. package/dist/esm/Primitive/enum/Registry.js +38 -25
  23. package/dist/esm/Primitive/index.js +1 -0
  24. package/dist/esm/Primitive/number/Admits.js +27 -0
  25. package/dist/esm/Primitive/number/Fabricator.js +18 -9
  26. package/dist/esm/Primitive/number/Registry.js +55 -4
  27. package/dist/esm/Primitive/number/Schema.js +10 -3
  28. package/dist/esm/Primitive/object/Fabricator.js +2 -0
  29. package/dist/esm/Primitive/record/Admits.js +42 -0
  30. package/dist/esm/Primitive/record/Fabricator.js +49 -19
  31. package/dist/esm/Primitive/record/Registry.js +29 -0
  32. package/dist/esm/Primitive/record/Schema.js +8 -0
  33. package/dist/esm/Primitive/recursive/Fabricator.js +3 -2
  34. package/dist/esm/Primitive/recursive/Terminate.js +16 -2
  35. package/dist/esm/Primitive/string/Admits.js +26 -0
  36. package/dist/esm/Primitive/string/Capacity.js +81 -0
  37. package/dist/esm/Primitive/string/Fabricator.js +23 -43
  38. package/dist/esm/Primitive/string/Registry.js +31 -9
  39. package/dist/esm/Primitive/string/Schema.js +10 -3
  40. package/dist/esm/Primitive/string/Sources.js +41 -0
  41. package/dist/esm/Primitive/unique/Registry.js +22 -0
  42. package/dist/esm/Primitive/unique/index.js +2 -0
  43. package/dist/esm/Types.js +2 -1
  44. package/dist/esm/Unique/Capacity.js +85 -0
  45. package/dist/esm/Unique/Distinct.js +187 -0
  46. package/dist/esm/Unique/Key.js +70 -0
  47. package/dist/esm/Unique/Override.js +23 -0
  48. package/dist/esm/Unique/Strategy.js +173 -0
  49. package/dist/esm/internal.js +1 -0
  50. package/dist/types/Bound.d.ts +66 -0
  51. package/dist/types/Distribution/index.d.ts +82 -0
  52. package/dist/types/Enumeration/Types.d.ts +10 -1
  53. package/dist/types/Error/index.d.ts +283 -0
  54. package/dist/types/Fabricator/Constructor.d.ts +6 -2
  55. package/dist/types/Fabricator/Types.d.ts +13 -0
  56. package/dist/types/Instance/Types.d.ts +14 -0
  57. package/dist/types/Primitive/array/Admits.d.ts +7 -0
  58. package/dist/types/Primitive/array/Fabricator.d.ts +2 -2
  59. package/dist/types/Primitive/array/Registry.d.ts +38 -1
  60. package/dist/types/Primitive/array/Schema.d.ts +6 -1
  61. package/dist/types/Primitive/array/Types.d.ts +33 -1
  62. package/dist/types/Primitive/bigint/Admits.d.ts +7 -0
  63. package/dist/types/Primitive/bigint/Fabricator.d.ts +3 -1
  64. package/dist/types/Primitive/bigint/Registry.d.ts +20 -0
  65. package/dist/types/Primitive/bigint/Schema.d.ts +9 -1
  66. package/dist/types/Primitive/bigint/Types.d.ts +27 -3
  67. package/dist/types/Primitive/date/Admits.d.ts +7 -0
  68. package/dist/types/Primitive/date/Defaults.d.ts +22 -1
  69. package/dist/types/Primitive/date/Fabricator.d.ts +3 -1
  70. package/dist/types/Primitive/date/Registry.d.ts +47 -11
  71. package/dist/types/Primitive/date/Schema.d.ts +10 -1
  72. package/dist/types/Primitive/date/Types.d.ts +53 -4
  73. package/dist/types/Primitive/enum/Fabricator.d.ts +3 -1
  74. package/dist/types/Primitive/enum/Registry.d.ts +20 -0
  75. package/dist/types/Primitive/enum/Types.d.ts +5 -0
  76. package/dist/types/Primitive/index.d.ts +1 -0
  77. package/dist/types/Primitive/number/Admits.d.ts +9 -0
  78. package/dist/types/Primitive/number/Fabricator.d.ts +3 -1
  79. package/dist/types/Primitive/number/Registry.d.ts +42 -1
  80. package/dist/types/Primitive/number/Schema.d.ts +10 -2
  81. package/dist/types/Primitive/number/Types.d.ts +32 -10
  82. package/dist/types/Primitive/record/Admits.d.ts +18 -0
  83. package/dist/types/Primitive/record/Fabricator.d.ts +2 -2
  84. package/dist/types/Primitive/record/Registry.d.ts +18 -1
  85. package/dist/types/Primitive/record/Schema.d.ts +8 -1
  86. package/dist/types/Primitive/record/Types.d.ts +60 -6
  87. package/dist/types/Primitive/recursive/Terminate.d.ts +7 -0
  88. package/dist/types/Primitive/string/Admits.d.ts +8 -0
  89. package/dist/types/Primitive/string/Capacity.d.ts +36 -0
  90. package/dist/types/Primitive/string/Fabricator.d.ts +3 -1
  91. package/dist/types/Primitive/string/Registry.d.ts +26 -5
  92. package/dist/types/Primitive/string/Schema.d.ts +28 -2
  93. package/dist/types/Primitive/string/Sources.d.ts +16 -0
  94. package/dist/types/Primitive/string/Types.d.ts +35 -15
  95. package/dist/types/Primitive/unique/Registry.d.ts +39 -0
  96. package/dist/types/Primitive/unique/index.d.ts +2 -0
  97. package/dist/types/Types.d.ts +9 -0
  98. package/dist/types/Unique/Capacity.d.ts +29 -0
  99. package/dist/types/Unique/Distinct.d.ts +95 -0
  100. package/dist/types/Unique/Key.d.ts +29 -0
  101. package/dist/types/Unique/Override.d.ts +16 -0
  102. package/dist/types/Unique/Strategy.d.ts +135 -0
  103. package/dist/types/index.d.ts +13 -0
  104. package/dist/types/internal.d.ts +1 -0
  105. package/package.json +2 -2
@@ -0,0 +1,18 @@
1
+ import { discreteWithin, toAdmittedEnds, toFiniteBound } from "../../Bound.js";
2
+ const label = ".admits on an array schema";
3
+ function toAdmits(meta, input) {
4
+ var _ref;
5
+ var _meta_whereby, _meta_admits;
6
+ const drawn = toEnds(null == (_meta_whereby = meta.whereby) ? void 0 : _meta_whereby.length);
7
+ return {
8
+ length: toAdmittedEnds(label, "length", null != (_ref = null == (_meta_admits = meta.admits) ? void 0 : _meta_admits.length) ? _ref : drawn, meta.produce ? void 0 : drawn, input.length, (end, given)=>toFiniteBound(label, end, given), discreteWithin)
9
+ };
10
+ }
11
+ function toEnds(length) {
12
+ if (!length) return {};
13
+ return {
14
+ min: length.min,
15
+ max: length.max
16
+ };
17
+ }
18
+ export { toAdmits };
@@ -2,6 +2,8 @@ import { effectiveDiscrete } from "../../Bound.js";
2
2
  import { discreteSampler } from "../../Distribution/index.js";
3
3
  import { toStreamFromTrace } from "../../Random/index.js";
4
4
  import { Children, Kind, Meta } from "../../Types.js";
5
+ import { assertCapacity, toDistinct } from "../../Unique/Distinct.js";
6
+ import { withinCollection } from "../../Unique/Strategy.js";
5
7
  import { Schema } from "./Schema.js";
6
8
  function Fabricator(context, element) {
7
9
  const { schema, algorithm, trace } = context;
@@ -25,9 +27,10 @@ function Fabricator(context, element) {
25
27
  const stream = toStreamFromTrace(algorithm, trace);
26
28
  const whereby = meta.whereby;
27
29
  const drawLength = discreteSampler(whereby.length.distribution, effectiveDiscrete(whereby.length.min, whereby.length.max), stream);
28
- const fabricate = ()=>Array.from({
30
+ const draw = withinCollection("array", element);
31
+ const fabricate = meta.unique ? distinctly(element, stream, meta.unique, whereby, drawLength) : ()=>Array.from({
29
32
  length: drawLength()
30
- }, ()=>element.fabricate());
33
+ }, draw);
31
34
  return {
32
35
  [Kind]: "array",
33
36
  [Meta]: meta,
@@ -37,4 +40,19 @@ function Fabricator(context, element) {
37
40
  schema: rehydrated
38
41
  };
39
42
  }
43
+ function distinctly(element, stream, unique, whereby, drawLength) {
44
+ const distinct = toDistinct(element, stream, {
45
+ label: "T.unique.array",
46
+ by: unique.by,
47
+ redraws: whereby.redraws
48
+ });
49
+ assertCapacity("T.unique.array", distinct, effectiveDiscrete(whereby.length.min, whereby.length.max).max);
50
+ return ()=>{
51
+ const count = drawLength();
52
+ const scope = distinct.open();
53
+ return Array.from({
54
+ length: count
55
+ }, ()=>scope.draw());
56
+ };
57
+ }
40
58
  export { Fabricator };
@@ -1,6 +1,7 @@
1
1
  import { toLengthRange } from "../../Bound.js";
2
2
  import { toSchema } from "../../Schema/Core.js";
3
3
  import { Kind, Meta } from "../../Types.js";
4
+ import { toRedraws } from "../../Unique/Strategy.js";
4
5
  import { Schema } from "./Schema.js";
5
6
  function Registry(definition) {
6
7
  const normalized = toSchema(definition);
@@ -23,4 +24,35 @@ function Registry(definition) {
23
24
  })
24
25
  };
25
26
  }
27
+ function unique(definition) {
28
+ const normalized = toSchema(definition);
29
+ const whereby = (by)=>(input)=>Schema({
30
+ [Kind]: "array",
31
+ [Meta]: {
32
+ whereby: toUniqueWhereby(input),
33
+ definition: normalized,
34
+ unique: by ? {
35
+ by
36
+ } : {}
37
+ }
38
+ });
39
+ return {
40
+ whereby: whereby(void 0),
41
+ by: (key)=>({
42
+ whereby: whereby(key)
43
+ })
44
+ };
45
+ }
46
+ function toUniqueWhereby(input) {
47
+ const label = "T.unique.array.whereby";
48
+ const length = toLengthRange(input.length, label);
49
+ const redraws = toRedraws(label, input.redraws);
50
+ return void 0 === redraws ? {
51
+ length
52
+ } : {
53
+ length,
54
+ redraws
55
+ };
56
+ }
26
57
  export default Registry;
58
+ export { unique };
@@ -1,6 +1,7 @@
1
1
  import { withAdaptations } from "../../Adapter/Core.js";
2
2
  import { schemaSatisfies } from "../../Schema/Satisfies.js";
3
3
  import { Kind, Meta } from "../../Types.js";
4
+ import { toAdmits } from "./Admits.js";
4
5
  function Schema(schema) {
5
6
  return {
6
7
  ...schema,
@@ -12,6 +13,13 @@ function Schema(schema) {
12
13
  produce
13
14
  }
14
15
  }),
16
+ admits: (admitted)=>Schema({
17
+ ...schema,
18
+ [Meta]: {
19
+ ...schema[Meta],
20
+ admits: toAdmits(schema[Meta], admitted)
21
+ }
22
+ }),
15
23
  satisfies: schemaSatisfies,
16
24
  adapt: (adapter, produce)=>Schema(withAdaptations(schema, adapter, produce))
17
25
  };
@@ -0,0 +1,11 @@
1
+ import { bigintWithin, toAdmittedEnds, toBigintBound } from "../../Bound.js";
2
+ const label = ".admits on a bigint schema";
3
+ function toAdmits(meta, input) {
4
+ var _meta_admits;
5
+ const drawn = meta.whereby ? {
6
+ min: meta.whereby.min,
7
+ max: meta.whereby.max
8
+ } : {};
9
+ return toAdmittedEnds(label, void 0, null != (_meta_admits = meta.admits) ? _meta_admits : drawn, meta.produce ? void 0 : drawn, input, (end, given)=>toBigintBound(label, end, given), bigintWithin);
10
+ }
11
+ export { toAdmits };
@@ -1,9 +1,12 @@
1
1
  import { effectiveDiscreteBigint } from "../../Bound.js";
2
+ import { bigintSampler } from "../../Distribution/index.js";
2
3
  import { toStreamFromTrace } from "../../Random/index.js";
3
- import { Kind, Meta } from "../../Types.js";
4
+ import { Claim, Kind, Meta } from "../../Types.js";
5
+ import { toBigintStrategy } from "../../Unique/Distinct.js";
6
+ import { toLifetimeScope } from "../../Unique/Strategy.js";
4
7
  import { Schema } from "./Schema.js";
5
8
  function Fabricator(context) {
6
- const { schema, algorithm, trace } = context;
9
+ const { schema, algorithm, trace, exhaustion } = context;
7
10
  const meta = schema[Meta];
8
11
  const rehydrated = Schema(schema);
9
12
  if (meta.produce) {
@@ -21,28 +24,18 @@ function Fabricator(context) {
21
24
  };
22
25
  }
23
26
  const stream = toStreamFromTrace(algorithm, trace);
24
- const { min, max } = effectiveDiscreteBigint(meta.whereby.min, meta.whereby.max);
25
- const range = max - min + BigInt(1);
26
- const byteBits = 8;
27
- const bitWidth = range.toString(2).length;
28
- const byteWidth = Math.ceil(bitWidth / byteBits);
29
- const bitMask = (BigInt(1) << BigInt(bitWidth)) - BigInt(1);
30
- const fabricate = ()=>{
31
- const buffer = new Uint8Array(byteWidth);
32
- const readBufferAsBigInt = ()=>{
33
- let value = BigInt(0);
34
- for (const byte of buffer)value = value << BigInt(byteBits) | BigInt(byte);
35
- return value;
36
- };
37
- const sample = ()=>{
38
- for(let i = 0; i < buffer.length; i++)buffer[i] = Math.floor(256 * stream.next());
39
- return readBufferAsBigInt() & bitMask;
27
+ if (meta.unique) {
28
+ const scope = toLifetimeScope(toBigintStrategy(meta.whereby, stream), exhaustion);
29
+ return {
30
+ [Kind]: "bigint",
31
+ [Meta]: meta,
32
+ trace,
33
+ fabricate: ()=>scope.draw(),
34
+ [Claim]: scope.claim,
35
+ schema: rehydrated
40
36
  };
41
- while(true){
42
- const candidate = sample();
43
- if (candidate < range) return min + candidate;
44
- }
45
- };
37
+ }
38
+ const fabricate = bigintSampler(effectiveDiscreteBigint(meta.whereby.min, meta.whereby.max), stream);
46
39
  return {
47
40
  [Kind]: "bigint",
48
41
  [Meta]: meta,
@@ -1,4 +1,4 @@
1
- import { assertNonemptyDiscreteBigint, toBound } from "../../Bound.js";
1
+ import { assertNonemptyDiscreteBigint, toBigintBound } from "../../Bound.js";
2
2
  import { Kind, Meta } from "../../Types.js";
3
3
  import { Schema } from "./Schema.js";
4
4
  const Registry = {
@@ -6,7 +6,7 @@ const Registry = {
6
6
  whereby: (whereby)=>Schema({
7
7
  [Kind]: "bigint",
8
8
  [Meta]: {
9
- whereby: toWhereby(whereby)
9
+ whereby: toWhereby("T.bigint.whereby", whereby)
10
10
  }
11
11
  }),
12
12
  as: (produce)=>Schema({
@@ -16,16 +16,27 @@ const Registry = {
16
16
  }
17
17
  })
18
18
  };
19
- function toWhereby(input) {
20
- const max = toBound(input.max);
19
+ const unique = {
20
+ [Kind]: "bigint",
21
+ whereby: (whereby)=>Schema({
22
+ [Kind]: "bigint",
23
+ [Meta]: {
24
+ whereby: toWhereby("T.unique.bigint.whereby", whereby),
25
+ unique: true
26
+ }
27
+ })
28
+ };
29
+ function toWhereby(label, input) {
30
+ const max = toBigintBound(label, "max", input.max);
21
31
  const min = void 0 === input.min ? {
22
32
  value: -max.value,
23
33
  exclusive: false
24
- } : toBound(input.min);
25
- assertNonemptyDiscreteBigint("T.bigint.whereby", min, max);
34
+ } : toBigintBound(label, "min", input.min);
35
+ assertNonemptyDiscreteBigint(label, min, max);
26
36
  return {
27
37
  min,
28
38
  max
29
39
  };
30
40
  }
31
41
  export default Registry;
42
+ export { unique };
@@ -1,6 +1,7 @@
1
1
  import { withAdaptations } from "../../Adapter/Core.js";
2
2
  import { schemaSatisfies } from "../../Schema/Satisfies.js";
3
3
  import { Kind, Meta } from "../../Types.js";
4
+ import { toAdmits } from "./Admits.js";
4
5
  function Schema(schema) {
5
6
  return {
6
7
  ...schema,
@@ -12,6 +13,13 @@ function Schema(schema) {
12
13
  produce
13
14
  }
14
15
  }),
16
+ admits: (admitted)=>Schema({
17
+ ...schema,
18
+ [Meta]: {
19
+ ...schema[Meta],
20
+ admits: toAdmits(schema[Meta], admitted)
21
+ }
22
+ }),
15
23
  satisfies: schemaSatisfies,
16
24
  adapt: (adapter, produce)=>Schema(withAdaptations(schema, adapter, produce))
17
25
  };
@@ -0,0 +1,22 @@
1
+ import { dateWithin, toAdmittedEnds, toDateBound } from "../../Bound.js";
2
+ const label = ".admits on a date schema";
3
+ function toAdmits(meta, input) {
4
+ var _meta_admits;
5
+ var _meta_whereby, _meta_whereby1;
6
+ const stated = {
7
+ min: null == (_meta_whereby = meta.whereby) ? void 0 : _meta_whereby.min,
8
+ max: null == (_meta_whereby1 = meta.whereby) ? void 0 : _meta_whereby1.max
9
+ };
10
+ return toAdmittedEnds(label, void 0, null != (_meta_admits = meta.admits) ? _meta_admits : stated, toFabricable(meta, stated), input, (end, given)=>toDateBound(label, end, given), dateWithin);
11
+ }
12
+ function toFabricable(meta, stated) {
13
+ if (meta.produce) return;
14
+ if ("past" === meta.mode) return {
15
+ min: stated.min
16
+ };
17
+ if ("future" === meta.mode) return {
18
+ max: stated.max
19
+ };
20
+ return stated;
21
+ }
22
+ export { toAdmits };
@@ -1,3 +1,4 @@
1
+ import { effectiveDiscrete, epochBound } from "../../Bound.js";
1
2
  import { MAX_TIME } from "../../Types.js";
2
3
  const unboundedEpoch = {
3
4
  min: {
@@ -9,4 +10,29 @@ const unboundedEpoch = {
9
10
  exclusive: false
10
11
  }
11
12
  };
12
- export { unboundedEpoch };
13
+ function toEpochBounds(meta, clock) {
14
+ const whereby = meta.whereby;
15
+ const min = (null == whereby ? void 0 : whereby.min) ? epochBound(whereby.min) : unboundedEpoch.min;
16
+ const max = (null == whereby ? void 0 : whereby.max) ? epochBound(whereby.max) : unboundedEpoch.max;
17
+ const now = {
18
+ value: clock,
19
+ exclusive: false
20
+ };
21
+ if ("past" === meta.mode) return {
22
+ min,
23
+ max: now
24
+ };
25
+ if ("future" === meta.mode) return {
26
+ min: now,
27
+ max
28
+ };
29
+ return {
30
+ min,
31
+ max
32
+ };
33
+ }
34
+ function toEpochRange(meta, clock) {
35
+ const { min, max } = toEpochBounds(meta, clock);
36
+ return effectiveDiscrete(min, max);
37
+ }
38
+ export { toEpochBounds, toEpochRange, unboundedEpoch };
@@ -1,11 +1,13 @@
1
- import { assertNonemptyDiscrete, effectiveDiscrete, epochBound } from "../../Bound.js";
1
+ import { assertNonemptyDiscrete, effectiveDiscrete } from "../../Bound.js";
2
2
  import { Distribution, sampler } from "../../Distribution/index.js";
3
3
  import { toStreamFromTrace } from "../../Random/index.js";
4
- import { Kind, MAX_TIME, Meta } from "../../Types.js";
5
- import { unboundedEpoch } from "./Defaults.js";
4
+ import { Claim, Kind, MAX_TIME, Meta } from "../../Types.js";
5
+ import { toDateStrategy } from "../../Unique/Distinct.js";
6
+ import { toLifetimeScope } from "../../Unique/Strategy.js";
7
+ import { toEpochBounds, toEpochRange } from "./Defaults.js";
6
8
  import { Schema } from "./Schema.js";
7
9
  function Fabricator(context) {
8
- const { schema, algorithm, trace } = context;
10
+ const { schema, algorithm, trace, exhaustion } = context;
9
11
  const { clock } = trace;
10
12
  const meta = schema[Meta];
11
13
  const rehydrated = Schema(schema);
@@ -24,43 +26,25 @@ function Fabricator(context) {
24
26
  };
25
27
  }
26
28
  const stream = toStreamFromTrace(algorithm, trace);
27
- const whereby = meta.whereby;
28
- if ("past" === meta.mode) {
29
- const fabricate = whereby ? drawPastOrFuture({
30
- min: whereby.min ? epochBound(whereby.min) : unboundedEpoch.min,
31
- max: {
32
- value: clock,
33
- exclusive: false
34
- }
35
- }, "T.date.past", whereby.distribution, stream) : ()=>new Date(clock - stream.next() * (clock + MAX_TIME));
36
- return {
37
- [Kind]: "date",
38
- [Meta]: meta,
39
- trace,
40
- fabricate,
41
- schema: rehydrated
42
- };
43
- }
44
- if ("future" === meta.mode) {
45
- const fabricate = whereby ? drawPastOrFuture({
46
- min: {
47
- value: clock,
48
- exclusive: false
49
- },
50
- max: whereby.max ? epochBound(whereby.max) : unboundedEpoch.max
51
- }, "T.date.future", whereby.distribution, stream) : ()=>new Date(clock + stream.next() * (MAX_TIME - clock));
29
+ if (meta.unique) {
30
+ var _meta_whereby, _meta_whereby1;
31
+ const scope = toLifetimeScope(toDateStrategy({
32
+ label: "T.unique.date",
33
+ range: toEpochRange(meta, clock),
34
+ distribution: null == (_meta_whereby = meta.whereby) ? void 0 : _meta_whereby.distribution,
35
+ stream,
36
+ redraws: null == (_meta_whereby1 = meta.whereby) ? void 0 : _meta_whereby1.redraws
37
+ }), exhaustion);
52
38
  return {
53
39
  [Kind]: "date",
54
40
  [Meta]: meta,
55
41
  trace,
56
- fabricate,
42
+ fabricate: ()=>scope.draw(),
43
+ [Claim]: scope.claim,
57
44
  schema: rehydrated
58
45
  };
59
46
  }
60
- const fabricate = whereby ? draw({
61
- min: whereby.min ? epochBound(whereby.min) : unboundedEpoch.min,
62
- max: whereby.max ? epochBound(whereby.max) : unboundedEpoch.max
63
- }, whereby.distribution, stream) : ()=>new Date((2 * stream.next() - 1) * MAX_TIME);
47
+ const fabricate = toDraw(meta, clock, stream);
64
48
  return {
65
49
  [Kind]: "date",
66
50
  [Meta]: meta,
@@ -69,6 +53,20 @@ function Fabricator(context) {
69
53
  schema: rehydrated
70
54
  };
71
55
  }
56
+ function toDraw(meta, clock, stream) {
57
+ const whereby = meta.whereby;
58
+ const prefix = meta.unique ? "T.unique.date" : "T.date";
59
+ if ("past" === meta.mode) {
60
+ if (!whereby) return ()=>new Date(clock - stream.next() * (clock + MAX_TIME));
61
+ return drawPastOrFuture(toEpochBounds(meta, clock), `${prefix}.past`, whereby.distribution, stream);
62
+ }
63
+ if ("future" === meta.mode) {
64
+ if (!whereby) return ()=>new Date(clock + stream.next() * (MAX_TIME - clock));
65
+ return drawPastOrFuture(toEpochBounds(meta, clock), `${prefix}.future`, whereby.distribution, stream);
66
+ }
67
+ if (!whereby) return ()=>new Date((2 * stream.next() - 1) * MAX_TIME);
68
+ return draw(toEpochBounds(meta, clock), whereby.distribution, stream);
69
+ }
72
70
  function drawPastOrFuture(range, label, distribution, stream) {
73
71
  assertNonemptyDiscrete(label, range.min, range.max);
74
72
  return draw(range, distribution, stream);
@@ -1,5 +1,6 @@
1
- import { assertNonemptyDiscrete, epochBound, toBound } from "../../Bound.js";
1
+ import { assertNonemptyDiscrete, epochBound, toDateBound } from "../../Bound.js";
2
2
  import { Adaptation, Kind, Meta, Produces } from "../../Types.js";
3
+ import { toRedraws } from "../../Unique/Strategy.js";
3
4
  import { unboundedEpoch } from "./Defaults.js";
4
5
  import { Schema } from "./Schema.js";
5
6
  function registry() {
@@ -10,16 +11,12 @@ function registry() {
10
11
  [Produces]: void 0,
11
12
  [Adaptation]: void 0
12
13
  }),
13
- whereby: (whereby)=>{
14
- const converted = toWhereby(whereby);
15
- assertNonemptyDiscrete("T.date.whereby", converted.min ? epochBound(converted.min) : unboundedEpoch.min, converted.max ? epochBound(converted.max) : unboundedEpoch.max);
16
- return Schema({
14
+ whereby: (whereby)=>Schema({
17
15
  [Kind]: "date",
18
16
  [Meta]: {
19
- whereby: converted
17
+ whereby: toCheckedWhereby("T.date.whereby", whereby)
20
18
  }
21
- });
22
- },
19
+ }),
23
20
  get past () {
24
21
  return {
25
22
  ...Schema({
@@ -32,7 +29,7 @@ function registry() {
32
29
  [Kind]: "date",
33
30
  [Meta]: {
34
31
  mode: "past",
35
- whereby: toWhereby(whereby)
32
+ whereby: toWhereby("T.date.past.whereby", whereby)
36
33
  }
37
34
  })
38
35
  };
@@ -49,7 +46,7 @@ function registry() {
49
46
  [Kind]: "date",
50
47
  [Meta]: {
51
48
  mode: "future",
52
- whereby: toWhereby(whereby)
49
+ whereby: toWhereby("T.date.future.whereby", whereby)
53
50
  }
54
51
  })
55
52
  };
@@ -57,9 +54,78 @@ function registry() {
57
54
  };
58
55
  }
59
56
  const Registry = registry;
60
- function toWhereby(input) {
61
- const min = void 0 === input.min ? void 0 : toBound(input.min);
62
- const max = void 0 === input.max ? void 0 : toBound(input.max);
57
+ function unique() {
58
+ return {
59
+ ...Schema({
60
+ [Kind]: "date",
61
+ [Meta]: {
62
+ unique: true
63
+ },
64
+ [Produces]: void 0,
65
+ [Adaptation]: void 0
66
+ }),
67
+ whereby: (whereby)=>Schema({
68
+ [Kind]: "date",
69
+ [Meta]: {
70
+ unique: true,
71
+ whereby: withRedraws("T.unique.date.whereby", toCheckedWhereby("T.unique.date.whereby", whereby), whereby)
72
+ }
73
+ }),
74
+ get past () {
75
+ return {
76
+ ...Schema({
77
+ [Kind]: "date",
78
+ [Meta]: {
79
+ mode: "past",
80
+ unique: true
81
+ }
82
+ }),
83
+ whereby: (whereby)=>Schema({
84
+ [Kind]: "date",
85
+ [Meta]: {
86
+ mode: "past",
87
+ unique: true,
88
+ whereby: withRedraws("T.unique.date.past.whereby", toWhereby("T.unique.date.past.whereby", whereby), whereby)
89
+ }
90
+ })
91
+ };
92
+ },
93
+ get future () {
94
+ return {
95
+ ...Schema({
96
+ [Kind]: "date",
97
+ [Meta]: {
98
+ mode: "future",
99
+ unique: true
100
+ }
101
+ }),
102
+ whereby: (whereby)=>Schema({
103
+ [Kind]: "date",
104
+ [Meta]: {
105
+ mode: "future",
106
+ unique: true,
107
+ whereby: withRedraws("T.unique.date.future.whereby", toWhereby("T.unique.date.future.whereby", whereby), whereby)
108
+ }
109
+ })
110
+ };
111
+ }
112
+ };
113
+ }
114
+ function toCheckedWhereby(label, input) {
115
+ const converted = toWhereby(label, input);
116
+ assertNonemptyDiscrete(label, converted.min ? epochBound(converted.min) : unboundedEpoch.min, converted.max ? epochBound(converted.max) : unboundedEpoch.max);
117
+ return converted;
118
+ }
119
+ function withRedraws(label, whereby, input) {
120
+ const redraws = toRedraws(label, input.redraws);
121
+ return void 0 === redraws ? whereby : {
122
+ ...whereby,
123
+ redraws
124
+ };
125
+ }
126
+ function toWhereby(label, input) {
127
+ const min = void 0 === input.min ? void 0 : toDateBound(label, "min", input.min);
128
+ const max = void 0 === input.max ? void 0 : toDateBound(label, "max", input.max);
63
129
  return void 0 === input.distribution ? {
64
130
  min,
65
131
  max
@@ -70,3 +136,4 @@ function toWhereby(input) {
70
136
  };
71
137
  }
72
138
  export default Registry;
139
+ export { unique };
@@ -1,6 +1,7 @@
1
1
  import { withAdaptations } from "../../Adapter/Core.js";
2
2
  import { schemaSatisfies } from "../../Schema/Satisfies.js";
3
3
  import { Kind, Meta } from "../../Types.js";
4
+ import { toAdmits } from "./Admits.js";
4
5
  function Schema(schema) {
5
6
  return {
6
7
  ...schema,
@@ -12,6 +13,13 @@ function Schema(schema) {
12
13
  produce
13
14
  }
14
15
  }),
16
+ admits: (admitted)=>Schema({
17
+ ...schema,
18
+ [Meta]: {
19
+ ...schema[Meta],
20
+ admits: toAdmits(schema[Meta], admitted)
21
+ }
22
+ }),
15
23
  satisfies: schemaSatisfies,
16
24
  adapt: (adapter, produce)=>Schema(withAdaptations(schema, adapter, produce))
17
25
  };
@@ -1,9 +1,10 @@
1
1
  import { weighted } from "../../Distribution/index.js";
2
2
  import { toStreamFromTrace } from "../../Random/index.js";
3
- import { Kind, Meta } from "../../Types.js";
3
+ import { Claim, Kind, Meta } from "../../Types.js";
4
+ import { exactValues, toLifetimeScope } from "../../Unique/Strategy.js";
4
5
  import { Schema } from "./Schema.js";
5
6
  function Fabricator(context) {
6
- const { schema, algorithm, trace } = context;
7
+ const { schema, algorithm, trace, exhaustion } = context;
7
8
  const meta = schema[Meta];
8
9
  const rehydrated = Schema(schema);
9
10
  if (meta.produce) {
@@ -21,6 +22,17 @@ function Fabricator(context) {
21
22
  };
22
23
  }
23
24
  const stream = toStreamFromTrace(algorithm, trace);
25
+ if (meta.unique) {
26
+ const scope = toLifetimeScope(exactValues("enum", meta.items, stream), exhaustion);
27
+ return {
28
+ [Kind]: "enum",
29
+ [Meta]: meta,
30
+ trace,
31
+ fabricate: ()=>scope.draw(),
32
+ [Claim]: scope.claim,
33
+ schema: rehydrated
34
+ };
35
+ }
24
36
  const pick = weighted(meta.items, stream, "T.enum");
25
37
  return {
26
38
  [Kind]: "enum",
@@ -2,32 +2,45 @@ import { assertDrawableWeights } from "../../Distribution/index.js";
2
2
  import { FabricatorError } from "../../Error/index.js";
3
3
  import { Kind, Meta } from "../../Types.js";
4
4
  import { Schema } from "./Schema.js";
5
- function assertNonEmpty(items) {
6
- if (0 === items.length) throw new FabricatorError.EmptyItemsError("T.enum", "member");
5
+ function assertNonEmpty(label, items) {
6
+ if (0 === items.length) throw new FabricatorError.EmptyItemsError(label, "member");
7
+ }
8
+ function uniform(label, flag, items) {
9
+ assertNonEmpty(label, items);
10
+ const weighted = items.map((x)=>[
11
+ 1,
12
+ x
13
+ ]);
14
+ return Schema({
15
+ [Kind]: "enum",
16
+ [Meta]: {
17
+ items: weighted,
18
+ ...flag
19
+ }
20
+ });
21
+ }
22
+ function Registry_weighted(label, flag, items) {
23
+ assertNonEmpty(label, items);
24
+ assertDrawableWeights(`${label}.weighted`, "member", items);
25
+ return Schema({
26
+ [Kind]: "enum",
27
+ [Meta]: {
28
+ items,
29
+ ...flag
30
+ }
31
+ });
7
32
  }
8
33
  const Registry = {
9
- uniform: (items)=>{
10
- assertNonEmpty(items);
11
- const weighted = items.map((x)=>[
12
- 1,
13
- x
14
- ]);
15
- return Schema({
16
- [Kind]: "enum",
17
- [Meta]: {
18
- items: weighted
19
- }
20
- });
21
- },
22
- weighted: (items)=>{
23
- assertNonEmpty(items);
24
- assertDrawableWeights("T.enum.weighted", "member", items);
25
- return Schema({
26
- [Kind]: "enum",
27
- [Meta]: {
28
- items
29
- }
30
- });
31
- }
34
+ uniform: (items)=>uniform("T.enum", {}, items),
35
+ weighted: (items)=>Registry_weighted("T.enum", {}, items)
36
+ };
37
+ const unique = {
38
+ uniform: (items)=>uniform("T.unique.enum", {
39
+ unique: true
40
+ }, items),
41
+ weighted: (items)=>Registry_weighted("T.unique.enum", {
42
+ unique: true
43
+ }, items)
32
44
  };
33
45
  export default Registry;
46
+ export { unique };