@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
|
@@ -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,19 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import * as C from
|
|
17
|
+
import * as C from '../Common';
|
|
18
18
|
|
|
19
19
|
class JavaScriptLanguage
|
|
20
20
|
implements C.ILanguageBuilder {
|
|
21
21
|
|
|
22
22
|
public switchCase(expr: string, cases: string[]): string {
|
|
23
23
|
|
|
24
|
-
return `switch (${expr}) { ${cases.join(
|
|
24
|
+
return `switch (${expr}) { ${cases.join('')} }`;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
public caseIf(cond: string[], expr: string): string {
|
|
28
28
|
|
|
29
|
-
return `${cond.map((x) => `case ${x}:`).join(
|
|
29
|
+
return `${cond.map((x) => `case ${x}:`).join(' ')} { ${expr} break; }`;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
public caseDefault(expr: string): string {
|
|
@@ -61,7 +61,7 @@ implements C.ILanguageBuilder {
|
|
|
61
61
|
|
|
62
62
|
public call(fnName: string, ...args: string[]): string {
|
|
63
63
|
|
|
64
|
-
return `${fnName}(${args.join(
|
|
64
|
+
return `${fnName}(${args.join(',')})`;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
public startsWith(
|
|
@@ -105,19 +105,19 @@ implements C.ILanguageBuilder {
|
|
|
105
105
|
return conditions[0];
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
if (conditions.includes(
|
|
108
|
+
if (conditions.includes('true')) {
|
|
109
109
|
|
|
110
|
-
return
|
|
110
|
+
return 'true';
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
conditions = this._dereplicate(conditions);
|
|
113
|
+
conditions = this._dereplicate(conditions.filter((x) => x !== 'false'));
|
|
114
114
|
|
|
115
115
|
if (!conditions.length) {
|
|
116
116
|
|
|
117
|
-
return
|
|
117
|
+
return 'true';
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
return `${conditions.map((x) => `(${x})`).join(
|
|
120
|
+
return `${conditions.map((x) => `(${x})`).join(' || ')}`;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
public and(conditions: string[]): string {
|
|
@@ -127,19 +127,19 @@ implements C.ILanguageBuilder {
|
|
|
127
127
|
return conditions[0];
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
if (conditions.includes(
|
|
130
|
+
if (conditions.includes('false')) {
|
|
131
131
|
|
|
132
|
-
return
|
|
132
|
+
return 'false';
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
conditions = this._dereplicate(conditions);
|
|
135
|
+
conditions = this._dereplicate(conditions.filter((x) => x !== 'true'));
|
|
136
136
|
|
|
137
137
|
if (!conditions.length) {
|
|
138
138
|
|
|
139
|
-
return
|
|
139
|
+
return 'true';
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
return `${conditions.map((x) => `(${x})`).join(
|
|
142
|
+
return `${conditions.map((x) => `(${x})`).join(' && ')}`;
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
public eq(a: string, b: string | number): string {
|
|
@@ -192,11 +192,11 @@ implements C.ILanguageBuilder {
|
|
|
192
192
|
regExp: string
|
|
193
193
|
): string {
|
|
194
194
|
|
|
195
|
-
|
|
195
|
+
const m = /^\/(.+)\/([a-z]*)$/i.exec(regExp);
|
|
196
196
|
|
|
197
197
|
if (m) {
|
|
198
198
|
|
|
199
|
-
return `/${m[1]}/${m[2] ||
|
|
199
|
+
return `/${m[1]}/${m[2] || ''}.test(${expr})`;
|
|
200
200
|
}
|
|
201
201
|
|
|
202
202
|
return `/${regExp}/.test(${expr})`;
|
|
@@ -214,12 +214,12 @@ implements C.ILanguageBuilder {
|
|
|
214
214
|
|
|
215
215
|
private _equal(positive: boolean = true): string {
|
|
216
216
|
|
|
217
|
-
return positive ?
|
|
217
|
+
return positive ? '===' : '!==';
|
|
218
218
|
}
|
|
219
219
|
|
|
220
220
|
private _not(positive: boolean = true): string {
|
|
221
221
|
|
|
222
|
-
return positive ?
|
|
222
|
+
return positive ? '' : '!';
|
|
223
223
|
}
|
|
224
224
|
|
|
225
225
|
public isString(vn: string, positive: boolean = true): string {
|
|
@@ -252,13 +252,13 @@ implements C.ILanguageBuilder {
|
|
|
252
252
|
this.isNumber(vn, true),
|
|
253
253
|
this.and([
|
|
254
254
|
this.isString(vn, true),
|
|
255
|
-
this.matchRegExp(vn,
|
|
255
|
+
this.matchRegExp(vn, '^[+-]?\\d+(\\.\\d+)?$')
|
|
256
256
|
])
|
|
257
257
|
]) : this.and([
|
|
258
258
|
this.isNumber(vn, false),
|
|
259
259
|
this.or([
|
|
260
260
|
this.isString(vn, false),
|
|
261
|
-
this.not(this.matchRegExp(vn,
|
|
261
|
+
this.not(this.matchRegExp(vn, '^[+-]?\\d+(\\.\\d+)?$'))
|
|
262
262
|
])
|
|
263
263
|
]);
|
|
264
264
|
}
|
|
@@ -289,19 +289,21 @@ implements C.ILanguageBuilder {
|
|
|
289
289
|
}
|
|
290
290
|
|
|
291
291
|
public forEach(
|
|
292
|
-
|
|
293
|
-
|
|
292
|
+
arrVar: string,
|
|
293
|
+
elVar: string,
|
|
294
|
+
iterVar: string,
|
|
294
295
|
body: string
|
|
295
296
|
): string {
|
|
296
297
|
|
|
297
|
-
return `for (
|
|
298
|
+
return `for (let ${elVar} = 0; ${elVar} < ${arrVar}.length; ${elVar}++) {
|
|
299
|
+
const ${iterVar} = ${arrVar}[${elVar}];
|
|
298
300
|
${body}
|
|
299
301
|
}`;
|
|
300
302
|
}
|
|
301
303
|
|
|
302
304
|
public series(statements: string[]): string {
|
|
303
305
|
|
|
304
|
-
return statements.map((s) => s.endsWith(
|
|
306
|
+
return statements.map((s) => s.endsWith(';') ? s : `${s};`).join('');
|
|
305
307
|
}
|
|
306
308
|
|
|
307
309
|
public ifThen(
|
|
@@ -379,22 +381,22 @@ implements C.ILanguageBuilder {
|
|
|
379
381
|
|
|
380
382
|
public get literalFalse(): string {
|
|
381
383
|
|
|
382
|
-
return
|
|
384
|
+
return 'false';
|
|
383
385
|
}
|
|
384
386
|
|
|
385
387
|
public get literalTrue(): string {
|
|
386
388
|
|
|
387
|
-
return
|
|
389
|
+
return 'true';
|
|
388
390
|
}
|
|
389
391
|
|
|
390
392
|
public get maxSafeInteger(): string {
|
|
391
393
|
|
|
392
|
-
return
|
|
394
|
+
return '0X1FFFFFFFFFFFFF';
|
|
393
395
|
}
|
|
394
396
|
|
|
395
397
|
public get minSafeInteger(): string {
|
|
396
398
|
|
|
397
|
-
return
|
|
399
|
+
return '-0X1FFFFFFFFFFFFF';
|
|
398
400
|
}
|
|
399
401
|
|
|
400
402
|
public isTrueValue(vn: string): string {
|
|
@@ -418,12 +420,63 @@ implements C.ILanguageBuilder {
|
|
|
418
420
|
body: string
|
|
419
421
|
): string {
|
|
420
422
|
|
|
421
|
-
return `(function(${params.join(
|
|
423
|
+
return `(function(${params.join(', ')}) {
|
|
422
424
|
${body}
|
|
423
425
|
})(${
|
|
424
|
-
args.join(
|
|
426
|
+
args.join(', ')
|
|
425
427
|
})`;
|
|
426
428
|
}
|
|
429
|
+
|
|
430
|
+
public escape(str: string): string {
|
|
431
|
+
|
|
432
|
+
return str.replace(/(['"])/g, '\\$1').replace(/\n/g, '\\n').replace(/\r/g, '\\r');
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
public stringTemplateVar(varExpr: string): string {
|
|
436
|
+
|
|
437
|
+
return `"\${${varExpr}.replace(/(['"])/g, '\\\\$1').replace(/\\n/g, '\\\\n').replace(/\\r/g, '\\\\r')}"`;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
public numberTemplateVar(varExpr: string): string {
|
|
441
|
+
|
|
442
|
+
return `\${${varExpr}}`;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
public addTrace(
|
|
446
|
+
vTrace: string,
|
|
447
|
+
vTraceStack: string,
|
|
448
|
+
path: string,
|
|
449
|
+
negative: boolean = false
|
|
450
|
+
): string {
|
|
451
|
+
|
|
452
|
+
if (negative) {
|
|
453
|
+
|
|
454
|
+
return `(${vTrace}.push(\`\${${vTraceStack}}${path}\`), true)`;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return `(${vTrace}.push(\`\${${vTraceStack}}${path}\`), false)`;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
public add(a: string | number, b: string | number): string {
|
|
461
|
+
|
|
462
|
+
return `${a} + ${b}`;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
public orAddTrace(
|
|
466
|
+
expr: string,
|
|
467
|
+
vTrace: string,
|
|
468
|
+
vTraceStack: string,
|
|
469
|
+
path: string,
|
|
470
|
+
negative: boolean = false
|
|
471
|
+
): string {
|
|
472
|
+
|
|
473
|
+
if (negative) {
|
|
474
|
+
|
|
475
|
+
return `((${expr}) && (${vTrace}.push(\`\${${vTraceStack}}${path}\`), true))`;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return `((${expr}) || (${vTrace}.push(\`\${${vTraceStack}}${path}\`), false))`;
|
|
479
|
+
}
|
|
427
480
|
}
|
|
428
481
|
|
|
429
482
|
/**
|
package/src/test/00-all.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,15 +14,15 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import testElementalTypes from
|
|
18
|
-
import testArrayAndList from
|
|
19
|
-
import testTuple from
|
|
20
|
-
import testFromString from
|
|
21
|
-
import testStringAssert from
|
|
22
|
-
import testStructure from
|
|
23
|
-
import testModifiers from
|
|
24
|
-
import testMapAndDict from
|
|
25
|
-
import testExceptions from
|
|
17
|
+
import testElementalTypes from './01-elemental-types';
|
|
18
|
+
import testArrayAndList from './02-array-and-list';
|
|
19
|
+
import testTuple from './03-tuple';
|
|
20
|
+
import testFromString from './04-from-string';
|
|
21
|
+
import testStringAssert from './05-string-asserts';
|
|
22
|
+
import testStructure from './06-structure';
|
|
23
|
+
import testModifiers from './07-modifiers';
|
|
24
|
+
import testMapAndDict from './08-map-and-dict';
|
|
25
|
+
import testExceptions from './09-exceptions';
|
|
26
26
|
|
|
27
27
|
testElementalTypes();
|
|
28
28
|
testArrayAndList();
|