@fncts/schema 0.0.4 → 0.0.6
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/AST.d.ts +528 -0
- package/ASTAnnotation.d.ts +82 -0
- package/ASTAnnotationMap.d.ts +14 -0
- package/Gen.d.ts +15 -0
- package/InvalidInterpretationError.d.ts +5 -0
- package/ParseError.d.ts +107 -0
- package/ParseResult.d.ts +24 -0
- package/Parser/api.d.ts +42 -0
- package/Parser/definition.d.ts +22 -0
- package/Parser/interpreter.d.ts +6 -0
- package/Parser.d.ts +2 -0
- package/Schema/api/conc.d.ts +18 -0
- package/Schema/api/hashMap.d.ts +21 -0
- package/Schema/api/immutableArray.d.ts +23 -0
- package/Schema/api/list.d.ts +23 -0
- package/Schema/api/maybe.d.ts +21 -0
- package/Schema/api.d.ts +243 -0
- package/Schema/definition.d.ts +29 -0
- package/Schema/derivations.d.ts +70 -0
- package/Schema.d.ts +2 -144
- package/_cjs/AST.cjs +1171 -0
- package/_cjs/AST.cjs.map +1 -0
- package/_cjs/ASTAnnotation.cjs +111 -0
- package/_cjs/ASTAnnotation.cjs.map +1 -0
- package/_cjs/ASTAnnotationMap.cjs +35 -0
- package/_cjs/ASTAnnotationMap.cjs.map +1 -0
- package/_cjs/Gen.cjs +185 -0
- package/_cjs/Gen.cjs.map +1 -0
- package/_cjs/InvalidInterpretationError.cjs +18 -0
- package/_cjs/InvalidInterpretationError.cjs.map +1 -0
- package/_cjs/ParseError.cjs +222 -0
- package/_cjs/ParseError.cjs.map +1 -0
- package/_cjs/{Decoder.cjs → ParseResult.cjs} +24 -22
- package/_cjs/ParseResult.cjs.map +1 -0
- package/_cjs/Parser/api.cjs +80 -0
- package/_cjs/Parser/api.cjs.map +1 -0
- package/_cjs/{Guard.cjs → Parser/definition.cjs} +17 -22
- package/_cjs/Parser/definition.cjs.map +1 -0
- package/_cjs/Parser/interpreter.cjs +409 -0
- package/_cjs/Parser/interpreter.cjs.map +1 -0
- package/_cjs/Parser.cjs +28 -0
- package/_cjs/Parser.cjs.map +1 -0
- package/_cjs/Schema/api/conc.cjs +84 -0
- package/_cjs/Schema/api/conc.cjs.map +1 -0
- package/_cjs/Schema/api/hashMap.cjs +161 -0
- package/_cjs/Schema/api/hashMap.cjs.map +1 -0
- package/_cjs/Schema/api/immutableArray.cjs +90 -0
- package/_cjs/Schema/api/immutableArray.cjs.map +1 -0
- package/_cjs/Schema/api/list.cjs +98 -0
- package/_cjs/Schema/api/list.cjs.map +1 -0
- package/_cjs/Schema/api/maybe.cjs +75 -0
- package/_cjs/Schema/api/maybe.cjs.map +1 -0
- package/_cjs/Schema/api.cjs +424 -0
- package/_cjs/Schema/api.cjs.map +1 -0
- package/_cjs/Schema/definition.cjs +26 -0
- package/_cjs/Schema/definition.cjs.map +1 -0
- package/_cjs/Schema/derivations.cjs +108 -0
- package/_cjs/Schema/derivations.cjs.map +1 -0
- package/_cjs/Schema.cjs +20 -237
- package/_cjs/Schema.cjs.map +1 -1
- package/_cjs/utils.cjs +52 -0
- package/_cjs/utils.cjs.map +1 -0
- package/_mjs/AST.mjs +1060 -0
- package/_mjs/AST.mjs.map +1 -0
- package/_mjs/ASTAnnotation.mjs +80 -0
- package/_mjs/ASTAnnotation.mjs.map +1 -0
- package/_mjs/ASTAnnotationMap.mjs +27 -0
- package/_mjs/ASTAnnotationMap.mjs.map +1 -0
- package/_mjs/Gen.mjs +176 -0
- package/_mjs/Gen.mjs.map +1 -0
- package/_mjs/InvalidInterpretationError.mjs +10 -0
- package/_mjs/InvalidInterpretationError.mjs.map +1 -0
- package/_mjs/ParseError.mjs +200 -0
- package/_mjs/ParseError.mjs.map +1 -0
- package/_mjs/ParseResult.mjs +21 -0
- package/_mjs/ParseResult.mjs.map +1 -0
- package/_mjs/Parser/api.mjs +67 -0
- package/_mjs/Parser/api.mjs.map +1 -0
- package/_mjs/Parser/definition.mjs +15 -0
- package/_mjs/Parser/definition.mjs.map +1 -0
- package/_mjs/Parser/interpreter.mjs +401 -0
- package/_mjs/Parser/interpreter.mjs.map +1 -0
- package/_mjs/Parser.mjs +4 -0
- package/_mjs/Parser.mjs.map +1 -0
- package/_mjs/Schema/api/conc.mjs +72 -0
- package/_mjs/Schema/api/conc.mjs.map +1 -0
- package/_mjs/Schema/api/hashMap.mjs +150 -0
- package/_mjs/Schema/api/hashMap.mjs.map +1 -0
- package/_mjs/Schema/api/immutableArray.mjs +79 -0
- package/_mjs/Schema/api/immutableArray.mjs.map +1 -0
- package/_mjs/Schema/api/list.mjs +87 -0
- package/_mjs/Schema/api/list.mjs.map +1 -0
- package/_mjs/Schema/api/maybe.mjs +64 -0
- package/_mjs/Schema/api/maybe.mjs.map +1 -0
- package/_mjs/Schema/api.mjs +367 -0
- package/_mjs/Schema/api.mjs.map +1 -0
- package/_mjs/Schema/definition.mjs +16 -0
- package/_mjs/Schema/definition.mjs.map +1 -0
- package/_mjs/Schema/derivations.mjs +94 -0
- package/_mjs/Schema/derivations.mjs.map +1 -0
- package/_mjs/Schema.mjs +3 -212
- package/_mjs/Schema.mjs.map +1 -1
- package/_mjs/utils.mjs +41 -0
- package/_mjs/utils.mjs.map +1 -0
- package/_src/AST.ts +1353 -0
- package/_src/ASTAnnotation.ts +98 -0
- package/_src/ASTAnnotationMap.ts +38 -0
- package/_src/Gen.ts +171 -0
- package/_src/InvalidInterpretationError.ts +6 -0
- package/_src/ParseError.ts +237 -0
- package/_src/ParseResult.ts +26 -0
- package/_src/Parser/api.ts +71 -0
- package/_src/Parser/definition.ts +24 -0
- package/_src/Parser/interpreter.ts +442 -0
- package/_src/Parser.ts +4 -0
- package/_src/Schema/api/conc.ts +78 -0
- package/_src/Schema/api/hashMap.ts +184 -0
- package/_src/Schema/api/immutableArray.ts +88 -0
- package/_src/Schema/api/list.ts +96 -0
- package/_src/Schema/api/maybe.ts +68 -0
- package/_src/Schema/api.ts +530 -0
- package/_src/Schema/definition.ts +32 -0
- package/_src/Schema/derivations.ts +185 -0
- package/_src/Schema.ts +4 -254
- package/_src/global.ts +53 -0
- package/_src/utils.ts +48 -0
- package/global.d.ts +52 -0
- package/package.json +2 -2
- package/utils.d.ts +8 -0
- package/Decoder.d.ts +0 -3
- package/Encoder.d.ts +0 -4
- package/Guard.d.ts +0 -3
- package/Schemable.d.ts +0 -39
- package/_cjs/Decoder.cjs.map +0 -1
- package/_cjs/Encoder.cjs +0 -45
- package/_cjs/Encoder.cjs.map +0 -1
- package/_cjs/Guard.cjs.map +0 -1
- package/_cjs/Schemable.cjs +0 -6
- package/_cjs/Schemable.cjs.map +0 -1
- package/_mjs/Decoder.mjs +0 -20
- package/_mjs/Decoder.mjs.map +0 -1
- package/_mjs/Encoder.mjs +0 -36
- package/_mjs/Encoder.mjs.map +0 -1
- package/_mjs/Guard.mjs +0 -20
- package/_mjs/Guard.mjs.map +0 -1
- package/_mjs/Schemable.mjs +0 -2
- package/_mjs/Schemable.mjs.map +0 -1
- package/_src/Decoder.ts +0 -20
- package/_src/Encoder.ts +0 -38
- package/_src/Guard.ts +0 -20
- package/_src/Schemable.ts +0 -46
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Sized } from "@fncts/test/control/Sized";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ImmutableArray,
|
|
5
|
+
ImmutableArrayTypeId,
|
|
6
|
+
ImmutableArrayVariance,
|
|
7
|
+
} from "@fncts/base/collection/immutable/ImmutableArray";
|
|
8
|
+
import { Vector } from "@fncts/base/collection/immutable/Vector";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @tsplus getter fncts.schema.Schema immutableArray
|
|
12
|
+
* @tsplus static fncts.schema.SchemaOps immutableArray
|
|
13
|
+
*/
|
|
14
|
+
export function immutableArray<A>(value: Schema<A>): Schema<ImmutableArray<A>> {
|
|
15
|
+
return Schema.declaration(
|
|
16
|
+
Vector(value),
|
|
17
|
+
inline(value),
|
|
18
|
+
parser,
|
|
19
|
+
ASTAnnotationMap.empty.annotate(ASTAnnotation.Identifier, "ImmutableArray").annotate(ASTAnnotation.GenHook, gen),
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @tsplus getter fncts.schema.Schema immutableArrayFromArray
|
|
25
|
+
* @tsplus static fncts.schema.SchemaOps immutableArrayFromArray
|
|
26
|
+
*/
|
|
27
|
+
export function immutableArrayFromArray<A>(value: Schema<A>): Schema<ImmutableArray<A>> {
|
|
28
|
+
return Schema.array(value).transform(
|
|
29
|
+
immutableArray(value),
|
|
30
|
+
(input) => new ImmutableArray(input),
|
|
31
|
+
(input) => input._array,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @tsplus derive fncts.schema.Schema[fncts.ImmutableArray]<_> 10
|
|
37
|
+
*/
|
|
38
|
+
export function deriveImmutableArray<A extends ImmutableArray<any>>(
|
|
39
|
+
...[value]: [A] extends [ImmutableArray<infer _A>]
|
|
40
|
+
? Check<Check.IsEqual<A, ImmutableArray<_A>>> extends Check.True
|
|
41
|
+
? [value: Schema<_A>]
|
|
42
|
+
: never
|
|
43
|
+
: never
|
|
44
|
+
): Schema<A> {
|
|
45
|
+
return unsafeCoerce(immutableArrayFromArray(value));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function parser<A>(value: Schema<A>): Parser<ImmutableArray<A>> {
|
|
49
|
+
const schema = immutableArray(value);
|
|
50
|
+
return Parser.make((u, options) => {
|
|
51
|
+
if (!ImmutableArray.is(u)) {
|
|
52
|
+
return ParseResult.fail(ParseError.TypeError(schema.ast, u));
|
|
53
|
+
}
|
|
54
|
+
const out: Array<A> = [];
|
|
55
|
+
const errors = Vector.emptyPushable<ParseError>();
|
|
56
|
+
const allErrors = options?.allErrors;
|
|
57
|
+
const index = 0;
|
|
58
|
+
for (const v of u) {
|
|
59
|
+
const t = value.decode(v, options);
|
|
60
|
+
Either.concrete(t);
|
|
61
|
+
if (t.isLeft()) {
|
|
62
|
+
errors.push(ParseError.IndexError(index, t.left));
|
|
63
|
+
if (allErrors) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
return ParseResult.failures(errors);
|
|
67
|
+
} else {
|
|
68
|
+
out.push(t.right);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return errors.isNonEmpty() ? ParseResult.failures(errors) : ParseResult.succeed(new ImmutableArray(out));
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function inline<A>(value: Schema<A>): Schema<ImmutableArray<A>> {
|
|
76
|
+
return Schema.struct({
|
|
77
|
+
[ImmutableArrayTypeId]: Schema.uniqueSymbol(ImmutableArrayTypeId),
|
|
78
|
+
[ImmutableArrayVariance]: Schema.any,
|
|
79
|
+
_array: Schema.array(value),
|
|
80
|
+
[Symbol.equals]: Schema.any,
|
|
81
|
+
[Symbol.hash]: Schema.any,
|
|
82
|
+
[Symbol.iterator]: Schema.any,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function gen<A>(value: Gen<Sized, A>): Gen<Sized, ImmutableArray<A>> {
|
|
87
|
+
return Gen.array(value).map((array) => new ImmutableArray(array));
|
|
88
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Sized } from "@fncts/test/control/Sized";
|
|
2
|
+
|
|
3
|
+
import { ListTypeId } from "@fncts/base/collection/immutable/List";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @tsplus static fncts.schema.SchemaOps list
|
|
7
|
+
* @tsplus getter fncts.Schema.Schema list
|
|
8
|
+
*/
|
|
9
|
+
export function list<A>(value: Schema<A>): Schema<List<A>> {
|
|
10
|
+
return Schema.declaration(
|
|
11
|
+
Vector(value),
|
|
12
|
+
inline(value),
|
|
13
|
+
parser,
|
|
14
|
+
ASTAnnotationMap.empty.annotate(ASTAnnotation.Identifier, "List").annotate(ASTAnnotation.GenHook, gen),
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @tsplus static fncts.schema.SchemaOps listFromArray
|
|
20
|
+
* @tsplus getter fncts.Schema.Schema listFromArray
|
|
21
|
+
*/
|
|
22
|
+
export function listFromArray<A>(value: Schema<A>): Schema<List<A>> {
|
|
23
|
+
return Schema.array(value).transform(
|
|
24
|
+
list(value),
|
|
25
|
+
(input) => List.from(input),
|
|
26
|
+
(input) => Array.from(input),
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @tsplus derive fncts.schema.Schema[fncts.List]<_> 10
|
|
32
|
+
*/
|
|
33
|
+
export function deriveList<A extends List<any>>(
|
|
34
|
+
...[value]: [A] extends [List<infer _A>]
|
|
35
|
+
? Check<Check.IsEqual<A, List<_A>>> extends Check.True
|
|
36
|
+
? [value: Schema<_A>]
|
|
37
|
+
: never
|
|
38
|
+
: never
|
|
39
|
+
): Schema<A> {
|
|
40
|
+
return unsafeCoerce(listFromArray(value));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function parser<A>(value: Schema<A>): Parser<List<A>> {
|
|
44
|
+
const schema = list(value);
|
|
45
|
+
return Parser.make((u, options) => {
|
|
46
|
+
if (!List.is(u)) {
|
|
47
|
+
return ParseResult.fail(ParseError.TypeError(schema.ast, u));
|
|
48
|
+
}
|
|
49
|
+
const out = new ListBuffer<A>();
|
|
50
|
+
const errors = Vector.emptyPushable<ParseError>();
|
|
51
|
+
const allErrors = options?.allErrors;
|
|
52
|
+
const index = 0;
|
|
53
|
+
for (const v of u) {
|
|
54
|
+
const t = value.decode(v, options);
|
|
55
|
+
Either.concrete(t);
|
|
56
|
+
if (t.isLeft()) {
|
|
57
|
+
errors.push(ParseError.IndexError(index, t.left));
|
|
58
|
+
if (allErrors) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
return ParseResult.failures(errors);
|
|
62
|
+
} else {
|
|
63
|
+
out.append(t.right);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return errors.isNonEmpty() ? ParseResult.failures(errors) : ParseResult.succeed(out.toList);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function nil<A>(_value: Schema<A>): Schema<Nil<A>> {
|
|
71
|
+
return Schema.struct({
|
|
72
|
+
_tag: Schema.literal("Nil"),
|
|
73
|
+
[ListTypeId]: Schema.uniqueSymbol(ListTypeId),
|
|
74
|
+
[Symbol.iterator]: Schema.any,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function cons<A>(value: Schema<A>): Schema<Cons<A>> {
|
|
79
|
+
return Schema.lazy(() =>
|
|
80
|
+
Schema.struct({
|
|
81
|
+
_tag: Schema.literal("Cons"),
|
|
82
|
+
[ListTypeId]: Schema.uniqueSymbol(ListTypeId),
|
|
83
|
+
[Symbol.iterator]: Schema.any,
|
|
84
|
+
head: value,
|
|
85
|
+
tail: inline(value),
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function inline<A>(value: Schema<A>): Schema<List<A>> {
|
|
91
|
+
return Schema.lazy(() => Schema.union(nil(value), cons(value)));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function gen<A>(value: Gen<Sized, A>): Gen<Sized, List<A>> {
|
|
95
|
+
return Gen.array(value).map((array) => List.from(array));
|
|
96
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Check } from "@fncts/typelevel";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @tsplus static fncts.schema.SchemaOps maybe
|
|
5
|
+
* @tsplus getter fncts.schema.Schema maybe
|
|
6
|
+
*/
|
|
7
|
+
export function maybe<A>(value: Schema<A>): Schema<Maybe<A>> {
|
|
8
|
+
return Schema.declaration(
|
|
9
|
+
Vector(value),
|
|
10
|
+
maybeInline(value),
|
|
11
|
+
maybeParser,
|
|
12
|
+
ASTAnnotationMap.empty.annotate(ASTAnnotation.Identifier, "Maybe"),
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @tsplus static fncts.schema.SchemaOps maybeFromNullable
|
|
18
|
+
* @tsplus getter fncts.schema.Schema maybeFromNullable
|
|
19
|
+
*/
|
|
20
|
+
export function maybeFromNullable<A>(value: Schema<A>): Schema<Maybe<A>> {
|
|
21
|
+
return Schema.union(Schema.undefined, value.nullable).transform(value.maybe, Maybe.fromNullable, (input) =>
|
|
22
|
+
input.getOrElse(null),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @tsplus derive fncts.schema.Schema[fncts.Maybe]<_> 10
|
|
28
|
+
*/
|
|
29
|
+
export function deriveMaybe<A extends Maybe<any>>(
|
|
30
|
+
...[value]: [A] extends [Maybe<infer _A>]
|
|
31
|
+
? Check<Check.IsEqual<A, Maybe<_A>>> extends Check.True
|
|
32
|
+
? [value: Schema<_A>]
|
|
33
|
+
: never
|
|
34
|
+
: never
|
|
35
|
+
): Schema<A> {
|
|
36
|
+
return unsafeCoerce(maybeFromNullable(value));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function maybeParser<A>(value: Schema<A>): Parser<Maybe<A>> {
|
|
40
|
+
const schema = maybe(value);
|
|
41
|
+
return Parser.make((u, options) => {
|
|
42
|
+
if (!Maybe.isMaybe(u)) {
|
|
43
|
+
return ParseResult.fail(ParseError.TypeError(schema.ast, u));
|
|
44
|
+
}
|
|
45
|
+
Maybe.concrete(u);
|
|
46
|
+
if (u.isNothing()) {
|
|
47
|
+
return ParseResult.succeed(Nothing());
|
|
48
|
+
} else {
|
|
49
|
+
return value.decode(u.value, options).map((a) => Just(a));
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function maybeInline<A>(value: Schema<A>): Schema<Maybe<A>> {
|
|
55
|
+
return Schema.union(
|
|
56
|
+
Schema.struct({
|
|
57
|
+
_tag: Schema.literal("Nothing"),
|
|
58
|
+
[Symbol.equals]: Schema.any,
|
|
59
|
+
[Symbol.hash]: Schema.any,
|
|
60
|
+
}),
|
|
61
|
+
Schema.struct({
|
|
62
|
+
_tag: Schema.literal("Just"),
|
|
63
|
+
value,
|
|
64
|
+
[Symbol.equals]: Schema.any,
|
|
65
|
+
[Symbol.hash]: Schema.any,
|
|
66
|
+
}),
|
|
67
|
+
) as unknown as Schema<Maybe<A>>;
|
|
68
|
+
}
|