exact-mirror 0.2.0 → 0.2.2

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,4 +1,4 @@
1
- import { TypeCompiler } from '@sinclair/typebox/compiler';
1
+ import { TypeCompiler, type TypeCheck } from '@sinclair/typebox/compiler';
2
2
  import type { TAnySchema, TModule } from '@sinclair/typebox';
3
3
  export declare const mergeObjectIntersection: (schema: TAnySchema) => TAnySchema;
4
4
  type MaybeArray<T> = T | T[];
@@ -7,7 +7,7 @@ export interface Instruction {
7
7
  optionalsInArray: string[][];
8
8
  parentIsOptional: boolean;
9
9
  array: number;
10
- unions: string[][];
10
+ unions: TypeCheck<any>[][];
11
11
  unionKeys: Record<string, 1>;
12
12
  sanitize: MaybeArray<(v: string) => string> | undefined;
13
13
  /**
@@ -38,8 +38,5 @@ export interface Instruction {
38
38
  removeUnknownUnionType: boolean;
39
39
  }
40
40
  export declare function deepClone<T>(source: T, weak?: WeakMap<object, any>): T;
41
- export declare const createMirrorCode: (schema: TAnySchema, { TypeCompiler, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType, name }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType"> & {
42
- name?: string;
43
- }>) => string | [string, ((v: string) => string)[]];
44
41
  export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType">>) => ((v: T["static"]) => T["static"]);
45
42
  export default createMirror;
package/dist/cjs/index.js CHANGED
@@ -21,7 +21,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  createMirror: () => createMirror,
24
- createMirrorCode: () => createMirrorCode,
25
24
  deepClone: () => deepClone,
26
25
  default: () => index_default,
27
26
  mergeObjectIntersection: () => mergeObjectIntersection
@@ -30,7 +29,7 @@ module.exports = __toCommonJS(index_exports);
30
29
  var import_compiler = require("@sinclair/typebox/compiler");
31
30
  var Kind = Symbol.for("TypeBox.Kind");
32
31
  var Hint = Symbol.for("TypeBox.Hint");
33
- var isSpecialProperty = (name) => /(\ |-|\t|\n)/.test(name) || !isNaN(+name[0]);
32
+ var isSpecialProperty = (name) => /(\ |-|\t|\n|\.)/.test(name) || !isNaN(+name[0]);
34
33
  var joinProperty = (v1, v2, isOptional = false) => {
35
34
  if (typeof v2 === "number") return `${v1}[${v2}]`;
36
35
  if (isSpecialProperty(v2)) return `${v1}${isOptional ? "?." : ""}["${v2}"]`;
@@ -40,7 +39,7 @@ var encodeProperty = (v) => isSpecialProperty(v) ? `"${v}"` : v;
40
39
  var sanitize = (key, sanitize2 = 0, schema) => {
41
40
  if (schema.type !== "string" || schema.const || schema.trusted) return key;
42
41
  let hof = "";
43
- for (let i = sanitize2 - 1; i >= 0; i--) hof += `sanitize[${i}](`;
42
+ for (let i = sanitize2 - 1; i >= 0; i--) hof += `d.h${i}(`;
44
43
  return hof + key + ")".repeat(sanitize2);
45
44
  };
46
45
  var mergeObjectIntersection = (schema) => {
@@ -118,10 +117,6 @@ function deepClone(source, weak = /* @__PURE__ */ new WeakMap()) {
118
117
  }
119
118
  return source;
120
119
  }
