@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.
Files changed (76) hide show
  1. package/CHANGES.md +16 -0
  2. package/lib/BuiltInTypeCompiler.d.ts +4 -4
  3. package/lib/BuiltInTypeCompiler.d.ts.map +1 -1
  4. package/lib/BuiltInTypeCompiler.js +167 -166
  5. package/lib/BuiltInTypeCompiler.js.map +1 -1
  6. package/lib/BuiltInTypes.d.ts +1 -1
  7. package/lib/BuiltInTypes.js +37 -36
  8. package/lib/BuiltInTypes.js.map +1 -1
  9. package/lib/Common.d.ts +27 -10
  10. package/lib/Common.d.ts.map +1 -1
  11. package/lib/Common.js +1 -1
  12. package/lib/Compiler.d.ts +2 -2
  13. package/lib/Compiler.d.ts.map +1 -1
  14. package/lib/Compiler.js +159 -102
  15. package/lib/Compiler.js.map +1 -1
  16. package/lib/Context.d.ts +6 -5
  17. package/lib/Context.d.ts.map +1 -1
  18. package/lib/Context.js +11 -7
  19. package/lib/Context.js.map +1 -1
  20. package/lib/FilterCompiler.d.ts +5 -5
  21. package/lib/FilterCompiler.d.ts.map +1 -1
  22. package/lib/FilterCompiler.js +25 -24
  23. package/lib/FilterCompiler.js.map +1 -1
  24. package/lib/InlineCompiler.d.ts +12 -5
  25. package/lib/InlineCompiler.d.ts.map +1 -1
  26. package/lib/InlineCompiler.js +18 -6
  27. package/lib/InlineCompiler.js.map +1 -1
  28. package/lib/Internal.d.ts +6 -4
  29. package/lib/Internal.d.ts.map +1 -1
  30. package/lib/Internal.js +12 -10
  31. package/lib/Internal.js.map +1 -1
  32. package/lib/Modifiers.d.ts +1 -1
  33. package/lib/Modifiers.js +2 -1
  34. package/lib/Modifiers.js.map +1 -1
  35. package/lib/index.d.ts +5 -5
  36. package/lib/index.js +19 -7
  37. package/lib/index.js.map +1 -1
  38. package/lib/langs/JavaScript.d.ts +2 -2
  39. package/lib/langs/JavaScript.d.ts.map +1 -1
  40. package/lib/langs/JavaScript.js +55 -29
  41. package/lib/langs/JavaScript.js.map +1 -1
  42. package/package.json +17 -13
  43. package/src/examples/quick-start.ts +172 -0
  44. package/src/lib/BuiltInTypeCompiler.ts +171 -171
  45. package/src/lib/BuiltInTypes.ts +36 -36
  46. package/src/lib/Common.ts +49 -10
  47. package/src/lib/Compiler.ts +354 -247
  48. package/src/lib/Context.ts +11 -13
  49. package/src/lib/FilterCompiler.ts +84 -84
  50. package/src/lib/InlineCompiler.ts +41 -15
  51. package/src/lib/Internal.ts +17 -13
  52. package/src/lib/Modifiers.ts +2 -2
  53. package/src/lib/index.ts +5 -5
  54. package/src/lib/langs/JavaScript.ts +84 -31
  55. package/src/test/00-all.ts +10 -10
  56. package/src/test/01-elemental-types.ts +1111 -1110
  57. package/src/test/02-array-and-list.ts +75 -75
  58. package/src/test/03-tuple.ts +87 -87
  59. package/src/test/04-from-string.ts +849 -848
  60. package/src/test/05-string-asserts.ts +422 -422
  61. package/src/test/06-structure.ts +107 -107
  62. package/src/test/07-modifiers.ts +151 -42
  63. package/src/test/08-map-and-dict.ts +46 -46
  64. package/src/test/09-exceptions.ts +30 -9
  65. package/src/test/abstracts.ts +83 -45
  66. package/dist/typeguard.amd.d.ts +0 -588
  67. package/dist/typeguard.amd.d.ts.map +0 -1
  68. package/dist/typeguard.amd.js +0 -2069
  69. package/dist/typeguard.amd.js.map +0 -1
  70. package/dist/typeguard.system.d.ts +0 -588
  71. package/dist/typeguard.system.d.ts.map +0 -1
  72. package/dist/typeguard.system.js +0 -2185
  73. package/dist/typeguard.system.js.map +0 -1
  74. package/src/samples/quick-start.ts +0 -52
  75. package/tsconfig-amd.json +0 -72
  76. package/tsconfig-systemjs.json +0 -72
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2019 Angus.Fenying <fenying@litert.org>
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 = "string";
17
+ export const STRING = 'string';
18
18
 
