@fncts/schema 0.0.12 → 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 (62) hide show
  1. package/ParseFailure.d.ts +18 -0
  2. package/ParseResult.d.ts +2 -1
  3. package/Schema/derivations.d.ts +6 -2
  4. package/Show.d.ts +8 -0
  5. package/_cjs/Gen.cjs +1 -2
  6. package/_cjs/Gen.cjs.map +1 -1
  7. package/_cjs/ParseFailure.cjs +28 -0
  8. package/_cjs/ParseFailure.cjs.map +1 -0
  9. package/_cjs/ParseResult.cjs +4 -3
  10. package/_cjs/ParseResult.cjs.map +1 -1
  11. package/_cjs/Parser/api.cjs +2 -2
  12. package/_cjs/Parser/api.cjs.map +1 -1
  13. package/_cjs/Parser/interpreter.cjs +8 -8
  14. package/_cjs/Parser/interpreter.cjs.map +1 -1
  15. package/_cjs/Schema/api/conc.cjs +1 -1
  16. package/_cjs/Schema/api/conc.cjs.map +1 -1
  17. package/_cjs/Schema/api/hashMap.cjs +1 -1
  18. package/_cjs/Schema/api/hashMap.cjs.map +1 -1
  19. package/_cjs/Schema/api/immutableArray.cjs +1 -1
  20. package/_cjs/Schema/api/immutableArray.cjs.map +1 -1
  21. package/_cjs/Schema/api/list.cjs +1 -1
  22. package/_cjs/Schema/api/list.cjs.map +1 -1
  23. package/_cjs/Schema/derivations.cjs +9 -3
  24. package/_cjs/Schema/derivations.cjs.map +1 -1
  25. package/_cjs/Show.cjs +146 -0
  26. package/_cjs/Show.cjs.map +1 -0
  27. package/_mjs/Gen.mjs +1 -2
  28. package/_mjs/Gen.mjs.map +1 -1
  29. package/_mjs/ParseFailure.mjs +20 -0
  30. package/_mjs/ParseFailure.mjs.map +1 -0
  31. package/_mjs/ParseResult.mjs +4 -3
  32. package/_mjs/ParseResult.mjs.map +1 -1
  33. package/_mjs/Parser/api.mjs +2 -2
  34. package/_mjs/Parser/api.mjs.map +1 -1
  35. package/_mjs/Parser/interpreter.mjs +8 -8
  36. package/_mjs/Parser/interpreter.mjs.map +1 -1
  37. package/_mjs/Schema/api/conc.mjs +1 -1
  38. package/_mjs/Schema/api/conc.mjs.map +1 -1
  39. package/_mjs/Schema/api/hashMap.mjs +1 -1
  40. package/_mjs/Schema/api/hashMap.mjs.map +1 -1
  41. package/_mjs/Schema/api/immutableArray.mjs +1 -1
  42. package/_mjs/Schema/api/immutableArray.mjs.map +1 -1
  43. package/_mjs/Schema/api/list.mjs +1 -1
  44. package/_mjs/Schema/api/list.mjs.map +1 -1
  45. package/_mjs/Schema/derivations.mjs +8 -3
  46. package/_mjs/Schema/derivations.mjs.map +1 -1
  47. package/_mjs/Show.mjs +138 -0
  48. package/_mjs/Show.mjs.map +1 -0
  49. package/_src/Gen.ts +0 -1
  50. package/_src/ParseFailure.ts +18 -0
  51. package/_src/ParseResult.ts +3 -3
  52. package/_src/Parser/api.ts +2 -2
  53. package/_src/Parser/interpreter.ts +8 -8
  54. package/_src/Schema/api/conc.ts +1 -1
  55. package/_src/Schema/api/hashMap.ts +1 -1
  56. package/_src/Schema/api/immutableArray.ts +1 -1
  57. package/_src/Schema/api/list.ts +1 -1
  58. package/_src/Schema/derivations.ts +15 -3
  59. package/_src/Show.ts +169 -0
  60. package/_src/global.ts +8 -0
  61. package/global.d.ts +8 -0
  62. package/package.json +3 -3
