@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,19 +14,19 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- import * as C from "../Common";
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(" ")} { ${expr} break; }`;
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("true")) {
108
+ if (conditions.includes('true')) {
109
109
 
110
- return "true";
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 "true";
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("false")) {
130
+ if (conditions.includes('false')) {
131
131
 
132
- return "false";
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 "true";
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
- let m = regExp.match(/^\/(.+)\/([a-z]*)$/i);
195
+ const m = /^\/(.+)\/([a-z]*)$/i.exec(regExp);
196
196
 
197
197
  if (m) {
198
198
 
199
- return `/${m[1]}/${m[2] || ""}.test(${expr})`;
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, "^[+-]?\\d+(\\.\\d+)?$")
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, "^[+-]?\\d+(\\.\\d+)?$"))
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
- an: string,
293
- it: string,
292
+ arrVar: string,
293
+ elVar: string,
294
+ iterVar: string,
294
295
  body: string
295
296
  ): string {
296
297
 
297
- return `for (const ${it} of ${an}) {
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(";") ? s : `${s};`).join("");
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 `false`;
384
+ return 'false';
383
385
  }
384
386
 
385
387
  public get literalTrue(): string {
386
388
 
387
- return `true`;
389
+ return 'true';
388
390
  }
389
391
 
390
392
  public get maxSafeInteger(): string {
391
393
 
392
- return "0X1FFFFFFFFFFFFF";
394
+ return '0X1FFFFFFFFFFFFF';
393
395
  }
394
396
 
395
397
  public get minSafeInteger(): string {
396
398
 
397
- return "-0X1FFFFFFFFFFFFF";
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
  /**
@@ -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,15 +14,15 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
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";
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();