@litert/typeguard 1.3.0-dev.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.
Files changed (61) hide show
  1. package/CHANGES.md +7 -1
  2. package/lib/BuiltInTypeCompiler.d.ts +1 -1
  3. package/lib/BuiltInTypeCompiler.js +4 -4
  4. package/lib/BuiltInTypeCompiler.js.map +1 -1
  5. package/lib/BuiltInTypes.d.ts +1 -1
  6. package/lib/BuiltInTypes.js +1 -1
  7. package/lib/Common.d.ts +6 -5
  8. package/lib/Common.d.ts.map +1 -1
  9. package/lib/Common.js +1 -1
  10. package/lib/Compiler.d.ts +1 -1
  11. package/lib/Compiler.d.ts.map +1 -1
  12. package/lib/Compiler.js +57 -35
  13. package/lib/Compiler.js.map +1 -1
  14. package/lib/Context.d.ts +2 -2
  15. package/lib/Context.d.ts.map +1 -1
  16. package/lib/Context.js +2 -2
  17. package/lib/Context.js.map +1 -1
  18. package/lib/FilterCompiler.d.ts +3 -3
  19. package/lib/FilterCompiler.d.ts.map +1 -1
  20. package/lib/FilterCompiler.js +4 -4
  21. package/lib/FilterCompiler.js.map +1 -1
  22. package/lib/InlineCompiler.d.ts +2 -2
  23. package/lib/InlineCompiler.d.ts.map +1 -1
  24. package/lib/InlineCompiler.js +5 -7
  25. package/lib/InlineCompiler.js.map +1 -1
  26. package/lib/Internal.d.ts +9 -4
  27. package/lib/Internal.d.ts.map +1 -1
  28. package/lib/Internal.js +45 -4
  29. package/lib/Internal.js.map +1 -1
  30. package/lib/Modifiers.d.ts +2 -1
  31. package/lib/Modifiers.d.ts.map +1 -1
  32. package/lib/Modifiers.js +3 -2
  33. package/lib/Modifiers.js.map +1 -1
  34. package/lib/index.d.ts +1 -1
  35. package/lib/index.js +1 -1
  36. package/lib/langs/JavaScript.d.ts +1 -1
  37. package/lib/langs/JavaScript.js +2 -2
  38. package/package.json +11 -13
  39. package/src/examples/quick-start.ts +5 -1
  40. package/src/lib/BuiltInTypeCompiler.ts +4 -4
  41. package/src/lib/BuiltInTypes.ts +1 -1
  42. package/src/lib/Common.ts +5 -4
  43. package/src/lib/Compiler.ts +66 -39
  44. package/src/lib/Context.ts +2 -2
  45. package/src/lib/FilterCompiler.ts +3 -3
  46. package/src/lib/InlineCompiler.ts +6 -9
  47. package/src/lib/Internal.ts +73 -4
  48. package/src/lib/Modifiers.ts +3 -1
  49. package/src/lib/index.ts +1 -1
  50. package/src/lib/langs/JavaScript.ts +2 -2
  51. package/src/test/00-all.ts +1 -1
  52. package/src/test/01-elemental-types.ts +580 -580
  53. package/src/test/02-array-and-list.ts +43 -43
  54. package/src/test/03-tuple.ts +61 -61
  55. package/src/test/04-from-string.ts +501 -501
  56. package/src/test/05-string-asserts.ts +203 -203
  57. package/src/test/06-structure.ts +71 -71
  58. package/src/test/07-modifiers.ts +297 -48
  59. package/src/test/08-map-and-dict.ts +11 -11
  60. package/src/test/09-exceptions.ts +11 -11
  61. package/src/test/abstracts.ts +57 -29
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2022 Angus Fenying <fenying@litert.org>
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
- inputName: string;
22
+ title: string;
23
23
 
24
- inputValue: any;
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 defaultItemss(
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
- 'inputName': 'true',
53
- 'inputValue': true,
52
+ 'title': 'true',
53
+ 'value': true,
54
54
  'expect': items['true'] === undefined ? defaultValue : items['true']
55
55
  },
56
56
  {
57
- 'inputName': 'false',
58
- 'inputValue': false,
57
+ 'title': 'false',
58
+ 'value': false,
59
59
  'expect': items['false'] === undefined ? defaultValue : items['false']
60
60
  },
61
61
  {
62
- 'inputName': 'undefined',
63
- 'inputValue': undefined,
62
+ 'title': 'undefined',
63
+ 'value': undefined,
64
64
  'expect': items['undefined'] === undefined ? defaultValue : items['undefined']
65
65
  },
66
66
  {
67
- 'inputName': 'null',
68
- 'inputValue': null,
67
+ 'title': 'null',
68
+ 'value': null,
69
69
  'expect': items['null'] === undefined ? defaultValue : items['null']
70
70
  },
71
71
  {
72
- 'inputName': 'empty array',
73
- 'inputValue': [],
72
+ 'title': 'empty array',
73
+ 'value': [],
74
74
  'expect': items['empty array'] === undefined ? defaultValue : items['empty array']
75
75
  },
76
76
  {
77
- 'inputName': 'string \'hello\'',
78
- 'inputValue': 'hello',
77
+ 'title': 'string \'hello\'',
78
+ 'value': 'hello',
79
79
  'expect': items['string \'hello\''] === undefined ? defaultValue : items['string \'hello\'']
80
80
  },
81
81
  {
82
- 'inputName': 'empty string',
83
- 'inputValue': '',
82
+ 'title': 'empty string',
83
+ 'value': '',
84
84
  'expect': items['empty string'] === undefined ? defaultValue : items['empty string']
85
85
  },
86
86
  {
87
- 'inputName': 'object',
88
- 'inputValue': {},
87
+ 'title': 'object',
88
+ 'value': {},
89
89
  'expect': items['object'] === undefined ? defaultValue : items['object']
90
90
  },
91
91
  {
92
- 'inputName': 'number 0',
93
- 'inputValue': 0,
92
+ 'title': 'number 0',
93
+ 'value': 0,
94
94
  'expect': items['number 0'] === undefined ? defaultValue : items['number 0']
95
95
  },
96
96
  {
97
- 'inputName': 'number 1',
98
- 'inputValue': 1,
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
- inputName: JSON.stringify(input),
130
- inputValue: input,
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.inputName
221
+ item.title
194
222
  }.`, function() {
195
223
 
196
224
  assert.equal(
197
- checkWithTrace(item.inputValue),
225
+ checkWithTrace(item.value),
198
226
  item.expect
199
227
  );
200
228
 
201
229
  assert.equal(
202
- checkWithoutTrace(item.inputValue),
230
+ checkWithoutTrace(item.value),
203
231
  item.expect
204
232
  );
205
233
  });