@sinclair/typebox 0.25.21 → 0.25.22

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.
@@ -14,8 +14,8 @@ export declare class TypeCheck<T extends Types.TSchema> {
14
14
  /** Returns true if the value matches the compiled type. */
15
15
  Check(value: unknown): value is Types.Static<T>;
16
16
  }
17
- export declare namespace Property {
18
- function Check(propertyName: string): boolean;
17
+ export declare namespace MemberExpression {
18
+ function Encode(object: string, key: string): string;
19
19
  }
20
20
  export declare class TypeCompilerUnknownTypeError extends Error {
21
21
  readonly schema: Types.TSchema;
@@ -27,7 +27,7 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.TypeCompiler = exports.TypeCompilerUnknownTypeError = exports.Property = exports.TypeCheck = void 0;
30
+ exports.TypeCompiler = exports.TypeCompilerUnknownTypeError = exports.MemberExpression = exports.TypeCheck = void 0;
31
31
  const index_1 = require("../errors/index");
32
32
  const index_2 = require("../system/index");
33
33
  const index_3 = require("../guard/index");
@@ -60,41 +60,74 @@ class TypeCheck {
60
60
  }
61
61
  exports.TypeCheck = TypeCheck;
62
62
  // -------------------------------------------------------------------
63
- // Property
63
+ // Character
64
64
  // -------------------------------------------------------------------
65
- var Property;
66
- (function (Property) {
65
+ var Character;
66
+ (function (Character) {
67
67
  function DollarSign(code) {
68
68
  return code === 36;
69
69
  }
70
+ Character.DollarSign = DollarSign;
70
71
  function Underscore(code) {
71
72
  return code === 95;
72
73
  }
74
+ Character.Underscore = Underscore;
73
75
  function Numeric(code) {
74
76
  return code >= 48 && code <= 57;
75
77
  }
78
+ Character.Numeric = Numeric;
76
79
  function Alpha(code) {
77
80
  return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
78
81
  }
82
+ Character.Alpha = Alpha;
83
+ })(Character || (Character = {}));
84
+ // -------------------------------------------------------------------
85
+ // Identifier
86
+ // -------------------------------------------------------------------
87
+ var Identifier;
88
+ (function (Identifier) {
89
+ function Encode($id) {
90
+ const buffer = [];
91
+ for (let i = 0; i < $id.length; i++) {
92
+ const code = $id.charCodeAt(i);
93
+ if (Character.Numeric(code) || Character.Alpha(code)) {
94
+ buffer.push($id.charAt(i));
95
+ }
96
+ else {
97
+ buffer.push(`_${code}_`);
98
+ }
99
+ }
100
+ return buffer.join('').replace(/__/g, '_');
101
+ }
102
+ Identifier.Encode = Encode;
103
+ })(Identifier || (Identifier = {}));
104
+ // -------------------------------------------------------------------
105
+ // MemberExpression
106
+ // -------------------------------------------------------------------
107
+ var MemberExpression;
108
+ (function (MemberExpression) {
79
109
  function Check(propertyName) {
80
110
  if (propertyName.length === 0)
81
111
  return false;
82
112
  {
83
113
  const code = propertyName.charCodeAt(0);
84
- if (!(DollarSign(code) || Underscore(code) || Alpha(code))) {
114
+ if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code))) {
85
115
  return false;
86
116
  }
87
117
  }
88
118
  for (let i = 1; i < propertyName.length; i++) {
89
119
  const code = propertyName.charCodeAt(i);
90
- if (!(DollarSign(code) || Underscore(code) || Alpha(code) || Numeric(code))) {
120
+ if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code) || Character.Numeric(code))) {
91
121
  return false;
92
122
  }
93
123
  }
94
124
  return true;
95
125
  }
96
- Property.Check = Check;
97
- })(Property = exports.Property || (exports.Property = {}));
126
+ function Encode(object, key) {
127
+ return !Check(key) ? `${object}['${key}']` : `${object}.${key}`;
128
+ }
129
+ MemberExpression.Encode = Encode;
130
+ })(MemberExpression = exports.MemberExpression || (exports.MemberExpression = {}));
98
131
  // -------------------------------------------------------------------
99
132
  // TypeCompiler
100
133
  // -------------------------------------------------------------------
@@ -108,6 +141,9 @@ exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError;
108
141
  /** Compiles Types for Runtime Type Checking */
