@litert/typeguard 1.1.0 → 1.3.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 (63) hide show
  1. package/CHANGES.md +10 -0
  2. package/lib/BuiltInTypeCompiler.d.ts +2 -2
  3. package/lib/BuiltInTypeCompiler.d.ts.map +1 -1
  4. package/lib/BuiltInTypeCompiler.js +1 -1
  5. package/lib/BuiltInTypeCompiler.js.map +1 -1
  6. package/lib/BuiltInTypes.d.ts +1 -1
  7. package/lib/BuiltInTypes.js +1 -1
  8. package/lib/Common.d.ts +19 -2
  9. package/lib/Common.d.ts.map +1 -1
  10. package/lib/Common.js +1 -1
  11. package/lib/Compiler.d.ts +1 -1
  12. package/lib/Compiler.d.ts.map +1 -1
  13. package/lib/Compiler.js +93 -39
  14. package/lib/Compiler.js.map +1 -1
  15. package/lib/Context.d.ts +5 -4
  16. package/lib/Context.d.ts.map +1 -1
  17. package/lib/Context.js +9 -6
  18. package/lib/Context.js.map +1 -1
  19. package/lib/FilterCompiler.d.ts +3 -3
  20. package/lib/FilterCompiler.d.ts.map +1 -1
  21. package/lib/FilterCompiler.js +2 -2
  22. package/lib/FilterCompiler.js.map +1 -1
  23. package/lib/InlineCompiler.d.ts +10 -3
  24. package/lib/InlineCompiler.d.ts.map +1 -1
  25. package/lib/InlineCompiler.js +16 -5
  26. package/lib/InlineCompiler.js.map +1 -1
  27. package/lib/Internal.d.ts +6 -4
  28. package/lib/Internal.d.ts.map +1 -1
  29. package/lib/Internal.js +3 -2
  30. package/lib/Internal.js.map +1 -1
  31. package/lib/Modifiers.d.ts +1 -1
  32. package/lib/Modifiers.js +1 -1
  33. package/lib/index.d.ts +1 -1
  34. package/lib/index.js +6 -2
  35. package/lib/index.js.map +1 -1
  36. package/lib/langs/JavaScript.d.ts +1 -1
  37. package/lib/langs/JavaScript.d.ts.map +1 -1
  38. package/lib/langs/JavaScript.js +29 -4
  39. package/lib/langs/JavaScript.js.map +1 -1
  40. package/package.json +12 -12
  41. package/src/examples/quick-start.ts +118 -2
  42. package/src/lib/BuiltInTypeCompiler.ts +2 -2
  43. package/src/lib/BuiltInTypes.ts +1 -1
  44. package/src/lib/Common.ts +41 -2
  45. package/src/lib/Compiler.ts +155 -50
  46. package/src/lib/Context.ts +9 -11
  47. package/src/lib/FilterCompiler.ts +4 -4
  48. package/src/lib/InlineCompiler.ts +36 -10
  49. package/src/lib/Internal.ts +8 -4
  50. package/src/lib/Modifiers.ts +1 -1
  51. package/src/lib/index.ts +1 -1
  52. package/src/lib/langs/JavaScript.ts +58 -5
  53. package/src/test/00-all.ts +1 -1
  54. package/src/test/01-elemental-types.ts +2 -1
  55. package/src/test/02-array-and-list.ts +18 -18
  56. package/src/test/03-tuple.ts +1 -1
  57. package/src/test/04-from-string.ts +2 -1
  58. package/src/test/05-string-asserts.ts +1 -1
  59. package/src/test/06-structure.ts +26 -26
  60. package/src/test/07-modifiers.ts +71 -10
  61. package/src/test/08-map-and-dict.ts +20 -20
  62. package/src/test/09-exceptions.ts +4 -4
  63. package/src/test/abstracts.ts +45 -7
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2021 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.
@@ -30,7 +30,7 @@ const testItems: ITestSuite = {
30
30
  'items': [
31
31
  {
32
32
  inputName: 'When test === \'ffff\'',
33
- inputValue: {'test': 'ffff'},
33
+ inputValue: { 'test': 'ffff' },
34
34
  expect: true
35
35
  },
36
36
  {
@@ -46,12 +46,12 @@ const testItems: ITestSuite = {
46
46
  'items': [
47
47
  {
48
48
  inputName: 'When test === 123',
49
- inputValue: {'test': 123},
49
+ inputValue: { 'test': 123 },
50
50
  expect: true
51
51
  },
52
52
  {
53
53
  inputName: 'When test === 1234',
54
- inputValue: {'test': 1234},
54
+ inputValue: { 'test': 1234 },
55
55
  expect: false
56
56
  },
57
57
  {
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2021 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.
@@ -103,6 +103,26 @@ export function defaultItemss(
103
103
 
104
104
  const compiler = TypeGuard.createInlineCompiler();
105
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
+
106
126
  export function assertItem(input: unknown, expect: boolean | 'throw'): ITestItem {
107
127
 
108
128
  return {
@@ -127,7 +147,7 @@ export function createTestDefinition(suite: ITestSuite) {
127
147
 
128
148
  describe(suite.name, function() {
129
149
 
130
- for (let section of suite.sections) {
150
+ for (const section of suite.sections) {
131
151
 
132
152
  describe(section.name, function() {
133
153
 
@@ -141,18 +161,31 @@ export function createTestDefinition(suite: ITestSuite) {
141
161
  assert.throws(() => {
142
162
 
143
163
  compiler.compile<any>({
144
- 'rule': section.rule
164
+ 'rule': section.rule,
165
+ traceErrors: true,
166
+ });
167
+ });
168
+
169
+ assert.throws(() => {
170
+
171
+ compiler2.compile<any>({
172
+ 'rule': section.rule,
145
173
  });
146
174
  });
147
175
  });
148
176
  return;
149
177
  }
150
178
 
151
- const check = compiler.compile<any>({
152
- 'rule': section.rule
179
+ const checkWithTrace = compiler.compile<any>({
180
+ 'rule': section.rule,
181
+ traceErrors: true,
153
182
  });
154
183
 
155
- for (let item of section.items.sort((a, b) => a.expect === b.expect ? 0 : (a.expect ? -1 : 1))) {
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))) {
156
189
 
157
190
  it(`${
158
191
  item.expect ? 'PASSED' : 'REJECTED'
@@ -161,7 +194,12 @@ export function createTestDefinition(suite: ITestSuite) {
161
194
  }.`, function() {
162
195
 
163
196
  assert.equal(
164
- check(item.inputValue),
197
+ checkWithTrace(item.inputValue),
198
+ item.expect
199
+ );
200
+
201
+ assert.equal(
202
+ checkWithoutTrace(item.inputValue),
165
203
  item.expect
166
204
  );
167
205
  });