package/_src/Show.ts ADDED
@@ -0,0 +1,169 @@
1
+ import type { TemplateLiteral, TemplateLiteralSpan } from "@fncts/schema/AST";
2
+
3
+ import { ASTTag } from "@fncts/schema/AST";
4
+ import { ASTAnnotation } from "@fncts/schema/ASTAnnotation";
5
+ import { memoize } from "@fncts/schema/utils";
6
+
7
+ /**
8
+ * @tsplus getter fncts.schema.Schema show
9
+ */
10
+ export function show<A>(self: Schema<A>): string {
11
+ const ev = go(self.ast);
12
+ return ev.run;
13
+ }
14
+
15
+ const go = memoize(function go(ast: AST): Eval<string> {
16
+ AST.concrete(ast);
17
+ switch (ast._tag) {
18
+ case ASTTag.Declaration: {
19
+ return ast.annotations.get(ASTAnnotation.Identifier).match(
20
+ () => Eval.now("Unknown Type"),
21
+ (id) => {
22
+ return ast.typeParameters
23
+ .traverse(Eval.Applicative)(go)
24
+ .map((ts) => {
25
+ if (ts.length <= 0) {
26
+ return id;
27
+ } else {
28
+ return `${id}<${ts.join(", ")}>`;
29
+ }
30
+ });
31
+ },
32
+ );
33
+ }
34
+ case ASTTag.Literal: {
35
+ if (ast.literal === null) {
36
+ return Eval.now("null");
37
+ } else {
38
+ return Eval.now(ast.literal.toString());
39
+ }
40
+ }
41
+ case ASTTag.UniqueSymbol:
42
+ return Eval.now(ast.symbol.toString());
43
+ case ASTTag.UndefinedKeyword:
44
+ return Eval.now("undefined");
45
+ case ASTTag.VoidKeyword:
46
+ return Eval.now("void");
47
+ case ASTTag.NeverKeyword:
48
+ return Eval.now("never");
49
+ case ASTTag.UnknownKeyword:
50
+ return Eval.now("unknown");
51
+ case ASTTag.AnyKeyword:
52
+ return Eval.now("any");
53
+ case ASTTag.StringKeyword:
54
+ return Eval.now("string");
55
+ case ASTTag.NumberKeyword:
56
+ return Eval.now("number");
57
+ case ASTTag.BooleanKeyword:
58
+ return Eval.now("boolean");
59
+ case ASTTag.BigIntKeyword:
60
+ return Eval.now("bigint");
61
+ case ASTTag.SymbolKeyword:
62
+ return Eval.now("symbol");
63
+ case ASTTag.ObjectKeyword:
64
+ return Eval.now("object");
65
+ case ASTTag.TemplateLiteral:
66
+ return Eval.now("`" + formatTemplateLiteral(ast) + "`");
67
+ case ASTTag.Tuple:
68
+ return Do((Δ) => {
69
+ const elements = Δ(ast.elements.map((element) => element.type).traverse(Eval.Applicative)(go));
70
+ const restElements = Δ(
71
+ ast.rest.match(
72
+ () => Eval.now(Vector.empty<string>()),
73
+ (rest) => rest.traverse(Eval.Applicative)(go),
74
+ ),
75
+ );
76
+
77
+ return Δ(
78
+ Eval(() => {
79
+ if (elements.length === 0 && restElements.length === 1) {
80
+ if (ast.isReadonly) {
81
+ return `ReadonlyArray<${restElements[0]}>`;
82
+ } else {
83
+ return `Array<${restElements[0]}>`;
84
+ }
85
+ }
86
+
87
+ const prefix = (ast.isReadonly ? "readonly " : "") + "[" + elements.join(", ");
88
+ const middle = restElements.length > 0 ? ", " : "";
89
+ const suffix = restElements.map((s) => `...${s}`).join(", ") + "]";
90
+ return prefix + middle + suffix;
91
+ }),
92
+ );
93
+ });
94
+ case ASTTag.TypeLiteral:
95
+ return Do((Δ) => {
96
+ const propertySignatures = Δ(ast.propertySignatures.traverse(Eval.Applicative)((ps) => go(ps.type)));
97
+ const indexSignatures = Δ(
98
+ ast.indexSignatures.traverse(Eval.Applicative)((is) => go(is.parameter).zip(go(is.type))),
99
+ );
100
+
101
+ const required: Array<[PropertyKey, string]> = [];
102
+ const optional: Array<[PropertyKey, string]> = [];
103
+
104
+ ast.propertySignatures.forEachWithIndex((i, ps) => {
105
+ const name = ps.name;
106
+ if (!ps.isOptional) {
107
+ required.push([name, propertySignatures[i]!]);
108
+ } else {
109
+ optional.push([name, propertySignatures[i]!]);
110
+ }
111
+ });
112
+
113
+ const prefix = "{";
114
+ const properties = required
115
+ .concat(optional)
116
+ .sort(([k1], [k2]) => k1.toLocaleString().localeCompare(k2.toLocaleString()))
117
+ .map(([propertyKey, type]) => `${String(propertyKey)}: ${type}`)
118
+ .join(", ");
119
+ const index = indexSignatures.map(([param, type]) => `[x: ${param}]: ${type}`).join(", ");
120
+ const suffix = "}";
121
+
122
+ return prefix + " " + properties + (index.length === 0 ? "" : ", ") + index + " " + suffix;
123
+ });
124
+ case ASTTag.Union:
125
+ return ast.types
126
+ .traverse(Eval.Applicative)(go)
127
+ .map((ts) => ts.join(" | "));
128
+ case ASTTag.Lazy: {
129
+ const f = () => go(ast.getAST());
130
+ const get = memoize<typeof f, Eval<string>>(f);
131
+ return Eval.defer(() => get(f));
132
+ }
133
+ case ASTTag.Enum: {
134
+ return Eval.now(ast.enums.map(([name]) => name).join(" | "));
135
+ }
136
+ case ASTTag.Refinement: {
137
+ return ast.annotations.get(ASTAnnotation.Identifier).match(
138
+ () => go(ast.from).map((from) => `Refined<${from}>`),
139
+ (id) => Eval.now(id),
140
+ );
141
+ }
142
+ case ASTTag.Transform:
143
+ return go(ast.to);
144
+ case ASTTag.Validation: {
145
+ return go(ast.from).map((from) => {
146
+ const validationNames = ast.validation.map((v) => v.name).join(" & ");
147
+
148
+ if (validationNames.length <= 0) {
149
+ return from;
150
+ }
151
+
152
+ return `${from} & ${validationNames}`;
153
+ });
154
+ }
155
+ }
156
+ });
157
+
158
+ function formatTemplateLiteralSpan(span: TemplateLiteralSpan): string {
159
+ switch (span.type._tag) {
160
+ case ASTTag.StringKeyword:
161
+ return "${string}";
162
+ case ASTTag.NumberKeyword:
163
+ return "${number}";
164
+ }
165
+ }
166
+
167
+ function formatTemplateLiteral(ast: TemplateLiteral): string {
168
+ return ast.head + ast.spans.map((span) => formatTemplateLiteralSpan(span) + span.literal).join("");
169
+ }
package/_src/global.ts CHANGED
@@ -39,6 +39,10 @@ import { ASTAnnotationMap } from "@fncts/schema/ASTAnnotationMap";
39
39
  * @tsplus global