109
142
  var TypeCompiler;
110
143
  (function (TypeCompiler) {
144
+ // -------------------------------------------------------------------
145
+ // Guards
146
+ // -------------------------------------------------------------------
111
147
  function IsNumber(value) {
112
148
  return typeof value === 'number' && !globalThis.isNaN(value);
113
149
  }
@@ -223,7 +259,7 @@ var TypeCompiler;
223
259
  yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
224
260
  }
225
261
  for (const propertyKey of propertyKeys) {
226
- const memberExpression = Property.Check(propertyKey) ? `${value}.${propertyKey}` : `${value}['${propertyKey}']`;
262
+ const memberExpression = MemberExpression.Encode(value, propertyKey);
227
263
  const propertySchema = schema.properties[propertyKey];
228
264
  if (schema.required && schema.required.includes(propertyKey)) {
229
265
  yield* Visit(propertySchema, memberExpression);
@@ -406,7 +442,7 @@ var TypeCompiler;
406
442
  return `(${[...Visit(schema, value)].join(' && ')})`;
407
443
  }
408
444
  function CreateFunctionName($id) {
409
- return `check_${$id.replace(/-/g, '_')}`;
445
+ return `check_${Identifier.Encode($id)}`;
410
446
  }
411
447
  function CreateFunction(name, schema, value) {
412
448
  const expression = [...Visit(schema, value)].map((condition) => ` ${condition}`).join(' &&\n');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.25.21",
3
+ "version": "0.25.22",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -1233,29 +1233,29 @@ This benchmark measures compilation performance for varying types. You can revie
1233
1233
  ┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
1234
1234
  │ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
1235
1235
  ├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
1236
- │ Number │ 2000 │ ' 437 ms' │ ' 15 ms' │ ' 29.13 x' │
1237
- │ String │ 2000 │ ' 332 ms' │ ' 13 ms' │ ' 25.54 x' │
1238
- │ Boolean │ 2000 │ ' 293 ms' │ ' 12 ms' │ ' 24.42 x' │
1239
- │ Null │ 2000 │ ' 259 ms' │ ' 9 ms' │ ' 28.78 x' │
1240
- │ RegEx │ 2000 │ ' 505 ms' │ ' 21 ms' │ ' 24.05 x' │
1241
- │ ObjectA │ 2000 │ ' 2726 ms' │ ' 53 ms' │ ' 51.43 x' │
1242
- │ ObjectB │ 2000 │ ' 2835 ms' │ ' 37 ms' │ ' 76.62 x' │
1243
- │ Tuple │ 2000 │ ' 1239 ms' │ ' 24 ms' │ ' 51.63 x' │
1244
- │ Union │ 2000 │ ' 1244 ms' │ ' 34 ms' │ ' 36.59 x' │
1245
- │ Vector4 │ 2000 │ ' 1521 ms' │ ' 21 ms' │ ' 72.43 x' │
1246
- │ Matrix4 │ 2000 │ ' 836 ms' │ ' 10 ms' │ ' 83.60 x' │
1247
- │ Literal_String │ 2000 │ ' 333 ms' │ ' 9 ms' │ ' 37.00 x' │
1248
- │ Literal_Number │ 2000 │ ' 357 ms' │ ' 9 ms' │ ' 39.67 x' │
1249
- │ Literal_Boolean │ 2000 │ ' 352 ms' │ ' 6 ms' │ ' 58.67 x' │
1250
- │ Array_Number │ 2000 │ ' 685 ms' │ ' 12 ms' │ ' 57.08 x' │
1251
- │ Array_String │ 2000 │ ' 720 ms' │ ' 12 ms' │ ' 60.00 x' │
1252
- │ Array_Boolean │ 2000 │ ' 709 ms' │ ' 8 ms' │ ' 88.63 x' │
1253
- │ Array_ObjectA │ 2000 │ ' 3590 ms' │ ' 40 ms' │ ' 89.75 x' │
1254
- │ Array_ObjectB │ 2000 │ ' 3610 ms' │ ' 51 ms' │ ' 70.78 x' │
1255
- │ Array_Tuple │ 2000 │ ' 2124 ms' │ ' 19 ms' │ ' 111.79 x' │
1256
- │ Array_Union │ 2000 │ ' 1533 ms' │ ' 21 ms' │ ' 73.00 x' │
1257
- │ Array_Vector4 │ 2000 │ ' 2278 ms' │ ' 19 ms' │ ' 119.89 x' │
1258
- │ Array_Matrix4 │ 2000 │ ' 1504 ms' │ ' 20 ms' │ ' 75.20 x' │
1236
+ │ Number │ 2000 │ ' 418 ms' │ ' 14 ms' │ ' 29.86 x' │
1237
+ │ String │ 2000 │ ' 331 ms' │ ' 12 ms' │ ' 27.58 x' │
1238
+ │ Boolean │ 2000 │ ' 290 ms' │ ' 13 ms' │ ' 22.31 x' │
1239
+ │ Null │ 2000 │ ' 253 ms' │ ' 8 ms' │ ' 31.63 x' │
1240
+ │ RegEx │ 2000 │ ' 481 ms' │ ' 18 ms' │ ' 26.72 x' │
1241
+ │ ObjectA │ 2000 │ ' 2675 ms' │ ' 54 ms' │ ' 49.54 x' │
1242
+ │ ObjectB │ 2000 │ ' 2849 ms' │ ' 39 ms' │ ' 73.05 x' │
1243
+ │ Tuple │ 2000 │ ' 1224 ms' │ ' 22 ms' │ ' 55.64 x' │
1244
+ │ Union │ 2000 │ ' 1225 ms' │ ' 26 ms' │ ' 47.12 x' │
1245
+ │ Vector4 │ 2000 │ ' 1777 ms' │ ' 24 ms' │ ' 74.04 x' │
1246
+ │ Matrix4 │ 2000 │ ' 825 ms' │ ' 12 ms' │ ' 68.75 x' │
1247
+ │ Literal_String │ 2000 │ ' 345 ms' │ ' 9 ms' │ ' 38.33 x' │
1248
+ │ Literal_Number │ 2000 │ ' 363 ms' │ ' 7 ms' │ ' 51.86 x' │
1249
+ │ Literal_Boolean │ 2000 │ ' 358 ms' │ ' 6 ms' │ ' 59.67 x' │
1250
+ │ Array_Number │ 2000 │ ' 687 ms' │ ' 8 ms' │ ' 85.88 x' │
1251
+ │ Array_String │ 2000 │ ' 726 ms' │ ' 8 ms' │ ' 90.75 x' │
1252
+ │ Array_Boolean │ 2000 │ ' 703 ms' │ ' 8 ms' │ ' 87.88 x' │
1253
+ │ Array_ObjectA │ 2000 │ ' 3686 ms' │ ' 40 ms' │ ' 92.15 x' │
1254
+ │ Array_ObjectB │ 2000 │ ' 3821 ms' │ ' 40 ms' │ ' 95.53 x' │
1255
+ │ Array_Tuple │ 2000 │ ' 2070 ms' │ ' 17 ms' │ ' 121.76 x' │
1256
+ │ Array_Union │ 2000 │ ' 1503 ms' │ ' 21 ms' │ ' 71.57 x' │
1257
+ │ Array_Vector4 │ 2000 │ ' 2185 ms' │ ' 21 ms' │ ' 104.05 x' │
1258
+ │ Array_Matrix4 │ 2000 │ ' 1502 ms' │ ' 16 ms' │ ' 93.88 x' │
1259
1259
  └──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
1260
1260
  ```
1261
1261
 
@@ -1269,31 +1269,31 @@ This benchmark measures validation performance for varying types. You can review
1269
1269
  ┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
1270
1270
  │ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
1271
1271
  ├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
1272
- │ Number │ 1000000 │ ' 29 ms' │ ' 6 ms' │ ' 6 ms' │ ' 1.00 x' │
1273
- │ String │ 1000000 │ ' 24 ms' │ ' 19 ms' │ ' 11 ms' │ ' 1.73 x' │
1274
- │ Boolean │ 1000000 │ ' 22 ms' │ ' 20 ms' │ ' 11 ms' │ ' 1.82 x' │
1275
- │ Null │ 1000000 │ ' 27 ms' │ ' 23 ms' │ ' 11 ms' │ ' 2.09 x' │
1276
- │ RegEx │ 1000000 │ ' 160 ms' │ ' 52 ms' │ ' 40 ms' │ ' 1.30 x' │
1277
- │ ObjectA │ 1000000 │ ' 577 ms' │ ' 37 ms' │ ' 26 ms' │ ' 1.42 x' │
1278
- │ ObjectB │ 1000000 │ ' 990 ms' │ ' 50 ms' │ ' 37 ms' │ ' 1.35 x' │
1279
- │ Tuple │ 1000000 │ ' 124 ms' │ ' 22 ms' │ ' 17 ms' │ ' 1.29 x' │
1280
- │ Union │ 1000000 │ ' 318 ms' │ ' 24 ms' │ ' 16 ms' │ ' 1.50 x' │
1281
- │ Recursive │ 1000000 │ ' 3102 ms' │ ' 408 ms' │ ' 102 ms' │ ' 4.00 x' │
1282
- │ Vector4 │ 1000000 │ ' 150 ms' │ ' 23 ms' │ ' 13 ms' │ ' 1.77 x' │
1283
- │ Matrix4 │ 1000000 │ ' 579 ms' │ ' 43 ms' │ ' 27 ms' │ ' 1.59 x' │
1284
- │ Literal_String │ 1000000 │ ' 50 ms' │ ' 22 ms' │ ' 10 ms' │ ' 2.20 x' │
1285
- │ Literal_Number │ 1000000 │ ' 47 ms' │ ' 21 ms' │ ' 11 ms' │ ' 1.91 x' │
1286
- │ Literal_Boolean │ 1000000 │ ' 48 ms' │ ' 20 ms' │ ' 10 ms' │ ' 2.00 x' │
1287
- │ Array_Number │ 1000000 │ ' 429 ms' │ ' 32 ms' │ ' 20 ms' │ ' 1.60 x' │
1288
- │ Array_String │ 1000000 │ ' 474 ms' │ ' 33 ms' │ ' 25 ms' │ ' 1.32 x' │
1289
- │ Array_Boolean │ 1000000 │ ' 447 ms' │ ' 35 ms' │ ' 31 ms' │ ' 1.13 x' │
1290
- │ Array_ObjectA │ 1000000 │ ' 14098 ms' │ ' 2352 ms' │ ' 2015 ms' │ ' 1.17 x' │
1291
- │ Array_ObjectB │ 1000000 │ ' 16216 ms' │ ' 2628 ms' │ ' 2511 ms' │ ' 1.05 x' │
1292
- │ Array_Tuple │ 1000000 │ ' 1749 ms' │ ' 95 ms' │ ' 75 ms' │ ' 1.27 x' │
1293
- │ Array_Union │ 1000000 │ ' 4712 ms' │ ' 233 ms' │ ' 84 ms' │ ' 2.77 x' │
1294
- │ Array_Recursive │ 1000000 │ ' 56118 ms' │ ' 7192 ms' │ ' 1180 ms' │ ' 6.09 x' │
1295
- │ Array_Vector4 │ 1000000 │ ' 2284 ms' │ ' 99 ms' │ ' 50 ms' │ ' 1.98 x' │
1296
- │ Array_Matrix4 │ 1000000 │ ' 12640 ms' │ ' 412 ms' │ ' 272 ms' │ ' 1.51 x' │
1272
+ │ Number │ 1000000 │ ' 28 ms' │ ' 6 ms' │ ' 6 ms' │ ' 1.00 x' │
1273
+ │ String │ 1000000 │ ' 25 ms' │ ' 23 ms' │ ' 11 ms' │ ' 2.09 x' │
1274
+ │ Boolean │ 1000000 │ ' 24 ms' │ ' 21 ms' │ ' 11 ms' │ ' 1.91 x' │
1275
+ │ Null │ 1000000 │ ' 25 ms' │ ' 22 ms' │ ' 10 ms' │ ' 2.20 x' │
1276
+ │ RegEx │ 1000000 │ ' 164 ms' │ ' 53 ms' │ ' 37 ms' │ ' 1.43 x' │
1277
+ │ ObjectA │ 1000000 │ ' 593 ms' │ ' 47 ms' │ ' 25 ms' │ ' 1.88 x' │
1278
+ │ ObjectB │ 1000000 │ ' 1053 ms' │ ' 54 ms' │ ' 40 ms' │ ' 1.35 x' │
1279
+ │ Tuple │ 1000000 │ ' 129 ms' │ ' 25 ms' │ ' 16 ms' │ ' 1.56 x' │
1280
+ │ Union │ 1000000 │ ' 334 ms' │ ' 25 ms' │ ' 16 ms' │ ' 1.56 x' │
1281
+ │ Recursive │ 1000000 │ ' 3127 ms' │ ' 424 ms' │ ' 98 ms' │ ' 4.33 x' │
1282
+ │ Vector4 │ 1000000 │ ' 152 ms' │ ' 24 ms' │ ' 12 ms' │ ' 2.00 x' │
1283
+ │ Matrix4 │ 1000000 │ ' 593 ms' │ ' 41 ms' │ ' 27 ms' │ ' 1.52 x' │
1284
+ │ Literal_String │ 1000000 │ ' 48 ms' │ ' 20 ms' │ ' 11 ms' │ ' 1.82 x' │
1285
+ │ Literal_Number │ 1000000 │ ' 47 ms' │ ' 22 ms' │ ' 10 ms' │ ' 2.20 x' │
1286
+ │ Literal_Boolean │ 1000000 │ ' 48 ms' │ ' 21 ms' │ ' 11 ms' │ ' 1.91 x' │
1287
+ │ Array_Number │ 1000000 │ ' 495 ms' │ ' 32 ms' │ ' 21 ms' │ ' 1.52 x' │
1288
+ │ Array_String │ 1000000 │ ' 481 ms' │ ' 31 ms' │ ' 21 ms' │ ' 1.48 x' │
1289
+ │ Array_Boolean │ 1000000 │ ' 446 ms' │ ' 32 ms' │ ' 27 ms' │ ' 1.19 x' │
1290
+ │ Array_ObjectA │ 1000000 │ ' 14314 ms' │ ' 2341 ms' │ ' 1969 ms' │ ' 1.19 x' │
1291
+ │ Array_ObjectB │ 1000000 │ ' 16883 ms' │ ' 2661 ms' │ ' 2606 ms' │ ' 1.02 x' │
1292
+ │ Array_Tuple │ 1000000 │ ' 1834 ms' │ ' 98 ms' │ ' 77 ms' │ ' 1.27 x' │
1293
+ │ Array_Union │ 1000000 │ ' 4960 ms' │ ' 240 ms' │ ' 87 ms' │ ' 2.76 x' │
1294
+ │ Array_Recursive │ 1000000 │ ' 56273 ms' │ ' 7118 ms' │ ' 1122 ms' │ ' 6.34 x' │
1295
+ │ Array_Vector4 │ 1000000 │ ' 2498 ms' │ ' 99 ms' │ ' 48 ms' │ ' 2.06 x' │
1296
+ │ Array_Matrix4 │ 1000000 │ ' 12487 ms' │ ' 383 ms' │ ' 246 ms' │ ' 1.56 x' │
1297
1297
  └──────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
1298
1298
  ```
1299
1299
 
@@ -1307,13 +1307,13 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
1307
1307
  ┌──────────────────────┬────────────┬────────────┬─────────────┐
1308
1308
  │ (index) │ Compiled │ Minified │ Compression │
1309
1309
  ├──────────────────────┼────────────┼────────────┼─────────────┤
1310
- │ typebox/compiler │ ' 61 kb' │ ' 30 kb' │ '2.03 x' │
1310
+ │ typebox/compiler │ ' 64 kb' │ ' 31 kb' │ '2.02 x' │
1311
1311
  │ typebox/conditional │ ' 45 kb' │ ' 18 kb' │ '2.44 x' │
1312
1312
  │ typebox/custom │ ' 0 kb' │ ' 0 kb' │ '2.61 x' │
1313
1313
  │ typebox/format │ ' 0 kb' │ ' 0 kb' │ '2.66 x' │
1314
1314
  │ typebox/guard │ ' 23 kb' │ ' 11 kb' │ '2.07 x' │
1315
- │ typebox/hash │ ' 4 kb' │ ' 1 kb' │ '2.27 x' │
1316
- │ typebox/value │ ' 85 kb' │ ' 39 kb' │ '2.16 x' │
1315
+ │ typebox/hash │ ' 4 kb' │ ' 1 kb' │ '2.30 x' │
1316
+ │ typebox/value │ ' 89 kb' │ ' 41 kb' │ '2.15 x' │
1317
1317
  │ typebox │ ' 12 kb' │ ' 6 kb' │ '1.89 x' │
1318
1318
  └──────────────────────┴────────────┴────────────┴─────────────┘
1319
1319
  ```