@litert/typeguard 1.4.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.
@@ -1,239 +0,0 @@
1
- /**
2
- * Copyright 2023 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
- title: string;
23
-
24
- value: 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 defaultItems(
46
- items: Record<string, boolean>,
47
- defaultValue: boolean = false
48
- ): ITestItem[] {
49
-
50
- return [
51
- {
52
- 'title': 'true',
53
- 'value': true,
54
- 'expect': items['true'] === undefined ? defaultValue : items['true']
55
- },
56
- {
57
- 'title': 'false',
58
- 'value': false,
59
- 'expect': items['false'] === undefined ? defaultValue : items['false']
60
- },
61
- {
62
- 'title': 'undefined',
63
- 'value': undefined,
64
- 'expect': items['undefined'] === undefined ? defaultValue : items['undefined']
65
- },
66
- {
67
- 'title': 'null',
68
- 'value': null,
69
- 'expect': items['null'] === undefined ? defaultValue : items['null']
70
- },
71
- {
72
- 'title': 'empty array',
73
- 'value': [],
74
- 'expect': items['empty array'] === undefined ? defaultValue : items['empty array']
75
- },
76
- {
77
- 'title': 'string \'hello\'',
78
- 'value': 'hello',
79
- 'expect': items['string \'hello\''] === undefined ? defaultValue : items['string \'hello\'']
80
- },
81
- {
82
- 'title': 'empty string',
83
- 'value': '',
84
- 'expect': items['empty string'] === undefined ? defaultValue : items['empty string']
85
- },
86
- {
87
- 'title': 'object',
88
- 'value': {},
89
- 'expect': items['object'] === undefined ? defaultValue : items['object']
90
- },
91
- {
92
- 'title': 'number 0',
93
- 'value': 0,
94
- 'expect': items['number 0'] === undefined ? defaultValue : items['number 0']
95
- },
96
- {
97
- 'title': 'number 1',
98
- 'value': 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
- 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
-
129
- const compiler2 = TypeGuard.createInlineCompiler();
130
-
131
- compiler2.addPredefinedType<string>(
132
- 'ipv4_address',
133
- function(i): i is string {
134
- return typeof i === 'string'
135
- && /^[0-9]{1,3}(\.[0-9]{1,3}){3}$/.test(i)
136
- && i.split('.').map(x => parseInt(x, 10)).every(x => x >= 0 && x <= 255);
137
- }
138
- );
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
-
154
- export function assertItem(input: unknown, expect: boolean | 'throw'): ITestItem {
155
-
156
- return {
157
- title: JSON.stringify(input),
158
- value: input,
159
- expect
160
- };
161
- }
162
-
163
- export function addRule(rule: unknown, items: ITestItem[]): ITestRule {
164
-
165
- return {
166
- name: JSON.stringify(rule),
167
- rule,
168
- items
169
- };
170
- }
171
-
172
- export function createTestDefinition(suite: ITestSuite) {
173
-
174
- return function(): void {
175
-
176
- describe(suite.name, function() {
177
-
178
- for (const section of suite.sections) {
179
-
180
- describe(section.name, function() {
181
-
182
- if (
183
- section.items.length === 1 &&
184
- section.items[0].expect === 'throw'
185
- ) {
186
-
187
- it('Throws exception.', function() {
188
-
189
- assert.throws(() => {
190
-
191
- compiler.compile<any>({
192
- 'rule': section.rule,
193
- traceErrors: true,
194
- });
195
- });
196
-
197
- assert.throws(() => {
198
-
199
- compiler2.compile<any>({
200
- 'rule': section.rule,
201
- });
202
- });
203
- });
204
- return;
205
- }
206
-
207
- const checkWithTrace = compiler.compile<any>({
208
- 'rule': section.rule,
209
- traceErrors: true,
210
- });
211
-
212
- const checkWithoutTrace = compiler2.compile<any>({
213
- 'rule': section.rule,
214
- });
215
-
216
- for (const item of section.items.sort((a, b) => a.expect === b.expect ? 0 : (a.expect ? -1 : 1))) {
217
-
218
- it(`${
219
- item.expect ? 'PASSED' : 'REJECTED'
220
- } when input ${
221
- item.title
222
- }.`, function() {
223
-
224
- assert.equal(
225
- checkWithTrace(item.value),
226
- item.expect
227
- );
228
-
229
- assert.equal(
230
- checkWithoutTrace(item.value),
231
- item.expect
232
- );
233
- });
234
- }
235
- });
236
- }
237
- });
238
- };
239
- }