40
40
  */
41
41
  import { ParseError } from "@fncts/schema/ParseError";
42
+ /**
43
+ * @tsplus global
44
+ */
45
+ import { ParseFailure } from "@fncts/schema/ParseFailure";
42
46
  /**
43
47
  * @tsplus global
44
48
  */
@@ -55,6 +59,10 @@ import {} from "@fncts/schema/Schema";
55
59
  * @tsplus global
56
60
  */
57
61
  import { OptionalSchema, Schema } from "@fncts/schema/Schema/definition";
62
+ /**
63
+ * @tsplus global
64
+ */
65
+ import {} from "@fncts/schema/Show";
58
66
  /**
59
67
  * @tsplus global
60
68
  */
package/global.d.ts CHANGED
@@ -38,6 +38,10 @@ import { ASTAnnotationMap } from "@fncts/schema/ASTAnnotationMap";
38
38
  * @tsplus global
39
39
  */
40
40
  import { ParseError } from "@fncts/schema/ParseError";
41
+ /**
42
+ * @tsplus global
43
+ */
44
+ import { ParseFailure } from "@fncts/schema/ParseFailure";
41
45
  /**
42
46
  * @tsplus global
43
47
  */
@@ -54,6 +58,10 @@ import {} from "@fncts/schema/Schema";
54
58
  * @tsplus global
55
59
  */
56
60
  import { OptionalSchema, Schema } from "@fncts/schema/Schema/definition";
61
+ /**
62
+ * @tsplus global
63
+ */
64
+ import {} from "@fncts/schema/Show";
57
65
  /**
58
66
  * @tsplus global
59
67
  */
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@fncts/schema",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "dependencies": {
5
- "@fncts/base": "0.0.33",
6
- "@fncts/typelevel": "0.0.16"
5
+ "@fncts/base": "0.0.34",
6
+ "@fncts/typelevel": "0.0.17"
7
7
  },
8
8
  "exports": {
9
9
  "./*": {