121
- var renameFunction = (v, name) => {
122
- if (!v.startsWith("return")) v = v.replace(/check_T0/g, `_${name}`);
123
- return v.replace("return function check(", `function ${name}(`);
124
- };
125
120
  var handleUnion = (schemas, property, instruction) => {
126
121
  if (instruction.TypeCompiler === void 0) {
127
122
  if (!instruction.typeCompilerWanred) {
@@ -168,13 +163,16 @@ var handleUnion = (schemas, property, instruction) => {
168
163
  type.items[i2] = unwrapRef(type.items[i2]);
169
164
  else type.items = unwrapRef(type.items);
170
165
  }
171
- const name = `un_${ui}_${i}`;
172
- typeChecks.push(renameFunction(import_compiler.TypeCompiler.Code(type), name));
173
- v += `if(${name}(${property})){return ${mirror(type, property, {
174
- ...instruction,
175
- recursion: instruction.recursion + 1,
176
- parentIsOptional: true
177
- })}}
166
+ typeChecks.push(import_compiler.TypeCompiler.Compile(type));
167
+ v += `if(d.unions[${ui}][${i}].Check(${property})){return ${mirror(
168
+ type,
169
+ property,
170
+ {
171
+ ...instruction,
172
+ recursion: instruction.recursion + 1,
173
+ parentIsOptional: true
174
+ }
175
+ )}}
178
176
  `;
179
177
  }
180
178
  v += `return ${instruction.removeUnknownUnionType ? "undefined" : property}})()`;
@@ -313,18 +311,17 @@ var mirror = (schema, property, instruction) => {
313
311
  }
314
312
  return `${v}return x`;
315
313
  };
316
- var createMirrorCode = (schema, {
314
+ var createMirror = (schema, {
317
315
  TypeCompiler: TypeCompiler2,
318
316
  modules,
319
317
  definitions,
320
318
  sanitize: sanitize2,
321
319
  recursionLimit = 8,
322
- removeUnknownUnionType = false,
323
- name = "mirror"
320
+ removeUnknownUnionType = false
324
321
  } = {}) => {
325
322
  const unions = [];
326
323
  if (typeof sanitize2 === "function") sanitize2 = [sanitize2];
327
- let f = mirror(schema, "v", {
324
+ const f = mirror(schema, "v", {
328
325
  optionals: [],
329
326
  optionalsInArray: [],
330
327
  array: 0,
@@ -340,43 +337,24 @@ var createMirrorCode = (schema, {
340
337
  recursionLimit,
341
338
  removeUnknownUnionType
342
339
  });
343
- if (unions.length) {
344
- let header = "";
345
- for (let i = 0; i < unions.length; i++) {
346
- for (let j = 0; j < unions[i].length; j++)
347
- header += unions[i][j] + "\n";
348
- }
349
- f = header + f;
350
- unions.length = 0;
340
+ if (!unions.length && !sanitize2?.length) return Function("v", f);
341
+ let hof;
342
+ if (sanitize2?.length) {
343
+ hof = {};
344
+ for (let i = 0; i < sanitize2.length; i++) hof[`h${i}`] = sanitize2[i];
351
345
  }
352
- f = `function ${name}(v){${f}}`;
353
- if (!sanitize2?.length) return f;
354
- return [`function ${name}(sanitize){return ${f}}`, sanitize2];
355
- };
356
- var createMirror = (schema, {
357
- TypeCompiler: TypeCompiler2,
358
- modules,
359
- definitions,
360
- sanitize: sanitize2,
361
- recursionLimit = 8,
362
- removeUnknownUnionType = false
363
- } = {}) => {
364
- const code = createMirrorCode(schema, {
365
- TypeCompiler: TypeCompiler2,
366
- modules,
367
- definitions,
368
- sanitize: sanitize2,
369
- recursionLimit,
370
- removeUnknownUnionType
346
+ return Function(
347
+ "d",
348
+ `return function mirror(v){${f}}`
349
+ )({
350
+ unions,
351
+ ...hof
371
352
  });
372
- if (typeof code === "string") return Function(`return ${code}`)();
373
- return Function(`return ${code[0]}`)()(code[1]);
374
353
  };
375
354
  var index_default = createMirror;
376
355
  // Annotate the CommonJS export names for ESM import in node:
377
356
  0 && (module.exports = {
378
357
  createMirror,
379
- createMirrorCode,
380
358
  deepClone,
381
359
  mergeObjectIntersection
382
360
  });
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TypeCompiler } from '@sinclair/typebox/compiler';
1
+ import { TypeCompiler, type TypeCheck } from '@sinclair/typebox/compiler';
2
2
  import type { TAnySchema, TModule } from '@sinclair/typebox';
3
3
  export declare const mergeObjectIntersection: (schema: TAnySchema) => TAnySchema;
4
4
  type MaybeArray<T> = T | T[];
@@ -7,7 +7,7 @@ export interface Instruction {
7
7
  optionalsInArray: string[][];
8
8
  parentIsOptional: boolean;
9
9
  array: number;
10
- unions: string[][];
10
+ unions: TypeCheck<any>[][];
11
11
  unionKeys: Record<string, 1>;
12
12
  sanitize: MaybeArray<(v: string) => string> | undefined;
13
13
  /**
@@ -38,8 +38,5 @@ export interface Instruction {
38
38
  removeUnknownUnionType: boolean;
39
39
  }
40
40
  export declare function deepClone<T>(source: T, weak?: WeakMap<object, any>): T;
41
- export declare const createMirrorCode: (schema: TAnySchema, { TypeCompiler, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType, name }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType"> & {
42
- name?: string;
43
- }>) => string | [string, ((v: string) => string)[]];
44
41
  export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType">>) => ((v: T["static"]) => T["static"]);
45
42
  export default createMirror;
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  import { TypeCompiler } from "@sinclair/typebox/compiler";
3
3
  var Kind = Symbol.for("TypeBox.Kind");
4
4
  var Hint = Symbol.for("TypeBox.Hint");
5
- var isSpecialProperty = (name) => /(\ |-|\t|\n)/.test(name) || !isNaN(+name[0]);
5
+ var isSpecialProperty = (name) => /(\ |-|\t|\n|\.)/.test(name) || !isNaN(+name[0]);
6
6
  var joinProperty = (v1, v2, isOptional = false) => {
7
7
  if (typeof v2 === "number") return `${v1}[${v2}]`;
8
8
  if (isSpecialProperty(v2)) return `${v1}${isOptional ? "?." : ""}["${v2}"]`;
@@ -12,7 +12,7 @@ var encodeProperty = (v) => isSpecialProperty(v) ? `"${v}"` : v;
12
12
  var sanitize = (key, sanitize2 = 0, schema) => {
13
13
  if (schema.type !== "string" || schema.const || schema.trusted) return key;
14
14
  let hof = "";
15
- for (let i = sanitize2 - 1; i >= 0; i--) hof += `sanitize[${i}](`;
15
+ for (let i = sanitize2 - 1; i >= 0; i--) hof += `d.h${i}(`;
16
16
  return hof + key + ")".repeat(sanitize2);
17
17
  };
18
18
  var mergeObjectIntersection = (schema) => {
@@ -90,10 +90,6 @@ function deepClone(source, weak = /* @__PURE__ */ new WeakMap()) {
90
90
  }
91
91
  return source;
92
92
  }
93
- var renameFunction = (v, name) => {
94
- if (!v.startsWith("return")) v = v.replace(/check_T0/g, `_${name}`);
95
- return v.replace("return function check(", `function ${name}(`);
96
- };
97
93
  var handleUnion = (schemas, property, instruction) => {
98
94
  if (instruction.TypeCompiler === void 0) {
99
95
  if (!instruction.typeCompilerWanred) {
@@ -140,13 +136,16 @@ var handleUnion = (schemas, property, instruction) => {
140
136
  type.items[i2] = unwrapRef(type.items[i2]);
141
137
  else type.items = unwrapRef(type.items);
142
138
  }
143
- const name = `un_${ui}_${i}`;
144
- typeChecks.push(renameFunction(TypeCompiler.Code(type), name));
145
- v += `if(${name}(${property})){return ${mirror(type, property, {
146
- ...instruction,
147
- recursion: instruction.recursion + 1,
148
- parentIsOptional: true
149
- })}}
139
+ typeChecks.push(TypeCompiler.Compile(type));
140
+ v += `if(d.unions[${ui}][${i}].Check(${property})){return ${mirror(
141
+ type,
142
+ property,
143
+ {
144
+ ...instruction,
145
+ recursion: instruction.recursion + 1,
146
+ parentIsOptional: true
147
+ }
148
+ )}}
150
149
  `;
151
150
  }
152
151
  v += `return ${instruction.removeUnknownUnionType ? "undefined" : property}})()`;
@@ -285,18 +284,17 @@ var mirror = (schema, property, instruction) => {
285
284
  }
286
285
  return `${v}return x`;
287
286
  };
288
- var createMirrorCode = (schema, {
287
+ var createMirror = (schema, {
289
288
  TypeCompiler: TypeCompiler2,
290
289
  modules,
291
290
  definitions,
292
291
  sanitize: sanitize2,
293
292
  recursionLimit = 8,
294
- removeUnknownUnionType = false,
295
- name = "mirror"
293
+ removeUnknownUnionType = false
296
294
  } = {}) => {
297
295
  const unions = [];
298
296
  if (typeof sanitize2 === "function") sanitize2 = [sanitize2];
299
- let f = mirror(schema, "v", {
297
+ const f = mirror(schema, "v", {
300
298
  optionals: [],
301
299
  optionalsInArray: [],
302
300
  array: 0,
@@ -312,42 +310,23 @@ var createMirrorCode = (schema, {
312
310
  recursionLimit,
313
311
  removeUnknownUnionType
314
312
  });
315
- if (unions.length) {
316
- let header = "";
317
- for (let i = 0; i < unions.length; i++) {
318
- for (let j = 0; j < unions[i].length; j++)
319
- header += unions[i][j] + "\n";
320
- }
321
- f = header + f;
322
- unions.length = 0;
313
+ if (!unions.length && !sanitize2?.length) return Function("v", f);
314
+ let hof;
315
+ if (sanitize2?.length) {
316
+ hof = {};
317
+ for (let i = 0; i < sanitize2.length; i++) hof[`h${i}`] = sanitize2[i];
323
318
  }
324
- f = `function ${name}(v){${f}}`;
325
- if (!sanitize2?.length) return f;
326
- return [`function ${name}(sanitize){return ${f}}`, sanitize2];
327
- };
328
- var createMirror = (schema, {
329
- TypeCompiler: TypeCompiler2,
330
- modules,
331
- definitions,
332
- sanitize: sanitize2,
333
- recursionLimit = 8,
334
- removeUnknownUnionType = false
335
- } = {}) => {
336
- const code = createMirrorCode(schema, {
337
- TypeCompiler: TypeCompiler2,
338
- modules,
339
- definitions,
340
- sanitize: sanitize2,
341
- recursionLimit,
342
- removeUnknownUnionType
319
+ return Function(
320
+ "d",
321
+ `return function mirror(v){${f}}`
322
+ )({
323
+ unions,
324
+ ...hof
343
325
  });
344
- if (typeof code === "string") return Function(`return ${code}`)();
345
- return Function(`return ${code[0]}`)()(code[1]);
346
326
  };
347
327
  var index_default = createMirror;
348
328
  export {
349
329
  createMirror,
350
- createMirrorCode,
351
330
  deepClone,
352
331
  index_default as default,
353
332
  mergeObjectIntersection
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exact-mirror",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Mirror exact value to TypeBox/OpenAPI model",
5
5
  "license": "MIT",
6
6
  "scripts": {