@ghostry/fabricator 0.0.13 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/Bound.js +74 -1
- package/dist/esm/Distribution/index.js +103 -2
- package/dist/esm/Enumeration/Enumerate.js +10 -5
- package/dist/esm/Enumeration/Plan.js +2 -2
- package/dist/esm/Error/index.js +96 -27
- package/dist/esm/Fabricator/Constructor.js +7 -5
- package/dist/esm/Instance/Core.js +3 -0
- package/dist/esm/Primitive/array/Admits.js +18 -0
- package/dist/esm/Primitive/array/Fabricator.js +20 -2
- package/dist/esm/Primitive/array/Registry.js +32 -0
- package/dist/esm/Primitive/array/Schema.js +8 -0
- package/dist/esm/Primitive/bigint/Admits.js +11 -0
- package/dist/esm/Primitive/bigint/Fabricator.js +16 -23
- package/dist/esm/Primitive/bigint/Registry.js +17 -6
- package/dist/esm/Primitive/bigint/Schema.js +8 -0
- package/dist/esm/Primitive/date/Admits.js +22 -0
- package/dist/esm/Primitive/date/Defaults.js +27 -1
- package/dist/esm/Primitive/date/Fabricator.js +32 -34
- package/dist/esm/Primitive/date/Registry.js +80 -13
- package/dist/esm/Primitive/date/Schema.js +8 -0
- package/dist/esm/Primitive/enum/Fabricator.js +14 -2
- package/dist/esm/Primitive/enum/Registry.js +38 -25
- package/dist/esm/Primitive/index.js +1 -0
- package/dist/esm/Primitive/number/Admits.js +27 -0
- package/dist/esm/Primitive/number/Fabricator.js +18 -9
- package/dist/esm/Primitive/number/Registry.js +55 -4
- package/dist/esm/Primitive/number/Schema.js +10 -3
- package/dist/esm/Primitive/object/Fabricator.js +2 -0
- package/dist/esm/Primitive/record/Admits.js +42 -0
- package/dist/esm/Primitive/record/Fabricator.js +49 -19
- package/dist/esm/Primitive/record/Registry.js +29 -0
- package/dist/esm/Primitive/record/Schema.js +8 -0
- package/dist/esm/Primitive/recursive/Fabricator.js +3 -2
- package/dist/esm/Primitive/recursive/Terminate.js +16 -2
- package/dist/esm/Primitive/string/Admits.js +26 -0
- package/dist/esm/Primitive/string/Capacity.js +81 -0
- package/dist/esm/Primitive/string/Fabricator.js +23 -43
- package/dist/esm/Primitive/string/Registry.js +31 -9
- package/dist/esm/Primitive/string/Schema.js +10 -3
- package/dist/esm/Primitive/string/Sources.js +41 -0
- package/dist/esm/Primitive/unique/Registry.js +22 -0
- package/dist/esm/Primitive/unique/index.js +2 -0
- package/dist/esm/Types.js +2 -1
- package/dist/esm/Unique/Capacity.js +85 -0
- package/dist/esm/Unique/Distinct.js +187 -0
- package/dist/esm/Unique/Key.js +70 -0
- package/dist/esm/Unique/Override.js +23 -0
- package/dist/esm/Unique/Strategy.js +173 -0
- package/dist/esm/internal.js +1 -0
- package/dist/types/Bound.d.ts +66 -0
- package/dist/types/Distribution/index.d.ts +82 -0
- package/dist/types/Enumeration/Types.d.ts +10 -1
- package/dist/types/Error/index.d.ts +283 -0
- package/dist/types/Fabricator/Constructor.d.ts +6 -2
- package/dist/types/Fabricator/Types.d.ts +13 -0
- package/dist/types/Instance/Types.d.ts +14 -0
- package/dist/types/Primitive/array/Admits.d.ts +7 -0
- package/dist/types/Primitive/array/Fabricator.d.ts +2 -2
- package/dist/types/Primitive/array/Registry.d.ts +38 -1
- package/dist/types/Primitive/array/Schema.d.ts +6 -1
- package/dist/types/Primitive/array/Types.d.ts +33 -1
- package/dist/types/Primitive/bigint/Admits.d.ts +7 -0
- package/dist/types/Primitive/bigint/Fabricator.d.ts +3 -1
- package/dist/types/Primitive/bigint/Registry.d.ts +20 -0
- package/dist/types/Primitive/bigint/Schema.d.ts +9 -1
- package/dist/types/Primitive/bigint/Types.d.ts +27 -3
- package/dist/types/Primitive/date/Admits.d.ts +7 -0
- package/dist/types/Primitive/date/Defaults.d.ts +22 -1
- package/dist/types/Primitive/date/Fabricator.d.ts +3 -1
- package/dist/types/Primitive/date/Registry.d.ts +47 -11
- package/dist/types/Primitive/date/Schema.d.ts +10 -1
- package/dist/types/Primitive/date/Types.d.ts +53 -4
- package/dist/types/Primitive/enum/Fabricator.d.ts +3 -1
- package/dist/types/Primitive/enum/Registry.d.ts +20 -0
- package/dist/types/Primitive/enum/Types.d.ts +5 -0
- package/dist/types/Primitive/index.d.ts +1 -0
- package/dist/types/Primitive/number/Admits.d.ts +9 -0
- package/dist/types/Primitive/number/Fabricator.d.ts +3 -1
- package/dist/types/Primitive/number/Registry.d.ts +42 -1
- package/dist/types/Primitive/number/Schema.d.ts +10 -2
- package/dist/types/Primitive/number/Types.d.ts +32 -10
- package/dist/types/Primitive/record/Admits.d.ts +18 -0
- package/dist/types/Primitive/record/Fabricator.d.ts +2 -2
- package/dist/types/Primitive/record/Registry.d.ts +18 -1
- package/dist/types/Primitive/record/Schema.d.ts +8 -1
- package/dist/types/Primitive/record/Types.d.ts +60 -6
- package/dist/types/Primitive/recursive/Terminate.d.ts +7 -0
- package/dist/types/Primitive/string/Admits.d.ts +8 -0
- package/dist/types/Primitive/string/Capacity.d.ts +36 -0
- package/dist/types/Primitive/string/Fabricator.d.ts +3 -1
- package/dist/types/Primitive/string/Registry.d.ts +26 -5
- package/dist/types/Primitive/string/Schema.d.ts +28 -2
- package/dist/types/Primitive/string/Sources.d.ts +16 -0
- package/dist/types/Primitive/string/Types.d.ts +35 -15
- package/dist/types/Primitive/unique/Registry.d.ts +39 -0
- package/dist/types/Primitive/unique/index.d.ts +2 -0
- package/dist/types/Types.d.ts +9 -0
- package/dist/types/Unique/Capacity.d.ts +29 -0
- package/dist/types/Unique/Distinct.d.ts +95 -0
- package/dist/types/Unique/Key.d.ts +29 -0
- package/dist/types/Unique/Override.d.ts +16 -0
- package/dist/types/Unique/Strategy.d.ts +135 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/types/internal.d.ts +1 -0
- package/package.json +2 -2
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { effectiveDiscrete } from "../../Bound.js";
|
|
2
2
|
import { discreteSampler, weighted } from "../../Distribution/index.js";
|
|
3
3
|
import { toStreamFromTrace } from "../../Random/index.js";
|
|
4
|
-
import { Kind, Meta } from "../../Types.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { Claim, Kind, Meta } from "../../Types.js";
|
|
5
|
+
import { toLifetimeScope, trackedByKey } from "../../Unique/Strategy.js";
|
|
6
|
+
import { toStringDomain } from "./Capacity.js";
|
|
7
|
+
import { bmpScalars, toRanges, toSources } from "./Sources.js";
|
|
7
8
|
import { Schema } from "./Schema.js";
|
|
8
9
|
function Fabricator(context) {
|
|
9
|
-
const { schema, algorithm, trace } = context;
|
|
10
|
+
const { schema, algorithm, trace, exhaustion } = context;
|
|
10
11
|
const meta = schema[Meta];
|
|
11
12
|
const rehydrated = Schema(schema);
|
|
12
13
|
if (meta.produce) {
|
|
@@ -30,20 +31,7 @@ function Fabricator(context) {
|
|
|
30
31
|
min,
|
|
31
32
|
max
|
|
32
33
|
}, stream);
|
|
33
|
-
const sources =
|
|
34
|
-
const composition = whereby.composition;
|
|
35
|
-
if (void 0 === composition) return [
|
|
36
|
-
[
|
|
37
|
-
1,
|
|
38
|
-
unicode.scalars
|
|
39
|
-
]
|
|
40
|
-
];
|
|
41
|
-
if (Array.isArray(composition)) return composition;
|
|
42
|
-
return Object.entries(composition).map(([name, weight])=>[
|
|
43
|
-
weight,
|
|
44
|
-
classes[name]
|
|
45
|
-
]);
|
|
46
|
-
});
|
|
34
|
+
const sources = toSources(whereby.composition);
|
|
47
35
|
const pick = weighted(sources.map(([weight, source])=>[
|
|
48
36
|
weight,
|
|
49
37
|
compiler(source, stream)
|
|
@@ -66,6 +54,23 @@ function Fabricator(context) {
|
|
|
66
54
|
}
|
|
67
55
|
return pieces.join("");
|
|
68
56
|
};
|
|
57
|
+
if (meta.unique) {
|
|
58
|
+
const scope = toLifetimeScope(trackedByKey({
|
|
59
|
+
label: "T.unique.string",
|
|
60
|
+
kind: "string",
|
|
61
|
+
draw: fabricate,
|
|
62
|
+
redraws: whereby.redraws,
|
|
63
|
+
...toStringDomain(whereby)
|
|
64
|
+
}), exhaustion);
|
|
65
|
+
return {
|
|
66
|
+
[Kind]: "string",
|
|
67
|
+
[Meta]: meta,
|
|
68
|
+
trace,
|
|
69
|
+
fabricate: ()=>scope.draw(),
|
|
70
|
+
[Claim]: scope.claim,
|
|
71
|
+
schema: rehydrated
|
|
72
|
+
};
|
|
73
|
+
}
|
|
69
74
|
return {
|
|
70
75
|
[Kind]: "string",
|
|
71
76
|
[Meta]: meta,
|
|
@@ -74,31 +79,6 @@ function Fabricator(context) {
|
|
|
74
79
|
schema: rehydrated
|
|
75
80
|
};
|
|
76
81
|
}
|
|
77
|
-
const toRanges = (source)=>{
|
|
78
|
-
if ("string" == typeof source) return [
|
|
79
|
-
...source
|
|
80
|
-
].map((char)=>{
|
|
81
|
-
const codepoint = char.codePointAt(0);
|
|
82
|
-
return {
|
|
83
|
-
from: codepoint,
|
|
84
|
-
to: codepoint
|
|
85
|
-
};
|
|
86
|
-
});
|
|
87
|
-
if (Array.isArray(source)) return source;
|
|
88
|
-
return [
|
|
89
|
-
source
|
|
90
|
-
];
|
|
91
|
-
};
|
|
92
|
-
const bmpScalars = [
|
|
93
|
-
{
|
|
94
|
-
from: 0x0,
|
|
95
|
-
to: 0xd7ff
|
|
96
|
-
},
|
|
97
|
-
{
|
|
98
|
-
from: 0xe000,
|
|
99
|
-
to: 0xffff
|
|
100
|
-
}
|
|
101
|
-
];
|
|
102
82
|
const compiler = (source, stream)=>{
|
|
103
83
|
const pick = weighted(toRanges(source).map((range)=>[
|
|
104
84
|
range.to - range.from + 1,
|
|
@@ -1,25 +1,47 @@
|
|
|
1
1
|
import { toLengthRange } from "../../Bound.js";
|
|
2
2
|
import { Kind, Meta } from "../../Types.js";
|
|
3
|
+
import { toRedraws } from "../../Unique/Strategy.js";
|
|
3
4
|
import { Schema } from "./Schema.js";
|
|
4
5
|
const Registry = {
|
|
5
6
|
[Kind]: "string",
|
|
6
|
-
as: (produce
|
|
7
|
+
as: (produce)=>Schema({
|
|
7
8
|
[Kind]: "string",
|
|
8
9
|
[Meta]: {
|
|
9
|
-
produce
|
|
10
|
-
hints
|
|
10
|
+
produce
|
|
11
11
|
}
|
|
12
12
|
}),
|
|
13
13
|
whereby: (whereby)=>Schema({
|
|
14
14
|
[Kind]: "string",
|
|
15
15
|
[Meta]: {
|
|
16
|
-
whereby:
|
|
17
|
-
length: toLengthRange(whereby.length, "T.string.whereby"),
|
|
18
|
-
...void 0 === whereby.composition ? {} : {
|
|
19
|
-
composition: whereby.composition
|
|
20
|
-
}
|
|
21
|
-
}
|
|
16
|
+
whereby: toWhereby("T.string.whereby", whereby)
|
|
22
17
|
}
|
|
23
18
|
})
|
|
24
19
|
};
|
|
20
|
+
const unique = {
|
|
21
|
+
[Kind]: "string",
|
|
22
|
+
whereby: (whereby)=>{
|
|
23
|
+
const label = "T.unique.string.whereby";
|
|
24
|
+
const converted = toWhereby(label, whereby);
|
|
25
|
+
const redraws = toRedraws(label, whereby.redraws);
|
|
26
|
+
return Schema({
|
|
27
|
+
[Kind]: "string",
|
|
28
|
+
[Meta]: {
|
|
29
|
+
whereby: void 0 === redraws ? converted : {
|
|
30
|
+
...converted,
|
|
31
|
+
redraws
|
|
32
|
+
},
|
|
33
|
+
unique: true
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
function toWhereby(label, input) {
|
|
39
|
+
return {
|
|
40
|
+
length: toLengthRange(input.length, label),
|
|
41
|
+
...void 0 === input.composition ? {} : {
|
|
42
|
+
composition: input.composition
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
25
46
|
export default Registry;
|
|
47
|
+
export { unique };
|
|
@@ -1,16 +1,23 @@
|
|
|
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,
|
|
7
8
|
[Kind]: "string",
|
|
8
|
-
as: (produce
|
|
9
|
+
as: (produce)=>Schema({
|
|
9
10
|
...schema,
|
|
10
11
|
[Meta]: {
|
|
11
12
|
...schema[Meta],
|
|
12
|
-
produce
|
|
13
|
-
|
|
13
|
+
produce
|
|
14
|
+
}
|
|
15
|
+
}),
|
|
16
|
+
admits: (admitted)=>Schema({
|
|
17
|
+
...schema,
|
|
18
|
+
[Meta]: {
|
|
19
|
+
...schema[Meta],
|
|
20
|
+
admits: toAdmits(schema[Meta], admitted)
|
|
14
21
|
}
|
|
15
22
|
}),
|
|
16
23
|
satisfies: schemaSatisfies,
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { isArray } from "../../Utility/Core.js";
|
|
2
|
+
import { classes, unicode } from "./Constants.js";
|
|
3
|
+
function toSources(composition) {
|
|
4
|
+
if (void 0 === composition) return [
|
|
5
|
+
[
|
|
6
|
+
1,
|
|
7
|
+
unicode.scalars
|
|
8
|
+
]
|
|
9
|
+
];
|
|
10
|
+
if (Array.isArray(composition)) return composition;
|
|
11
|
+
return Object.entries(composition).map(([name, weight])=>[
|
|
12
|
+
weight,
|
|
13
|
+
classes[name]
|
|
14
|
+
]);
|
|
15
|
+
}
|
|
16
|
+
function toRanges(source) {
|
|
17
|
+
if ("string" == typeof source) return [
|
|
18
|
+
...source
|
|
19
|
+
].map((char)=>{
|
|
20
|
+
const codepoint = char.codePointAt(0);
|
|
21
|
+
return {
|
|
22
|
+
from: codepoint,
|
|
23
|
+
to: codepoint
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
if (isArray(source)) return source;
|
|
27
|
+
return [
|
|
28
|
+
source
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
const bmpScalars = [
|
|
32
|
+
{
|
|
33
|
+
from: 0x0,
|
|
34
|
+
to: 0xd7ff
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
from: 0xe000,
|
|
38
|
+
to: 0xffff
|
|
39
|
+
}
|
|
40
|
+
];
|
|
41
|
+
export { bmpScalars, toRanges, toSources };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { unique } from "../array/Registry.js";
|
|
2
|
+
import { unique as Registry_js_unique } from "../bigint/Registry.js";
|
|
3
|
+
import { unique as external_date_Registry_js_unique } from "../date/Registry.js";
|
|
4
|
+
import { unique as external_enum_Registry_js_unique } from "../enum/Registry.js";
|
|
5
|
+
import { unique as external_number_Registry_js_unique } from "../number/Registry.js";
|
|
6
|
+
import { unique as external_record_Registry_js_unique } from "../record/Registry.js";
|
|
7
|
+
import { unique as external_string_Registry_js_unique } from "../string/Registry.js";
|
|
8
|
+
const registry = {
|
|
9
|
+
enum: external_enum_Registry_js_unique,
|
|
10
|
+
get number () {
|
|
11
|
+
return external_number_Registry_js_unique();
|
|
12
|
+
},
|
|
13
|
+
bigint: Registry_js_unique,
|
|
14
|
+
string: external_string_Registry_js_unique,
|
|
15
|
+
get date () {
|
|
16
|
+
return external_date_Registry_js_unique();
|
|
17
|
+
},
|
|
18
|
+
array: unique,
|
|
19
|
+
record: external_record_Registry_js_unique
|
|
20
|
+
};
|
|
21
|
+
const Registry = registry;
|
|
22
|
+
export default Registry;
|
package/dist/esm/Types.js
CHANGED
|
@@ -7,5 +7,6 @@ const Layer = Symbol.for("fabricator:layer");
|
|
|
7
7
|
const Children = Symbol.for("fabricator:children");
|
|
8
8
|
const Adaptation = Symbol.for("fabricator:adaptation");
|
|
9
9
|
const Omitted = Symbol.for("fabricator:omitted");
|
|
10
|
+
const Claim = Symbol.for("fabricator:claim");
|
|
10
11
|
const MAX_TIME = 8640000000000000;
|
|
11
|
-
export { Adaptation, Children, Fixed, Kind, Layer, MAX_TIME, Meta, Omitted, Produces, Replace };
|
|
12
|
+
export { Adaptation, Children, Claim, Fixed, Kind, Layer, MAX_TIME, Meta, Omitted, Produces, Replace };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { effectiveDiscrete, effectiveDiscreteBigint } from "../Bound.js";
|
|
2
|
+
import { isDrawable } from "../Distribution/index.js";
|
|
3
|
+
import { toEpochRange } from "../Primitive/date/Defaults.js";
|
|
4
|
+
import { toStringDomain } from "../Primitive/string/Capacity.js";
|
|
5
|
+
import { toSchema } from "../Schema/Core.js";
|
|
6
|
+
import { Fixed, Kind, Meta } from "../Types.js";
|
|
7
|
+
import { toNumberCapacity, toRangeCapacity } from "./Distinct.js";
|
|
8
|
+
import { toDistinctValues } from "./Strategy.js";
|
|
9
|
+
function toCapacity(schema, clock) {
|
|
10
|
+
const limits = [];
|
|
11
|
+
walk(toSchema(schema), BigInt(1), clock, limits);
|
|
12
|
+
return limits.reduce((tightest, limit)=>void 0 === tightest || limit < tightest ? limit : tightest, void 0);
|
|
13
|
+
}
|
|
14
|
+
function walk(node, draws, clock, limits) {
|
|
15
|
+
const meta = null == node ? void 0 : node[Meta];
|
|
16
|
+
if (!meta || meta.produce || draws === BigInt(0)) return;
|
|
17
|
+
if (true === meta.unique && "record" !== node[Kind]) {
|
|
18
|
+
const capacity = toLeafCapacity(node[Kind], meta, clock);
|
|
19
|
+
if (void 0 !== capacity) limits.push(capacity / draws);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const visit = (child, times = draws)=>walk(child, times, clock, limits);
|
|
23
|
+
switch(node[Kind]){
|
|
24
|
+
case "object":
|
|
25
|
+
{
|
|
26
|
+
const fields = {
|
|
27
|
+
...meta.definition,
|
|
28
|
+
...meta.hidden
|
|
29
|
+
};
|
|
30
|
+
for (const field of Object.values(fields))if (!(Fixed in field)) visit(field);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
case "tuple":
|
|
34
|
+
meta.items.forEach((item)=>visit(item));
|
|
35
|
+
return;
|
|
36
|
+
case "derive":
|
|
37
|
+
meta.from.forEach((item)=>visit(item));
|
|
38
|
+
return;
|
|
39
|
+
case "choice":
|
|
40
|
+
for (const [weight, option] of meta.items)if (isDrawable(weight)) visit(option);
|
|
41
|
+
return;
|
|
42
|
+
case "nullable":
|
|
43
|
+
case "nullish":
|
|
44
|
+
case "undefinable":
|
|
45
|
+
case "object.omittable":
|
|
46
|
+
case "object.optional":
|
|
47
|
+
visit(meta.definition);
|
|
48
|
+
return;
|
|
49
|
+
case "array":
|
|
50
|
+
{
|
|
51
|
+
const { max } = effectiveDiscrete(meta.whereby.length.min, meta.whereby.length.max);
|
|
52
|
+
visit(meta.definition, draws * BigInt(max));
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
case "record":
|
|
56
|
+
{
|
|
57
|
+
const max = meta.unique ? effectiveDiscrete(meta.whereby.size.min, meta.whereby.size.max).max : meta.whereby.size.max;
|
|
58
|
+
visit(meta.key, draws * BigInt(max));
|
|
59
|
+
visit(meta.value, draws * BigInt(max));
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
default:
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function toLeafCapacity(kind, meta, clock) {
|
|
67
|
+
switch(kind){
|
|
68
|
+
case "enum":
|
|
69
|
+
return BigInt(toDistinctValues(meta.items).values.length);
|
|
70
|
+
case "number":
|
|
71
|
+
return toNumberCapacity(meta);
|
|
72
|
+
case "bigint":
|
|
73
|
+
{
|
|
74
|
+
const range = effectiveDiscreteBigint(meta.whereby.min, meta.whereby.max);
|
|
75
|
+
return range.max - range.min + BigInt(1);
|
|
76
|
+
}
|
|
77
|
+
case "date":
|
|
78
|
+
return toRangeCapacity(toEpochRange(meta, clock));
|
|
79
|
+
case "string":
|
|
80
|
+
return toStringDomain(meta.whereby).capacity;
|
|
81
|
+
default:
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export { toCapacity };
|
|
@@ -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 };
|