@litert/typeguard 1.3.0 → 1.4.0
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/CHANGES.md +7 -1
- package/lib/BuiltInTypeCompiler.d.ts +1 -1
- package/lib/BuiltInTypeCompiler.js +4 -4
- package/lib/BuiltInTypeCompiler.js.map +1 -1
- package/lib/BuiltInTypes.d.ts +1 -1
- package/lib/BuiltInTypes.js +1 -1
- package/lib/Common.d.ts +6 -5
- package/lib/Common.d.ts.map +1 -1
- package/lib/Common.js +1 -1
- package/lib/Compiler.d.ts +1 -1
- package/lib/Compiler.d.ts.map +1 -1
- package/lib/Compiler.js +57 -35
- package/lib/Compiler.js.map +1 -1
- package/lib/Context.d.ts +2 -2
- package/lib/Context.d.ts.map +1 -1
- package/lib/Context.js +2 -2
- package/lib/Context.js.map +1 -1
- package/lib/FilterCompiler.d.ts +3 -3
- package/lib/FilterCompiler.d.ts.map +1 -1
- package/lib/FilterCompiler.js +4 -4
- package/lib/FilterCompiler.js.map +1 -1
- package/lib/InlineCompiler.d.ts +2 -2
- package/lib/InlineCompiler.d.ts.map +1 -1
- package/lib/InlineCompiler.js +5 -7
- package/lib/InlineCompiler.js.map +1 -1
- package/lib/Internal.d.ts +9 -4
- package/lib/Internal.d.ts.map +1 -1
- package/lib/Internal.js +45 -4
- package/lib/Internal.js.map +1 -1
- package/lib/Modifiers.d.ts +2 -1
- package/lib/Modifiers.d.ts.map +1 -1
- package/lib/Modifiers.js +3 -2
- package/lib/Modifiers.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/langs/JavaScript.d.ts +1 -1
- package/lib/langs/JavaScript.js +2 -2
- package/package.json +10 -12
- package/src/examples/quick-start.ts +5 -1
- package/src/lib/BuiltInTypeCompiler.ts +4 -4
- package/src/lib/BuiltInTypes.ts +1 -1
- package/src/lib/Common.ts +5 -4
- package/src/lib/Compiler.ts +66 -39
- package/src/lib/Context.ts +2 -2
- package/src/lib/FilterCompiler.ts +3 -3
- package/src/lib/InlineCompiler.ts +6 -9
- package/src/lib/Internal.ts +73 -4
- package/src/lib/Modifiers.ts +3 -1
- package/src/lib/index.ts +1 -1
- package/src/lib/langs/JavaScript.ts +2 -2
- package/src/test/00-all.ts +1 -1
- package/src/test/01-elemental-types.ts +580 -580
- package/src/test/02-array-and-list.ts +43 -43
- package/src/test/03-tuple.ts +61 -61
- package/src/test/04-from-string.ts +501 -501
- package/src/test/05-string-asserts.ts +203 -203
- package/src/test/06-structure.ts +71 -71
- package/src/test/07-modifiers.ts +297 -48
- package/src/test/08-map-and-dict.ts +11 -11
- package/src/test/09-exceptions.ts +11 -11
- package/src/test/abstracts.ts +57 -29
package/src/test/abstracts.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2023 Angus Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -19,9 +19,9 @@ import * as TypeGuard from '../lib';
|
|
|
19
19
|
|
|
20
20
|
export interface ITestItem {
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
title: string;
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
value: any;
|
|
25
25
|
|
|
26
26
|
expect: boolean | 'throw';
|
|
27
27
|
}
|
|
@@ -42,60 +42,60 @@ export interface ITestSuite {
|
|
|
42
42
|
sections: ITestRule[];
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
export function
|
|
45
|
+
export function defaultItems(
|
|
46
46
|
items: Record<string, boolean>,
|
|
47
47
|
defaultValue: boolean = false
|
|
48
48
|
): ITestItem[] {
|
|
49
49
|
|
|
50
50
|
return [
|
|
51
51
|
{
|
|
52
|
-
'
|
|
53
|
-
'
|
|
52
|
+
'title': 'true',
|
|
53
|
+
'value': true,
|
|
54
54
|
'expect': items['true'] === undefined ? defaultValue : items['true']
|
|
55
55
|
},
|
|
56
56
|
{
|
|
57
|
-
'
|
|
58
|
-
'
|
|
57
|
+
'title': 'false',
|
|
58
|
+
'value': false,
|
|
59
59
|
'expect': items['false'] === undefined ? defaultValue : items['false']
|
|
60
60
|
},
|
|
61
61
|
{
|
|
62
|
-
'
|
|
63
|
-
'
|
|
62
|
+
'title': 'undefined',
|
|
63
|
+
'value': undefined,
|
|
64
64
|
'expect': items['undefined'] === undefined ? defaultValue : items['undefined']
|
|
65
65
|
},
|
|
66
66
|
{
|
|
67
|
-
'
|
|
68
|
-
'
|
|
67
|
+
'title': 'null',
|
|
68
|
+
'value': null,
|
|
69
69
|
'expect': items['null'] === undefined ? defaultValue : items['null']
|
|
70
70
|
},
|
|
71
71
|
{
|
|
72
|
-
'
|
|
73
|
-
'
|
|
72
|
+
'title': 'empty array',
|
|
73
|
+
'value': [],
|
|
74
74
|
'expect': items['empty array'] === undefined ? defaultValue : items['empty array']
|
|
75
75
|
},
|
|
76
76
|
{
|
|
77
|
-
'
|
|
78
|
-
'
|
|
77
|
+
'title': 'string \'hello\'',
|
|
78
|
+
'value': 'hello',
|
|
79
79
|
'expect': items['string \'hello\''] === undefined ? defaultValue : items['string \'hello\'']
|
|
80
80
|
},
|
|
81
81
|
{
|
|
82
|
-
'
|
|
83
|
-
'
|
|
82
|
+
'title': 'empty string',
|
|
83
|
+
'value': '',
|
|
84
84
|
'expect': items['empty string'] === undefined ? defaultValue : items['empty string']
|
|
85
85
|
},
|
|
86
86
|
{
|
|
87
|
-
'
|
|
88
|
-
'
|
|
87
|
+
'title': 'object',
|
|
88
|
+
'value': {},
|
|
89
89
|
'expect': items['object'] === undefined ? defaultValue : items['object']
|
|
90
90
|
},
|
|
91
91
|
{
|
|
92
|
-
'
|
|
93
|
-
'
|
|
92
|
+
'title': 'number 0',
|
|
93
|
+
'value': 0,
|
|
94
94
|
'expect': items['number 0'] === undefined ? defaultValue : items['number 0']
|
|
95
95
|
},
|
|
96
96
|
{
|
|
97
|
-
'
|
|
98
|
-
'
|
|
97
|
+
'title': 'number 1',
|
|
98
|
+
'value': 1,
|
|
99
99
|
'expect': items['number 1'] === undefined ? defaultValue : items['number 1']
|
|
100
100
|
}
|
|
101
101
|
];
|
|
@@ -112,6 +112,20 @@ compiler.addPredefinedType<string>(
|
|
|
112
112
|
}
|
|
113
113
|
);
|
|
114
114
|
|
|
115
|
+
compiler.addPredefinedType<string>(
|
|
116
|
+
'trim_string',
|
|
117
|
+
function(i, minLen: number = 0, maxLen?: number): i is string {
|
|
118
|
+
return typeof i === 'string' && i.trim().length >= minLen && i.trim().length <= (maxLen ?? i.length);
|
|
119
|
+
}
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
compiler.addPredefinedType<string>(
|
|
123
|
+
'enum',
|
|
124
|
+
function(i, ...candidates: any[]): i is string {
|
|
125
|
+
return candidates.includes(i);
|
|
126
|
+
}
|
|
127
|
+
);
|
|
128
|
+
|
|
115
129
|
const compiler2 = TypeGuard.createInlineCompiler();
|
|
116
130
|
|
|
117
131
|
compiler2.addPredefinedType<string>(
|
|
@@ -123,11 +137,25 @@ compiler2.addPredefinedType<string>(
|
|
|
123
137
|
}
|
|
124
138
|
);
|
|
125
139
|
|
|
140
|
+
compiler2.addPredefinedType<string>(
|
|
141
|
+
'trim_string',
|
|
142
|
+
function(i, minLen: number = 0, maxLen?: number): i is string {
|
|
143
|
+
return typeof i === 'string' && i.trim().length >= minLen && i.trim().length <= (maxLen ?? i.length);
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
compiler2.addPredefinedType<string>(
|
|
148
|
+
'enum',
|
|
149
|
+
function(i, ...candidates: any[]): i is string {
|
|
150
|
+
return candidates.includes(i);
|
|
151
|
+
}
|
|
152
|
+
);
|
|
153
|
+
|
|
126
154
|
export function assertItem(input: unknown, expect: boolean | 'throw'): ITestItem {
|
|
127
155
|
|
|
128
156
|
return {
|
|
129
|
-
|
|
130
|
-
|
|
157
|
+
title: JSON.stringify(input),
|
|
158
|
+
value: input,
|
|
131
159
|
expect
|
|
132
160
|
};
|
|
133
161
|
}
|
|
@@ -190,16 +218,16 @@ export function createTestDefinition(suite: ITestSuite) {
|
|
|
190
218
|
it(`${
|
|
191
219
|
item.expect ? 'PASSED' : 'REJECTED'
|
|
192
220
|
} when input ${
|
|
193
|
-
item.
|
|
221
|
+
item.title
|
|
194
222
|
}.`, function() {
|
|
195
223
|
|
|
196
224
|
assert.equal(
|
|
197
|
-
checkWithTrace(item.
|
|
225
|
+
checkWithTrace(item.value),
|
|
198
226
|
item.expect
|
|
199
227
|
);
|
|
200
228
|
|
|
201
229
|
assert.equal(
|
|
202
|
-
checkWithoutTrace(item.
|
|
230
|
+
checkWithoutTrace(item.value),
|
|
203
231
|
item.expect
|
|
204
232
|
);
|
|
205
233
|
});
|