19
- export const ASCII_STRING = "ascii_string";
19
+ export const ASCII_STRING = 'ascii_string';
20
20
 
21
- export const HEX_STRING = "hex_string";
21
+ export const HEX_STRING = 'hex_string';
22
22
 
23
- export const LATIN_STRING = "latin_string";
23
+ export const LATIN_STRING = 'latin_string';
24
24
 
25
- export const NUMERIC = "numeric";
25
+ export const NUMERIC = 'numeric';
26
26
 
27
- export const SAFE_INT = "safe_int";
27
+ export const SAFE_INT = 'safe_int';
28
28
 
29
- export const INT = "int";
29
+ export const INT = 'int';
30
30
 
31
- export const INT8 = "int8";
31
+ export const INT8 = 'int8';
32
32
 
33
- export const INT16 = "int16";
33
+ export const INT16 = 'int16';
34
34
 
35
- export const INT32 = "int32";
35
+ export const INT32 = 'int32';
36
36
 
37
- export const INT64 = "int64";
37
+ export const INT64 = 'int64';
38
38
 
39
- export const SAFE_UINT = "safe_uint";
39
+ export const SAFE_UINT = 'safe_uint';
40
40
 
41
- export const UINT = "uint";
41
+ export const UINT = 'uint';
42
42
 
43
- export const UINT8 = "uint8";
43
+ export const UINT8 = 'uint8';
44
44
 
45
- export const UINT16 = "uint16";
45
+ export const UINT16 = 'uint16';
46
46
 
47
- export const UINT32 = "uint32";
47
+ export const UINT32 = 'uint32';
48
48
 
49
- export const UINT64 = "uint64";
49
+ export const UINT64 = 'uint64';
50
50
 
51
- export const BOOLEAN = "boolean";
51
+ export const BOOLEAN = 'boolean';
52
52
 
53
- export const ARRAY = "array";
53
+ export const ARRAY = 'array';
54
54
 
55
- export const ANY = "any";
55
+ export const ANY = 'any';
56
56
 
57
- export const STRUCT = "struct";
57
+ export const STRUCT = 'struct';
58
58
 
59
- export const NULL = "null";
59
+ export const NULL = 'null';
60
60
 
61
- export const UNDEFINED = "undefined";
61
+ export const UNDEFINED = 'undefined';
62
62
 
63
- export const VOID = "void";
63
+ export const VOID = 'void';
64
64
 
65
- export const FALSE = "false";
65
+ export const FALSE = 'false';
66
66
 
67
- export const TRUE = "true";
67
+ export const TRUE = 'true';
68
68
 
69
- export const FALSE_VALUE = "false_value";
69
+ export const FALSE_VALUE = 'false_value';
70
70
 
71
- export const TRUE_VALUE = "true_value";
71
+ export const TRUE_VALUE = 'true_value';
72
72
 
73
- export const OPTIONAL = "optional";
73
+ export const OPTIONAL = 'optional';
74
74
 
75
- export const REQUIRED = "required";
75
+ export const REQUIRED = 'required';
76
76
 
77
- export const DECIMAL = "decimal";
77
+ export const DECIMAL = 'decimal';
78
78
 
79
- export const NUMBER = "number";
79
+ export const NUMBER = 'number';
80
80
 
81
- export const FLOAT = "float";
81
+ export const FLOAT = 'float';
82
82
 
83
- export const UDECIMAL = "udecimal";
83
+ export const UDECIMAL = 'udecimal';
84
84
 
85
- export const UFLOAT = "ufloat";
85
+ export const UFLOAT = 'ufloat';
package/src/lib/Common.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright 2019 Angus.Fenying <fenying@litert.org>
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 TypeChecker<T> = (v: unknown) => v is T;
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
- "name": string;
29
+ 'name': string;
25
30
 
26
31
  /**
27
32
  * The type of argument.
28
33
  */
29
- "type": string;
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
- "arguments": ICompileOutputArgument[];
44
+ 'arguments': ICompileOutputArgument[];
38
45
 
39
46
  /**
40
47
  * The variable name of pre-defined type checkers.
41
48
  */
42
- "typeSlotName": string;
49
+ 'typeSlotName': string;
43
50
 
44
51
  /**
45
52
  * The source code of checker.
46
53
  */
47
- "source": string;
54
+ 'source': string;
48
55
 
49
56
  /**
50
57
  * The predefined types used by this type.
51
58
  */
52
- "referredTypes": string[];
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
- "rule": any;
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
- "name"?: string;
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,