shapedef 1.0.3 → 1.0.4
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/package.json +1 -1
- package/src/shape-utils.ts +44 -1
- package/src/translations/typescript.ts +6 -1
package/package.json
CHANGED
package/src/shape-utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TupleOf } from "./common";
|
|
2
|
-
import { Shape } from "./shape";
|
|
2
|
+
import { Shape, ShapeRef } from "./shape";
|
|
3
3
|
|
|
4
4
|
export const shapeCompare = (a: Shape, b: Shape): boolean =>
|
|
5
5
|
JSON.stringify(a) === JSON.stringify(b);
|
|
@@ -44,3 +44,46 @@ export const simplifyShape = (shape: Shape): Shape => {
|
|
|
44
44
|
return shape;
|
|
45
45
|
}
|
|
46
46
|
};
|
|
47
|
+
|
|
48
|
+
export const getShapeDeps = (shape: Shape): ShapeRef[] => {
|
|
49
|
+
const deps: Map<string, ShapeRef> = new Map();
|
|
50
|
+
|
|
51
|
+
switch (shape.type) {
|
|
52
|
+
case "ref":
|
|
53
|
+
{
|
|
54
|
+
deps.set(shape.name, shape);
|
|
55
|
+
if (shape.hint) {
|
|
56
|
+
getShapeDeps(shape.hint).forEach((x) => deps.set(x.name, x));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
break;
|
|
60
|
+
case "mapping":
|
|
61
|
+
{
|
|
62
|
+
for (const inner of Object.values(shape.rec)) {
|
|
63
|
+
getShapeDeps(inner).forEach((x) => deps.set(x.name, x));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
case "array":
|
|
68
|
+
{
|
|
69
|
+
getShapeDeps(shape.item).forEach((x) => deps.set(x.name, x));
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
case "union":
|
|
73
|
+
{
|
|
74
|
+
for (const inner of shape.ofs) {
|
|
75
|
+
getShapeDeps(inner).forEach((x) => deps.set(x.name, x));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
case "tuple":
|
|
80
|
+
{
|
|
81
|
+
for (const inner of shape.tup) {
|
|
82
|
+
getShapeDeps(inner).forEach((x) => deps.set(x.name, x));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return Array.from(deps.values());
|
|
89
|
+
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { mkpad } from "../common/padding";
|
|
2
2
|
import { Shape, shapes } from "../shape";
|
|
3
3
|
import { ShapeCollection } from "../shape-collection";
|
|
4
|
+
import { getShapeDeps } from "../shape-utils";
|
|
4
5
|
import { TranslationFunc } from "./translation";
|
|
5
6
|
|
|
6
7
|
type Context = {
|
|
@@ -150,7 +151,11 @@ export const typescriptCollection = (
|
|
|
150
151
|
) => {
|
|
151
152
|
const { summaryName = "Schema" } = options;
|
|
152
153
|
|
|
153
|
-
const items = Array.from(collection.values())
|
|
154
|
+
const items = Array.from(collection.values()).toSorted((a, b) => {
|
|
155
|
+
const depsA = getShapeDeps(a);
|
|
156
|
+
const depsB = getShapeDeps(b);
|
|
157
|
+
return depsA.length - depsB.length;
|
|
158
|
+
});
|
|
154
159
|
|
|
155
160
|
const rec = shapes.mapping(
|
|
156
161
|
Object.fromEntries(
|