@litert/typeguard 1.3.0 → 1.4.1

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 +11 -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 +58 -36
  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 +10 -12
  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 +67 -40
  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 +0 -35
  52. package/src/test/01-elemental-types.ts +0 -1963
  53. package/src/test/02-array-and-list.ts +0 -197
  54. package/src/test/03-tuple.ts +0 -220
  55. package/src/test/04-from-string.ts +0 -1488
  56. package/src/test/05-string-asserts.ts +0 -794
  57. package/src/test/06-structure.ts +0 -277
  58. package/src/test/07-modifiers.ts +0 -220
  59. package/src/test/08-map-and-dict.ts +0 -151
  60. package/src/test/09-exceptions.ts +0 -67
  61. package/src/test/abstracts.ts +0 -211
@@ -1,67 +0,0 @@
1
- /**
2
- * Copyright 2022 Angus Fenying <fenying@litert.org>
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * https://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- import {
18
- createTestDefinition,
19
- ITestSuite
20
- } from './abstracts';
21
-
22
- const testItems: ITestSuite = {
23
-
24
- name: 'When optional with any',
25
- sections: [
26
-
27
- {
28
- 'name': JSON.stringify({ 'test?': 'any' }),
29
- 'rule': { 'test?': 'any' },
30
- 'items': [
31
- {
32
- inputName: 'When test === \'ffff\'',
33
- inputValue: { 'test': 'ffff' },
34
- expect: true
35
- },
36
- {
37
- inputName: 'When test is omitted',
38
- inputValue: {},
39
- expect: true
40
- }
41
- ]
42
- },
43
- {
44
- 'name': JSON.stringify({ 'test': 'int8' }),
45
- 'rule': { 'test': 'int8' },
46
- 'items': [
47
- {
48
- inputName: 'When test === 123',
49
- inputValue: { 'test': 123 },
50
- expect: true
51
- },
52
- {
53
- inputName: 'When test === 1234',
54
- inputValue: { 'test': 1234 },
55
- expect: false
56
- },
57
- {
58
- inputName: 'When test is omitted',
59
- inputValue: {},
60
- expect: false
61
- }
62
- ]
63
- },
64
- ]
65
- };
66
-
67
- export default createTestDefinition(testItems);
@@ -1,211 +0,0 @@
1
- /**
2
- * Copyright 2022 Angus Fenying <fenying@litert.org>
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * https://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- import * as assert from 'assert';
18
- import * as TypeGuard from '../lib';
19
-
20
- export interface ITestItem {
21
-
22
- inputName: string;
23
-
24
- inputValue: any;
25
-
26
- expect: boolean | 'throw';
27
- }
28
-
29
- export interface ITestRule {
30
-
31
- name: string;
32
-
33
- rule: any;
34
-
35
- items: ITestItem[];
36
- }
37
-
38
- export interface ITestSuite {
39
-
40
- name: string;
41
-
42
- sections: ITestRule[];
43
- }
44
-
45
- export function defaultItemss(
46
- items: Record<string, boolean>,
47
- defaultValue: boolean = false
48
- ): ITestItem[] {
49
-
50
- return [
51
- {
52
- 'inputName': 'true',
53
- 'inputValue': true,
54
- 'expect': items['true'] === undefined ? defaultValue : items['true']
55
- },
56
- {
57
- 'inputName': 'false',
58
- 'inputValue': false,
59
- 'expect': items['false'] === undefined ? defaultValue : items['false']
60
- },
61
- {
62
- 'inputName': 'undefined',
63
- 'inputValue': undefined,
64
- 'expect': items['undefined'] === undefined ? defaultValue : items['undefined']
65
- },
66
- {
67
- 'inputName': 'null',
68
- 'inputValue': null,
69
- 'expect': items['null'] === undefined ? defaultValue : items['null']
70
- },
71
- {
72
- 'inputName': 'empty array',
73
- 'inputValue': [],
74
- 'expect': items['empty array'] === undefined ? defaultValue : items['empty array']
75
- },
76
- {
77
- 'inputName': 'string \'hello\'',
78
- 'inputValue': 'hello',
79
- 'expect': items['string \'hello\''] === undefined ? defaultValue : items['string \'hello\'']
80
- },
81
- {
82
- 'inputName': 'empty string',
83
- 'inputValue': '',
84
- 'expect': items['empty string'] === undefined ? defaultValue : items['empty string']
85
- },
86
- {
87
- 'inputName': 'object',
88
- 'inputValue': {},
89
- 'expect': items['object'] === undefined ? defaultValue : items['object']
90
- },
91
- {
92
- 'inputName': 'number 0',
93
- 'inputValue': 0,
94
- 'expect': items['number 0'] === undefined ? defaultValue : items['number 0']
95
- },
96
- {
97
- 'inputName': 'number 1',
98
- 'inputValue': 1,
99
- 'expect': items['number 1'] === undefined ? defaultValue : items['number 1']
100
- }
101
- ];
102
- }
103
-
104
- const compiler = TypeGuard.createInlineCompiler();
105
-
106
- compiler.addPredefinedType<string>(
107
- 'ipv4_address',
108
- function(i): i is string {
109
- return typeof i === 'string'
110
- && /^[0-9]{1,3}(\.[0-9]{1,3}){3}$/.test(i)
111
- && i.split('.').map(x => parseInt(x, 10)).every(x => x >= 0 && x <= 255);
112
- }
113
- );
114
-
115
- const compiler2 = TypeGuard.createInlineCompiler();
116
-
117
- compiler2.addPredefinedType<string>(
118
- 'ipv4_address',
119
- function(i): i is string {
120
- return typeof i === 'string'
121
- && /^[0-9]{1,3}(\.[0-9]{1,3}){3}$/.test(i)
122
- && i.split('.').map(x => parseInt(x, 10)).every(x => x >= 0 && x <= 255);
123
- }
124
- );
125
-
126
- export function assertItem(input: unknown, expect: boolean | 'throw'): ITestItem {
127
-
128
- return {
129
- inputName: JSON.stringify(input),
130
- inputValue: input,
131
- expect
132
- };
133
- }
134
-
135
- export function addRule(rule: unknown, items: ITestItem[]): ITestRule {
136
-
137
- return {
138
- name: JSON.stringify(rule),
139
- rule,
140
- items
141
- };
142
- }
143
-
144
- export function createTestDefinition(suite: ITestSuite) {
145
-
146
- return function(): void {
147
-
148
- describe(suite.name, function() {
149
-
150
- for (const section of suite.sections) {
151
-
152
- describe(section.name, function() {
153
-
154
- if (
155
- section.items.length === 1 &&
156
- section.items[0].expect === 'throw'
157
- ) {
158
-
159
- it('Throws exception.', function() {
160
-
161
- assert.throws(() => {
162
-
163
- compiler.compile<any>({
164
- 'rule': section.rule,
165
- traceErrors: true,
166
- });
167
- });
168
-
169
- assert.throws(() => {
170
-
171
- compiler2.compile<any>({
172
- 'rule': section.rule,
173
- });
174
- });
175
- });
176
- return;
177
- }
178
-
179
- const checkWithTrace = compiler.compile<any>({
180
- 'rule': section.rule,
181
- traceErrors: true,
182
- });
183
-
184
- const checkWithoutTrace = compiler2.compile<any>({
185
- 'rule': section.rule,
186
- });
187
-
188
- for (const item of section.items.sort((a, b) => a.expect === b.expect ? 0 : (a.expect ? -1 : 1))) {
189
-
190
- it(`${
191
- item.expect ? 'PASSED' : 'REJECTED'
192
- } when input ${
193
- item.inputName
194
- }.`, function() {
195
-
196
- assert.equal(
197
- checkWithTrace(item.inputValue),
198
- item.expect
199
- );
200
-
201
- assert.equal(
202
- checkWithoutTrace(item.inputValue),
203
- item.expect
204
- );
205
- });
206
- }
207
- });
208
- }
209
- });
210
- };
211
- }