arktype 2.0.0-dev.6 → 2.0.0-dev.8
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/__tests__/array.test.ts +23 -25
- package/__tests__/config.test.ts +8 -8
- package/__tests__/cyclic.test.ts +3 -3
- package/__tests__/examples.test.ts +3 -3
- package/__tests__/instanceof.test.ts +7 -7
- package/__tests__/intersection.test.ts +2 -2
- package/__tests__/keyTraversal.test.ts +15 -15
- package/__tests__/keywords.test.ts +50 -37
- package/__tests__/literal.test.ts +2 -2
- package/__tests__/match.bench.ts +6 -6
- package/__tests__/narrow.test.ts +17 -23
- package/__tests__/objectLiteral.test.ts +4 -4
- package/__tests__/operator.bench.ts +4 -4
- package/__tests__/{morph.test.ts → pipe.test.ts} +136 -75
- package/__tests__/scope.test.ts +5 -15
- package/__tests__/traverse.test.ts +36 -35
- package/__tests__/type.test.ts +8 -10
- package/ark.ts +14 -11
- package/generic.ts +8 -6
- package/main.ts +3 -3
- package/match.ts +59 -70
- package/out/ark.d.ts +4 -3
- package/out/ark.d.ts.map +1 -1
- package/out/ark.js +4 -4
- package/out/ark.js.map +1 -1
- package/out/generic.d.ts.map +1 -1
- package/out/generic.js +8 -6
- package/out/generic.js.map +1 -1
- package/out/main.d.ts +3 -3
- package/out/main.d.ts.map +1 -1
- package/out/main.js +3 -2
- package/out/main.js.map +1 -1
- package/out/match.d.ts.map +1 -1
- package/out/match.js +50 -56
- package/out/match.js.map +1 -1
- package/out/parser/generic.d.ts +3 -3
- package/out/parser/generic.d.ts.map +1 -1
- package/out/parser/generic.js +4 -5
- package/out/parser/generic.js.map +1 -1
- package/out/parser/objectLiteral.d.ts.map +1 -1
- package/out/parser/objectLiteral.js +2 -3
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/validate.d.ts +2 -2
- package/out/parser/semantic/validate.d.ts.map +1 -1
- package/out/parser/semantic/validate.js.map +1 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +1 -1
- package/out/parser/string/reduce/dynamic.js +4 -8
- package/out/parser/string/reduce/dynamic.js.map +1 -1
- package/out/parser/string/shift/operand/date.js +2 -4
- package/out/parser/string/shift/operand/date.js.map +1 -1
- package/out/parser/string/shift/operand/enclosed.js +6 -8
- package/out/parser/string/shift/operand/enclosed.js.map +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
- package/out/parser/string/shift/operand/genericArgs.js +4 -5
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts +4 -4
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.js +5 -10
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +1 -1
- package/out/parser/string/shift/operator/bounds.js +4 -8
- package/out/parser/string/shift/operator/bounds.js.map +1 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +1 -1
- package/out/parser/string/shift/operator/divisor.js +1 -2
- package/out/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/scanner.d.ts.map +1 -1
- package/out/parser/string/shift/scanner.js +2 -4
- package/out/parser/string/shift/scanner.js.map +1 -1
- package/out/parser/string/string.d.ts.map +1 -1
- package/out/parser/string/string.js +1 -2
- package/out/parser/string/string.js.map +1 -1
- package/out/parser/tuple.d.ts.map +1 -1
- package/out/parser/tuple.js +12 -15
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +9 -9
- package/out/scope.d.ts.map +1 -1
- package/out/scope.js +11 -33
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +38 -18
- package/out/type.d.ts.map +1 -1
- package/out/type.js +32 -1
- package/out/type.js.map +1 -1
- package/package.json +3 -3
- package/parser/generic.ts +11 -12
- package/parser/objectLiteral.ts +2 -4
- package/parser/semantic/validate.ts +2 -3
- package/parser/string/reduce/dynamic.ts +7 -10
- package/parser/string/shift/operand/date.ts +3 -6
- package/parser/string/shift/operand/enclosed.ts +8 -8
- package/parser/string/shift/operand/genericArgs.ts +8 -8
- package/parser/string/shift/operand/unenclosed.ts +14 -23
- package/parser/string/shift/operator/bounds.ts +7 -9
- package/parser/string/shift/operator/divisor.ts +2 -3
- package/parser/string/shift/scanner.ts +2 -4
- package/parser/string/string.ts +2 -3
- package/parser/tuple.ts +15 -16
- package/scope.ts +24 -48
- package/type.ts +150 -35
package/__tests__/narrow.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { attest, contextualize } from "@arktype/attest"
|
|
2
2
|
import type { Narrowed, Out, of, string } from "@arktype/schema"
|
|
3
|
-
import { type equals
|
|
4
|
-
import { type
|
|
3
|
+
import { reference, type equals } from "@arktype/util"
|
|
4
|
+
import { type, type Type } from "arktype"
|
|
5
5
|
|
|
6
6
|
contextualize(() => {
|
|
7
7
|
it("implicit problem", () => {
|
|
@@ -10,15 +10,13 @@ contextualize(() => {
|
|
|
10
10
|
const odd = type(["number", ":", isOdd])
|
|
11
11
|
attest<number>(odd.infer)
|
|
12
12
|
attest(odd.json).equals({ domain: "number", predicate: [isOddRef] })
|
|
13
|
-
attest(odd(1)
|
|
14
|
-
attest(odd(2).
|
|
15
|
-
"must be valid according to isOdd (was 2)"
|
|
16
|
-
)
|
|
13
|
+
attest(odd(1)).equals(1)
|
|
14
|
+
attest(odd(2).toString()).snap("must be valid according to isOdd (was 2)")
|
|
17
15
|
})
|
|
18
16
|
|
|
19
17
|
it("implicit problem anonymous", () => {
|
|
20
|
-
const even = type("number", ":",
|
|
21
|
-
attest(even(1).
|
|
18
|
+
const even = type("number", ":", n => n % 2 === 0)
|
|
19
|
+
attest(even(1).toString()).snap(
|
|
22
20
|
"must be valid according to an anonymous predicate (was 1)"
|
|
23
21
|
)
|
|
24
22
|
})
|
|
@@ -27,9 +25,9 @@ contextualize(() => {
|
|
|
27
25
|
const even = type([
|
|
28
26
|
"number",
|
|
29
27
|
":",
|
|
30
|
-
(n, ctx) => n % 3 === 0 || ctx.invalid(
|
|
28
|
+
(n, ctx) => n % 3 === 0 || ctx.invalid("divisible by 3")
|
|
31
29
|
])
|
|
32
|
-
attest(even(1).
|
|
30
|
+
attest(even(1).toString()).snap("must be divisible by 3 (was 1)")
|
|
33
31
|
})
|
|
34
32
|
|
|
35
33
|
it("problem at path", () => {
|
|
@@ -40,16 +38,15 @@ contextualize(() => {
|
|
|
40
38
|
},
|
|
41
39
|
":",
|
|
42
40
|
({ a, b }, ctx) => {
|
|
43
|
-
if (a === b)
|
|
44
|
-
|
|
45
|
-
}
|
|
41
|
+
if (a === b) return true
|
|
42
|
+
|
|
46
43
|
ctx.error({ expected: "equal to b", path: ["a"] })
|
|
47
44
|
ctx.error({ expected: "equal to a", path: ["b"] })
|
|
48
45
|
return false
|
|
49
46
|
}
|
|
50
47
|
])
|
|
51
|
-
attest(abEqual({ a: 1, b: 1 })
|
|
52
|
-
attest(abEqual({ a: 1, b: 2 }).
|
|
48
|
+
attest(abEqual({ a: 1, b: 1 })).equals({ a: 1, b: 1 })
|
|
49
|
+
attest(abEqual({ a: 1, b: 2 }).toString()).snap(
|
|
53
50
|
'a must be equal to b (was {"a":1,"b":2})\nb must be equal to a (was {"a":1,"b":2})'
|
|
54
51
|
)
|
|
55
52
|
})
|
|
@@ -65,11 +62,8 @@ contextualize(() => {
|
|
|
65
62
|
t: equals<t, Expected> extends true ? t : Expected
|
|
66
63
|
) => true
|
|
67
64
|
attest<number | boolean[]>(
|
|
68
|
-
type([
|
|
69
|
-
|
|
70
|
-
":",
|
|
71
|
-
(data) => validateNumberOrBooleanList(data)
|
|
72
|
-
]).infer
|
|
65
|
+
type(["number|boolean[]", ":", data => validateNumberOrBooleanList(data)])
|
|
66
|
+
.infer
|
|
73
67
|
)
|
|
74
68
|
attest(() => {
|
|
75
69
|
// @ts-expect-error
|
|
@@ -85,15 +79,15 @@ contextualize(() => {
|
|
|
85
79
|
s === [...s].reverse().join("") ? true : ctx.invalid("a palindrome")
|
|
86
80
|
])
|
|
87
81
|
attest<Type<string.narrowed>>(palindrome)
|
|
88
|
-
attest(palindrome("dad")
|
|
89
|
-
attest(palindrome("david").
|
|
82
|
+
attest(palindrome("dad")).snap("dad")
|
|
83
|
+
attest(palindrome("david").toString()).snap(
|
|
90
84
|
'must be a palindrome (was "david")'
|
|
91
85
|
)
|
|
92
86
|
})
|
|
93
87
|
|
|
94
88
|
it("narrows the output type of a morph", () => {
|
|
95
89
|
const t = type("string")
|
|
96
|
-
.
|
|
90
|
+
.pipe(s => s.length)
|
|
97
91
|
.narrow((n): n is 5 => n === 5)
|
|
98
92
|
|
|
99
93
|
attest<Type<(In: string) => Out<of<5, Narrowed>>, {}>>(t)
|
|
@@ -92,16 +92,16 @@ contextualize(
|
|
|
92
92
|
// TODO: reenable
|
|
93
93
|
// it("traverse optional", () => {
|
|
94
94
|
// const o = type({ "a?": "string" }).configure({ keys: "strict" })
|
|
95
|
-
// attest(o({ a: "a" })
|
|
96
|
-
// attest(o({})
|
|
97
|
-
// attest(o({ a: 1 }).
|
|
95
|
+
// attest(o({ a: "a" })).snap({ a: "a" })
|
|
96
|
+
// attest(o({})).snap({})
|
|
97
|
+
// attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
|
|
98
98
|
// })
|
|
99
99
|
|
|
100
100
|
// it("multiple bad strict", () => {
|
|
101
101
|
// const t = type({ a: "string", b: "boolean" }).configure({
|
|
102
102
|
// keys: "strict"
|
|
103
103
|
// })
|
|
104
|
-
// attest(t({ a: 1, b: 2 }).
|
|
104
|
+
// attest(t({ a: 1, b: 2 }).toString()).snap(
|
|
105
105
|
// "a must be a string (was number)\nb must be boolean (was number)"
|
|
106
106
|
// )
|
|
107
107
|
// })
|
|
@@ -106,25 +106,25 @@ bench("divisor", () => {
|
|
|
106
106
|
.types([1023, "instantiations"])
|
|
107
107
|
|
|
108
108
|
bench("filter-tuple", () => {
|
|
109
|
-
const _ = type(["boolean", ":",
|
|
109
|
+
const _ = type(["boolean", ":", b => b])
|
|
110
110
|
})
|
|
111
111
|
.median([8.04, "us"])
|
|
112
112
|
.types([583, "instantiations"])
|
|
113
113
|
|
|
114
114
|
bench("filter-chain", () => {
|
|
115
|
-
const _ = type("boolean").narrow(
|
|
115
|
+
const _ = type("boolean").narrow(b => b)
|
|
116
116
|
})
|
|
117
117
|
.median([12.19, "us"])
|
|
118
118
|
.types([42, "instantiations"])
|
|
119
119
|
|
|
120
120
|
bench("morph-tuple", () => {
|
|
121
|
-
const _ = type(["boolean", "=>",
|
|
121
|
+
const _ = type(["boolean", "=>", b => b])
|
|
122
122
|
})
|
|
123
123
|
.median([8.96, "us"])
|
|
124
124
|
.types([562, "instantiations"])
|
|
125
125
|
|
|
126
126
|
bench("morph-chain", () => {
|
|
127
|
-
const _ = type("boolean").
|
|
127
|
+
const _ = type("boolean").pipe(b => b)
|
|
128
128
|
})
|
|
129
129
|
.median([2.98, "us"])
|
|
130
130
|
.types([26, "instantiations"])
|
|
@@ -1,110 +1,172 @@
|
|
|
1
1
|
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
-
import { type Out
|
|
3
|
-
import {
|
|
2
|
+
import { assertNodeKind, type Out } from "@arktype/schema"
|
|
3
|
+
import { scope, type, type Type } from "arktype"
|
|
4
4
|
|
|
5
5
|
contextualize(() => {
|
|
6
6
|
it("base", () => {
|
|
7
|
-
const t = type("number").
|
|
7
|
+
const t = type("number").pipe(data => `${data}`)
|
|
8
8
|
attest<Type<(In: number) => Out<string>>>(t)
|
|
9
9
|
attest<string>(t.infer)
|
|
10
10
|
attest<number>(t.in.infer)
|
|
11
|
-
const
|
|
12
|
-
attest<string |
|
|
13
|
-
const
|
|
14
|
-
attest(
|
|
11
|
+
const out = t(5)
|
|
12
|
+
attest<string | type.errors>(out).equals("5")
|
|
13
|
+
const result = t("foo")
|
|
14
|
+
attest(result.toString()).snap("must be a number (was string)")
|
|
15
15
|
})
|
|
16
16
|
|
|
17
17
|
it("within type", () => {
|
|
18
|
-
const t = type(["boolean", "=>",
|
|
18
|
+
const t = type(["boolean", "=>", data => !data])
|
|
19
19
|
attest<Type<(In: boolean) => Out<boolean>>>(t)
|
|
20
|
-
const
|
|
21
|
-
attest<boolean |
|
|
20
|
+
const out = t(true)
|
|
21
|
+
attest<boolean | type.errors>(out).equals(false)
|
|
22
22
|
})
|
|
23
23
|
|
|
24
|
-
it("
|
|
25
|
-
const
|
|
24
|
+
it("type instance reference", () => {
|
|
25
|
+
const user = type({
|
|
26
26
|
name: "string",
|
|
27
27
|
age: "number"
|
|
28
28
|
})
|
|
29
|
+
const parsedUser = type("string").pipe(s => JSON.parse(s), user)
|
|
30
|
+
|
|
29
31
|
attest<
|
|
30
32
|
Type<
|
|
31
33
|
(In: string) => Out<{
|
|
32
34
|
name: string
|
|
33
35
|
age: number
|
|
34
|
-
}
|
|
35
|
-
{}
|
|
36
|
+
}>
|
|
36
37
|
>
|
|
37
38
|
>(parsedUser)
|
|
39
|
+
|
|
40
|
+
const validUser = { name: "David", age: 30 }
|
|
41
|
+
attest(parsedUser(JSON.stringify(validUser))).equals(validUser)
|
|
42
|
+
const missingKey = { name: "David" }
|
|
43
|
+
attest(parsedUser(JSON.stringify(missingKey)).toString()).snap(
|
|
44
|
+
"age must be defined"
|
|
45
|
+
)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it("many pipes", () => {
|
|
49
|
+
const pipeAlphabet = type("'a'").pipe(
|
|
50
|
+
s => `${s}b` as const,
|
|
51
|
+
s => `${s}c` as const,
|
|
52
|
+
s => `${s}d` as const,
|
|
53
|
+
s => `${s}e` as const,
|
|
54
|
+
s => `${s}f` as const,
|
|
55
|
+
s => `${s}g` as const,
|
|
56
|
+
s => `${s}h` as const
|
|
57
|
+
)
|
|
58
|
+
attest<"abcdefgh">(pipeAlphabet.infer)
|
|
59
|
+
attest(pipeAlphabet("a")).equals("abcdefgh")
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it("uses pipe for consecutive types", () => {
|
|
63
|
+
const bar = type({ bar: "number" })
|
|
64
|
+
const t = type({ foo: "string" }).pipe(bar)
|
|
65
|
+
attest<
|
|
66
|
+
Type<{
|
|
67
|
+
foo: string
|
|
68
|
+
bar: number
|
|
69
|
+
}>
|
|
70
|
+
>(t)
|
|
71
|
+
const expected = type({ foo: "string", bar: "number" })
|
|
72
|
+
attest(t.json).equals(expected.json)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it("uses pipe for many consecutive types", () => {
|
|
76
|
+
const t = type({ a: "1" }).pipe(
|
|
77
|
+
type({ b: "1" }),
|
|
78
|
+
type({ c: "1" }),
|
|
79
|
+
type({ d: "1" })
|
|
80
|
+
)
|
|
81
|
+
attest<
|
|
82
|
+
Type<{
|
|
83
|
+
a: 1
|
|
84
|
+
b: 1
|
|
85
|
+
c: 1
|
|
86
|
+
d: 1
|
|
87
|
+
}>
|
|
88
|
+
>(t)
|
|
89
|
+
const expected = type({ a: "1", b: "1", c: "1", d: "1" })
|
|
90
|
+
attest(t.json).equals(expected.json)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it("two morphs", () => {
|
|
94
|
+
const inefficientStringIsEmpty = type("string").pipe(
|
|
95
|
+
s => s.length,
|
|
96
|
+
length => length === 0
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
attest<Type<(In: string) => Out<boolean>>>(inefficientStringIsEmpty)
|
|
100
|
+
attest(inefficientStringIsEmpty("")).equals(true)
|
|
101
|
+
attest(inefficientStringIsEmpty("foo")).equals(false)
|
|
102
|
+
attest(inefficientStringIsEmpty(0).toString()).snap(
|
|
103
|
+
"must be a string (was number)"
|
|
104
|
+
)
|
|
38
105
|
})
|
|
39
106
|
|
|
40
107
|
it("any as out", () => {
|
|
41
|
-
const t = type("string", "=>",
|
|
108
|
+
const t = type("string", "=>", s => s as any)
|
|
42
109
|
attest<string>(t.in.infer)
|
|
43
110
|
attest<any>(t.infer)
|
|
44
111
|
})
|
|
45
112
|
|
|
46
113
|
it("never as out", () => {
|
|
47
|
-
const t = type("string", "=>",
|
|
114
|
+
const t = type("string", "=>", s => s as never)
|
|
48
115
|
attest<string>(t.in.infer)
|
|
49
116
|
attest<never>(t.infer)
|
|
50
117
|
})
|
|
51
118
|
|
|
52
|
-
it("return
|
|
119
|
+
it("return error", () => {
|
|
53
120
|
const divide100By = type("number", "=>", (n, ctx) =>
|
|
54
121
|
n !== 0 ? 100 / n : ctx.error("non-zero")
|
|
55
122
|
)
|
|
56
123
|
attest<Type<(In: number) => Out<number>>>(divide100By)
|
|
57
|
-
attest(divide100By(5)
|
|
58
|
-
attest(divide100By(0).
|
|
124
|
+
attest(divide100By(5)).equals(20)
|
|
125
|
+
attest(divide100By(0).toString()).snap("must be non-zero (was 0)")
|
|
59
126
|
})
|
|
60
127
|
|
|
61
128
|
it("at path", () => {
|
|
62
|
-
const t = type({ a: ["string", "=>",
|
|
129
|
+
const t = type({ a: ["string", "=>", data => data.length] })
|
|
63
130
|
attest<Type<{ a: (In: string) => Out<number> }>>(t)
|
|
64
131
|
|
|
65
132
|
const input = { a: "four" }
|
|
66
133
|
|
|
67
|
-
const
|
|
134
|
+
const out = t(input)
|
|
68
135
|
|
|
69
|
-
attest<{ a: number } |
|
|
70
|
-
attest<{ a: string } | undefined>(data).snap({ a: "four" })
|
|
71
|
-
// out is cloned before the morph
|
|
72
|
-
attest(out === (input as never)).equals(false)
|
|
73
|
-
// data has the original reference + data
|
|
74
|
-
attest(data === input).equals(true)
|
|
136
|
+
attest<{ a: number } | type.errors>(out).equals({ a: 4 })
|
|
75
137
|
})
|
|
76
138
|
|
|
77
139
|
it("in array", () => {
|
|
78
140
|
const types = scope({
|
|
79
|
-
lengthOfString: ["string", "=>",
|
|
141
|
+
lengthOfString: ["string", "=>", data => data.length],
|
|
80
142
|
mapToLengths: "lengthOfString[]"
|
|
81
143
|
}).export()
|
|
82
144
|
attest<Type<((In: string) => Out<number>)[]>>(types.mapToLengths)
|
|
83
|
-
const
|
|
84
|
-
attest<number[] |
|
|
145
|
+
const out = types.mapToLengths(["1", "22", "333"])
|
|
146
|
+
attest<number[] | type.errors>(out).equals([1, 2, 3])
|
|
85
147
|
})
|
|
86
148
|
|
|
87
149
|
it("object to string", () => {
|
|
88
|
-
const t = type([{ a: "string" }, "=>",
|
|
89
|
-
const
|
|
90
|
-
attest<
|
|
91
|
-
attest<string | undefined>(out).snap('{"a":"foo"}')
|
|
150
|
+
const t = type([{ a: "string" }, "=>", data => JSON.stringify(data)])
|
|
151
|
+
const out = t({ a: "foo" })
|
|
152
|
+
attest<string | type.errors>(out).snap('{"a":"foo"}')
|
|
92
153
|
})
|
|
93
154
|
|
|
94
155
|
it("intersection", () => {
|
|
95
156
|
const $ = scope({
|
|
96
157
|
b: "3.14",
|
|
97
|
-
a: ["number", "=>",
|
|
158
|
+
a: ["number", "=>", data => `${data}`],
|
|
98
159
|
aAndB: () => $.type("a&b"),
|
|
99
160
|
bAndA: () => $.type("b&a")
|
|
100
161
|
})
|
|
101
162
|
const types = $.export()
|
|
102
|
-
assertNodeKind(types.bAndA, "morph")
|
|
103
|
-
assertNodeKind(types.aAndB, "morph")
|
|
163
|
+
assertNodeKind(types.bAndA.raw, "morph")
|
|
164
|
+
assertNodeKind(types.aAndB.raw, "morph")
|
|
165
|
+
|
|
104
166
|
attest<Type<(In: 3.14) => Out<string>>>(types.aAndB)
|
|
105
167
|
attest(types.aAndB.json).snap({
|
|
106
|
-
|
|
107
|
-
morphs: types.aAndB.serializedMorphs
|
|
168
|
+
from: { unit: 3.14 },
|
|
169
|
+
morphs: types.aAndB.raw.serializedMorphs
|
|
108
170
|
})
|
|
109
171
|
attest<typeof types.aAndB>(types.bAndA)
|
|
110
172
|
attest(types.bAndA).equals(types.aAndB)
|
|
@@ -112,29 +174,29 @@ contextualize(() => {
|
|
|
112
174
|
|
|
113
175
|
it("object intersection", () => {
|
|
114
176
|
const $ = scope({
|
|
115
|
-
a: [{ a: "1" }, "=>",
|
|
177
|
+
a: [{ a: "1" }, "=>", data => `${data}`],
|
|
116
178
|
b: { b: "2" },
|
|
117
179
|
c: "a&b"
|
|
118
180
|
})
|
|
119
181
|
const types = $.export()
|
|
120
182
|
// TODO: FIX
|
|
121
183
|
// attest<Type<(In: { a: 1; b: 2 }) => string>>(types.c)
|
|
122
|
-
assertNodeKind(types.c, "morph")
|
|
184
|
+
assertNodeKind(types.c.raw, "morph")
|
|
123
185
|
attest(types.c.json).snap({
|
|
124
|
-
|
|
186
|
+
from: {
|
|
125
187
|
domain: "object",
|
|
126
188
|
prop: [
|
|
127
189
|
{ key: "a", value: { unit: 1 } },
|
|
128
190
|
{ key: "b", value: { unit: 2 } }
|
|
129
191
|
]
|
|
130
192
|
},
|
|
131
|
-
morphs: types.c.serializedMorphs
|
|
193
|
+
morphs: types.c.raw.serializedMorphs
|
|
132
194
|
})
|
|
133
195
|
})
|
|
134
196
|
|
|
135
197
|
it("union", () => {
|
|
136
198
|
const types = scope({
|
|
137
|
-
a: ["number", "=>",
|
|
199
|
+
a: ["number", "=>", data => `${data}`],
|
|
138
200
|
b: "boolean",
|
|
139
201
|
aOrB: "a|b",
|
|
140
202
|
bOrA: "b|a"
|
|
@@ -143,7 +205,7 @@ contextualize(() => {
|
|
|
143
205
|
const serializedMorphs =
|
|
144
206
|
types.aOrB.raw.firstReferenceOfKindOrThrow("morph").serializedMorphs
|
|
145
207
|
attest(types.aOrB.json).snap([
|
|
146
|
-
{
|
|
208
|
+
{ from: "number", morphs: serializedMorphs },
|
|
147
209
|
{ unit: false },
|
|
148
210
|
{ unit: true }
|
|
149
211
|
])
|
|
@@ -158,7 +220,7 @@ contextualize(() => {
|
|
|
158
220
|
|
|
159
221
|
it("deep union", () => {
|
|
160
222
|
const types = scope({
|
|
161
|
-
a: { a: ["number>0", "=>",
|
|
223
|
+
a: { a: ["number>0", "=>", data => `${data}`] },
|
|
162
224
|
b: { a: "Function" },
|
|
163
225
|
c: "a|b"
|
|
164
226
|
}).export()
|
|
@@ -184,7 +246,7 @@ contextualize(() => {
|
|
|
184
246
|
{
|
|
185
247
|
key: "a",
|
|
186
248
|
value: {
|
|
187
|
-
|
|
249
|
+
from: {
|
|
188
250
|
domain: "number",
|
|
189
251
|
min: { exclusive: true, rule: 0 }
|
|
190
252
|
},
|
|
@@ -198,77 +260,77 @@ contextualize(() => {
|
|
|
198
260
|
|
|
199
261
|
it("chained reference", () => {
|
|
200
262
|
const $ = scope({
|
|
201
|
-
a: type("string").
|
|
202
|
-
b: () => $.type("a").
|
|
263
|
+
a: type("string").pipe(s => s.length),
|
|
264
|
+
b: () => $.type("a").pipe(n => n === 0)
|
|
203
265
|
})
|
|
204
266
|
const types = $.export()
|
|
205
267
|
attest<Type<(In: string) => Out<boolean>>>(types.b)
|
|
206
|
-
assertNodeKind(types.b, "morph")
|
|
268
|
+
assertNodeKind(types.b.raw, "morph")
|
|
207
269
|
attest(types.b.json).snap({
|
|
208
|
-
|
|
209
|
-
morphs: types.b.serializedMorphs
|
|
270
|
+
from: "string",
|
|
271
|
+
morphs: types.b.raw.serializedMorphs
|
|
210
272
|
})
|
|
211
273
|
})
|
|
212
274
|
|
|
213
275
|
it("chained nested", () => {
|
|
214
276
|
const $ = scope({
|
|
215
|
-
a: type("string").
|
|
216
|
-
b: () => $.type({ a: "a" }).
|
|
277
|
+
a: type("string").pipe(s => s.length),
|
|
278
|
+
b: () => $.type({ a: "a" }).pipe(({ a }) => a === 0)
|
|
217
279
|
})
|
|
218
280
|
|
|
219
281
|
const types = $.export()
|
|
220
282
|
attest<Type<(In: { a: string }) => Out<boolean>>>(types.b)
|
|
221
|
-
assertNodeKind(types.b, "morph")
|
|
222
|
-
assertNodeKind(types.a, "morph")
|
|
283
|
+
assertNodeKind(types.b.raw, "morph")
|
|
284
|
+
assertNodeKind(types.a.raw, "morph")
|
|
223
285
|
attest(types.b.json).snap({
|
|
224
|
-
|
|
286
|
+
from: {
|
|
225
287
|
domain: "object",
|
|
226
288
|
prop: [
|
|
227
289
|
{
|
|
228
290
|
key: "a",
|
|
229
291
|
value: {
|
|
230
|
-
|
|
231
|
-
morphs: types.a.serializedMorphs
|
|
292
|
+
from: "string",
|
|
293
|
+
morphs: types.a.raw.serializedMorphs
|
|
232
294
|
}
|
|
233
295
|
}
|
|
234
296
|
]
|
|
235
297
|
},
|
|
236
|
-
morphs: types.b.serializedMorphs
|
|
298
|
+
morphs: types.b.raw.serializedMorphs
|
|
237
299
|
})
|
|
238
300
|
})
|
|
239
301
|
|
|
240
302
|
it("directly nested", () => {
|
|
241
303
|
const t = type([
|
|
242
304
|
{
|
|
243
|
-
a: ["string", "=>",
|
|
305
|
+
a: ["string", "=>", s => s.length]
|
|
244
306
|
},
|
|
245
307
|
"=>",
|
|
246
308
|
({ a }) => a === 0
|
|
247
309
|
])
|
|
248
310
|
// TODO: check
|
|
249
311
|
// attest<Type<(In: { a: string }) => Out<boolean>>>(t)
|
|
250
|
-
assertNodeKind(t, "morph")
|
|
251
|
-
const nestedMorph = t.firstReferenceOfKindOrThrow("morph")
|
|
312
|
+
assertNodeKind(t.raw, "morph")
|
|
313
|
+
const nestedMorph = t.raw.firstReferenceOfKindOrThrow("morph")
|
|
252
314
|
attest(t.json).snap({
|
|
253
|
-
|
|
315
|
+
from: {
|
|
254
316
|
domain: "object",
|
|
255
317
|
prop: [
|
|
256
318
|
{
|
|
257
319
|
key: "a",
|
|
258
320
|
value: {
|
|
259
|
-
|
|
321
|
+
from: "string",
|
|
260
322
|
morphs: nestedMorph.serializedMorphs
|
|
261
323
|
}
|
|
262
324
|
}
|
|
263
325
|
]
|
|
264
326
|
},
|
|
265
|
-
morphs: t.serializedMorphs
|
|
327
|
+
morphs: t.raw.serializedMorphs
|
|
266
328
|
})
|
|
267
329
|
})
|
|
268
330
|
|
|
269
331
|
it("discriminable tuple union", () => {
|
|
270
332
|
const $ = scope({
|
|
271
|
-
a: () => $.type(["string"]).
|
|
333
|
+
a: () => $.type(["string"]).pipe(s => [...s, "!"]),
|
|
272
334
|
b: ["boolean"],
|
|
273
335
|
c: () => $.type("a|b")
|
|
274
336
|
})
|
|
@@ -282,21 +344,20 @@ contextualize(() => {
|
|
|
282
344
|
"=>",
|
|
283
345
|
(s, ctx) => {
|
|
284
346
|
const result = Number.parseInt(s)
|
|
285
|
-
if (Number.isNaN(result))
|
|
286
|
-
|
|
287
|
-
}
|
|
347
|
+
if (Number.isNaN(result)) return ctx.error("an integer string")
|
|
348
|
+
|
|
288
349
|
return result
|
|
289
350
|
}
|
|
290
351
|
])
|
|
291
352
|
attest<Type<(In: string) => Out<number>>>(parsedInt)
|
|
292
|
-
attest(parsedInt("5")
|
|
293
|
-
attest(parsedInt("five").
|
|
353
|
+
attest(parsedInt("5")).snap(5)
|
|
354
|
+
attest(parsedInt("five").toString()).snap(
|
|
294
355
|
'must be an integer string (was "five")'
|
|
295
356
|
)
|
|
296
357
|
})
|
|
297
358
|
|
|
298
359
|
it("nullable return", () => {
|
|
299
|
-
const toNullableNumber = type(["string", "=>",
|
|
360
|
+
const toNullableNumber = type(["string", "=>", s => s.length || null])
|
|
300
361
|
attest<Type<(In: string) => Out<number | null>>>(toNullableNumber)
|
|
301
362
|
})
|
|
302
363
|
|
|
@@ -304,7 +365,7 @@ contextualize(() => {
|
|
|
304
365
|
const toUndefinableNumber = type([
|
|
305
366
|
"string",
|
|
306
367
|
"=>",
|
|
307
|
-
|
|
368
|
+
s => s.length || undefined
|
|
308
369
|
])
|
|
309
370
|
attest<Type<(In: string) => Out<number | undefined>>>(toUndefinableNumber)
|
|
310
371
|
})
|
|
@@ -313,7 +374,7 @@ contextualize(() => {
|
|
|
313
374
|
const toMaybeNumber = type([
|
|
314
375
|
"string",
|
|
315
376
|
"=>",
|
|
316
|
-
|
|
377
|
+
s =>
|
|
317
378
|
s.length === 0 ? undefined
|
|
318
379
|
: s.length === 1 ? null
|
|
319
380
|
: s.length
|
package/__tests__/scope.test.ts
CHANGED
|
@@ -53,22 +53,12 @@ contextualize(() => {
|
|
|
53
53
|
|
|
54
54
|
it("infers input and output", () => {
|
|
55
55
|
const $ = scope({
|
|
56
|
-
a: ["string", "=>",
|
|
56
|
+
a: ["string", "=>", s => s.length]
|
|
57
57
|
})
|
|
58
58
|
attest<{ a: number }>($.infer)
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
|
|
60
|
+
attest<{ a: string }>($.inferIn)
|
|
61
61
|
})
|
|
62
|
-
// TODO: remove if not preserving
|
|
63
|
-
// it("scope.scope", () => {
|
|
64
|
-
// const $ = scope({
|
|
65
|
-
// a: "string"
|
|
66
|
-
// })
|
|
67
|
-
// const importer = $.scope({ b: "a[]" })
|
|
68
|
-
// attest<{ b: string[] }>(importer.infer)
|
|
69
|
-
// const t = importer.type("b")
|
|
70
|
-
// attest(t.json).equals(type("string[]").json)
|
|
71
|
-
// })
|
|
72
62
|
|
|
73
63
|
it("infers its own helpers", () => {
|
|
74
64
|
const $ = scope({
|
|
@@ -151,8 +141,8 @@ contextualize(() => {
|
|
|
151
141
|
X: Apple
|
|
152
142
|
}).export()
|
|
153
143
|
|
|
154
|
-
const
|
|
155
|
-
attest(
|
|
144
|
+
const out = X({ pear: { tasty: true } })
|
|
145
|
+
attest(out).snap({ pear: { tasty: true } })
|
|
156
146
|
})
|
|
157
147
|
describe("define", () => {
|
|
158
148
|
it("ark", () => {
|