@litert/typeguard 1.0.1 → 1.3.0-dev.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 +16 -0
- package/lib/BuiltInTypeCompiler.d.ts +4 -4
- package/lib/BuiltInTypeCompiler.d.ts.map +1 -1
- package/lib/BuiltInTypeCompiler.js +167 -166
- package/lib/BuiltInTypeCompiler.js.map +1 -1
- package/lib/BuiltInTypes.d.ts +1 -1
- package/lib/BuiltInTypes.js +37 -36
- package/lib/BuiltInTypes.js.map +1 -1
- package/lib/Common.d.ts +27 -10
- package/lib/Common.d.ts.map +1 -1
- package/lib/Common.js +1 -1
- package/lib/Compiler.d.ts +2 -2
- package/lib/Compiler.d.ts.map +1 -1
- package/lib/Compiler.js +159 -102
- package/lib/Compiler.js.map +1 -1
- package/lib/Context.d.ts +6 -5
- package/lib/Context.d.ts.map +1 -1
- package/lib/Context.js +11 -7
- package/lib/Context.js.map +1 -1
- package/lib/FilterCompiler.d.ts +5 -5
- package/lib/FilterCompiler.d.ts.map +1 -1
- package/lib/FilterCompiler.js +25 -24
- package/lib/FilterCompiler.js.map +1 -1
- package/lib/InlineCompiler.d.ts +12 -5
- package/lib/InlineCompiler.d.ts.map +1 -1
- package/lib/InlineCompiler.js +18 -6
- package/lib/InlineCompiler.js.map +1 -1
- package/lib/Internal.d.ts +6 -4
- package/lib/Internal.d.ts.map +1 -1
- package/lib/Internal.js +12 -10
- package/lib/Internal.js.map +1 -1
- package/lib/Modifiers.d.ts +1 -1
- package/lib/Modifiers.js +2 -1
- package/lib/Modifiers.js.map +1 -1
- package/lib/index.d.ts +5 -5
- package/lib/index.js +19 -7
- package/lib/index.js.map +1 -1
- package/lib/langs/JavaScript.d.ts +2 -2
- package/lib/langs/JavaScript.d.ts.map +1 -1
- package/lib/langs/JavaScript.js +55 -29
- package/lib/langs/JavaScript.js.map +1 -1
- package/package.json +17 -13
- package/src/examples/quick-start.ts +172 -0
- package/src/lib/BuiltInTypeCompiler.ts +171 -171
- package/src/lib/BuiltInTypes.ts +36 -36
- package/src/lib/Common.ts +49 -10
- package/src/lib/Compiler.ts +354 -247
- package/src/lib/Context.ts +11 -13
- package/src/lib/FilterCompiler.ts +84 -84
- package/src/lib/InlineCompiler.ts +41 -15
- package/src/lib/Internal.ts +17 -13
- package/src/lib/Modifiers.ts +2 -2
- package/src/lib/index.ts +5 -5
- package/src/lib/langs/JavaScript.ts +84 -31
- package/src/test/00-all.ts +10 -10
- package/src/test/01-elemental-types.ts +1111 -1110
- package/src/test/02-array-and-list.ts +75 -75
- package/src/test/03-tuple.ts +87 -87
- package/src/test/04-from-string.ts +849 -848
- package/src/test/05-string-asserts.ts +422 -422
- package/src/test/06-structure.ts +107 -107
- package/src/test/07-modifiers.ts +151 -42
- package/src/test/08-map-and-dict.ts +46 -46
- package/src/test/09-exceptions.ts +30 -9
- package/src/test/abstracts.ts +83 -45
- package/dist/typeguard.amd.d.ts +0 -588
- package/dist/typeguard.amd.d.ts.map +0 -1
- package/dist/typeguard.amd.js +0 -2069
- package/dist/typeguard.amd.js.map +0 -1
- package/dist/typeguard.system.d.ts +0 -588
- package/dist/typeguard.system.d.ts.map +0 -1
- package/dist/typeguard.system.js +0 -2185
- package/dist/typeguard.system.js.map +0 -1
- package/src/samples/quick-start.ts +0 -52
- package/tsconfig-amd.json +0 -72
- package/tsconfig-systemjs.json +0 -72
package/src/lib/BuiltInTypes.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2022 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.
|
|
@@ -14,72 +14,72 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
export const STRING =
|
|
17
|
+
export const STRING = 'string';
|
|
18
18
|
|
|
19
|
-
export const ASCII_STRING =
|
|
19
|
+
export const ASCII_STRING = 'ascii_string';
|
|
20
20
|
|
|
21
|
-
export const HEX_STRING =
|
|
21
|
+
export const HEX_STRING = 'hex_string';
|
|
22
22
|
|
|
23
|
-
export const LATIN_STRING =
|
|
23
|
+
export const LATIN_STRING = 'latin_string';
|
|
24
24
|
|
|
25
|
-
export const NUMERIC =
|
|
25
|
+
export const NUMERIC = 'numeric';
|
|
26
26
|
|
|
27
|
-
export const SAFE_INT =
|
|
27
|
+
export const SAFE_INT = 'safe_int';
|
|
28
28
|
|
|
29
|
-
export const INT =
|
|
29
|
+
export const INT = 'int';
|
|
30
30
|
|
|
31
|
-
export const INT8 =
|
|
31
|
+
export const INT8 = 'int8';
|
|
32
32
|
|
|
33
|
-
export const INT16 =
|
|
33
|
+
export const INT16 = 'int16';
|
|
34
34
|
|
|
35
|
-
export const INT32 =
|
|
35
|
+
export const INT32 = 'int32';
|
|
36
36
|
|
|
37
|
-
export const INT64 =
|
|
37
|
+
export const INT64 = 'int64';
|
|
38
38
|
|
|
39
|
-
export const SAFE_UINT =
|
|
39
|
+
export const SAFE_UINT = 'safe_uint';
|
|
40
40
|
|
|
41
|
-
export const UINT =
|
|
41
|
+
export const UINT = 'uint';
|
|
42
42
|
|
|
43
|
-
export const UINT8 =
|
|
43
|
+
export const UINT8 = 'uint8';
|
|
44
44
|
|
|
45
|
-
export const UINT16 =
|
|
45
|
+
export const UINT16 = 'uint16';
|
|
46
46
|
|
|
47
|
-
export const UINT32 =
|
|
47
|
+
export const UINT32 = 'uint32';
|
|
48
48
|
|
|
49
|
-
export const UINT64 =
|
|
49
|
+
export const UINT64 = 'uint64';
|
|
50
50
|
|
|
51
|
-
export const BOOLEAN =
|
|
51
|
+
export const BOOLEAN = 'boolean';
|
|
52
52
|
|
|
53
|
-
export const ARRAY =
|
|
53
|
+
export const ARRAY = 'array';
|
|
54
54
|
|
|
55
|
-
export const ANY =
|
|
55
|
+
export const ANY = 'any';
|
|
56
56
|
|
|
57
|
-
export const STRUCT =
|
|
57
|
+
export const STRUCT = 'struct';
|
|
58
58
|
|
|
59
|
-
export const NULL =
|
|
59
|
+
export const NULL = 'null';
|
|
60
60
|
|
|
61
|
-
export const UNDEFINED =
|
|
61
|
+
export const UNDEFINED = 'undefined';
|
|
62
62
|
|
|
63
|
-
export const VOID =
|
|
63
|
+
export const VOID = 'void';
|
|
64
64
|
|
|
65
|
-
export const FALSE =
|
|
65
|
+
export const FALSE = 'false';
|
|
66
66
|
|
|
67
|
-
export const TRUE =
|
|
67
|
+
export const TRUE = 'true';
|
|
68
68
|
|
|
69
|
-
export const FALSE_VALUE =
|
|
69
|
+
export const FALSE_VALUE = 'false_value';
|
|
70
70
|
|
|
71
|
-
export const TRUE_VALUE =
|
|
71
|
+
export const TRUE_VALUE = 'true_value';
|
|
72
72
|
|
|
73
|
-
export const OPTIONAL =
|
|
73
|
+
export const OPTIONAL = 'optional';
|
|
74
74
|
|
|
75
|
-
export const REQUIRED =
|
|
75
|
+
export const REQUIRED = 'required';
|
|
76
76
|
|
|
77
|
-
export const DECIMAL =
|
|
77
|
+
export const DECIMAL = 'decimal';
|
|
78
78
|
|
|
79
|
-
export const NUMBER =
|
|
79
|
+
export const NUMBER = 'number';
|
|
80
80
|
|
|
81
|
-
export const FLOAT =
|
|
81
|
+
export const FLOAT = 'float';
|
|
82
82
|
|
|
83
|
-
export const UDECIMAL =
|
|
83
|
+
export const UDECIMAL = 'udecimal';
|
|
84
84
|
|
|
85
|
-
export const UFLOAT =
|
|
85
|
+
export const UFLOAT = 'ufloat';
|
package/src/lib/Common.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2022 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.
|
|
@@ -14,19 +14,26 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
export type
|
|
17
|
+
export type ITypeChecker<T> = (v: unknown, errorTraces?: string[]) => v is T;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated Use `ITypeChecker` instead.
|
|
21
|
+
*/
|
|
22
|
+
export type TypeChecker<T> = ITypeChecker<T>;
|
|
18
23
|
|
|
19
24
|
export interface ICompileOutputArgument {
|
|
20
25
|
|
|
21
26
|
/**
|
|
22
27
|
* The name of argument.
|
|
23
28
|
*/
|
|
24
|
-
|
|
29
|
+
'name': string;
|
|
25
30
|
|
|
26
31
|
/**
|
|
27
32
|
* The type of argument.
|
|
28
33
|
*/
|
|
29
|
-
|
|
34
|
+
'type': string;
|
|
35
|
+
|
|
36
|
+
'initial': string;
|
|
30
37
|
}
|
|
31
38
|
|
|
32
39
|
export interface ICompileResult {
|
|
@@ -34,22 +41,22 @@ export interface ICompileResult {
|
|
|
34
41
|
/**
|
|
35
42
|
* The arguments of checker.
|
|
36
43
|
*/
|
|
37
|
-
|
|
44
|
+
'arguments': ICompileOutputArgument[];
|
|
38
45
|
|
|
39
46
|
/**
|
|
40
47
|
* The variable name of pre-defined type checkers.
|
|
41
48
|
*/
|
|
42
|
-
|
|
49
|
+
'typeSlotName': string;
|
|
43
50
|
|
|
44
51
|
/**
|
|
45
52
|
* The source code of checker.
|
|
46
53
|
*/
|
|
47
|
-
|
|
54
|
+
'source': string;
|
|
48
55
|
|
|
49
56
|
/**
|
|
50
57
|
* The predefined types used by this type.
|
|
51
58
|
*/
|
|
52
|
-
|
|
59
|
+
'referredTypes': string[];
|
|
53
60
|
}
|
|
54
61
|
|
|
55
62
|
export interface ICompileOptions {
|
|
@@ -57,12 +64,19 @@ export interface ICompileOptions {
|
|
|
57
64
|
/**
|
|
58
65
|
* The rules to be compiled.
|
|
59
66
|
*/
|
|
60
|
-
|
|
67
|
+
'rule': any;
|
|
61
68
|
|
|
62
69
|
/**
|
|
63
70
|
* Give this type a name, so it could be used as a pre-defined type.
|
|
64
71
|
*/
|
|
65
|
-
|
|
72
|
+
'name'?: string;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Enable adding failed asserts tracing info to the second argument.
|
|
76
|
+
*
|
|
77
|
+
* @default false
|
|
78
|
+
*/
|
|
79
|
+
'traceErrors'?: boolean;
|
|
66
80
|
}
|
|
67
81
|
|
|
68
82
|
export interface ILanguageBuilder {
|
|
@@ -83,6 +97,25 @@ export interface ILanguageBuilder {
|
|
|
83
97
|
*/
|
|
84
98
|
caseIf(cond: string[], expr: string): string;
|
|
85
99
|
|
|
100
|
+
orAddTrace(
|
|
101
|
+
expr: string,
|
|
102
|
+
vTrace: string,
|
|
103
|
+
vTraceStack: string,
|
|
104
|
+
path: string,
|
|
105
|
+
): string;
|
|
106
|
+
|
|
107
|
+
addTrace(
|
|
108
|
+
vTrace: string,
|
|
109
|
+
vTraceStack: string,
|
|
110
|
+
path: string,
|
|
111
|
+
): string;
|
|
112
|
+
|
|
113
|
+
stringTemplateVar(varExpr: string): string;
|
|
114
|
+
|
|
115
|
+
numberTemplateVar(varExpr: string): string;
|
|
116
|
+
|
|
117
|
+
escape(str: string): string;
|
|
118
|
+
|
|
86
119
|
caseDefault(expr: string): string;
|
|
87
120
|
|
|
88
121
|
array(v: any[]): string;
|
|
@@ -281,6 +314,7 @@ export interface ILanguageBuilder {
|
|
|
281
314
|
|
|
282
315
|
forEach(
|
|
283
316
|
arrayName: string,
|
|
317
|
+
elName: string,
|
|
284
318
|
itemName: string,
|
|
285
319
|
forBody: string
|
|
286
320
|
): string;
|
|
@@ -308,6 +342,11 @@ export interface ILanguageBuilder {
|
|
|
308
342
|
index: string | number
|
|
309
343
|
): string;
|
|
310
344
|
|
|
345
|
+
add(
|
|
346
|
+
expr1: string | number,
|
|
347
|
+
expr2: string | number,
|
|
348
|
+
): string;
|
|
349
|
+
|
|
311
350
|
arraySlice(
|
|
312
351
|
arrayName: string,
|
|
313
352
|
start: string | number,
|