exact-mirror 0.1.6 → 0.2.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.
@@ -1,4 +1,4 @@
1
- import { TypeCompiler, type TypeCheck } from '@sinclair/typebox/compiler';
1
+ import { TypeCompiler } 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: TypeCheck<any>[][];
10
+ unions: string[][];
11
11
  unionKeys: Record<string, 1>;
12
12
  sanitize: MaybeArray<(v: string) => string> | undefined;
13
13
  /**
@@ -38,5 +38,8 @@ 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)[]];
41
44
  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"]);
42
45
  export default createMirror;
package/dist/cjs/index.js CHANGED
@@ -21,6 +21,7 @@ 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,
24
25
  deepClone: () => deepClone,
25
26
  default: () => index_default,
26
27
  mergeObjectIntersection: () => mergeObjectIntersection
@@ -39,7 +40,7 @@ var encodeProperty = (v) => isSpecialProperty(v) ? `"${v}"` : v;
39
40
  var sanitize = (key, sanitize2 = 0, schema) => {
40
41
  if (schema.type !== "string" || schema.const || schema.trusted) return key;
41
42
  let hof = "";
42
- for (let i = sanitize2 - 1; i >= 0; i--) hof += `d.h${i}(`;
43
+ for (let i = sanitize2 - 1; i >= 0; i--) hof += `sanitize[${i}](`;
43
44
  return hof + key + ")".repeat(sanitize2);
44
45
  };
45
46
  var mergeObjectIntersection = (schema) => {
@@ -117,6 +118,10 @@ function deepClone(source, weak = /* @__PURE__ */ new WeakMap()) {
117
118
  }
118
119
  return source;
119
120
  }
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
+ };
120
125
  var handleUnion = (schemas, property, instruction) => {
121
126
  if (instruction.TypeCompiler === void 0) {
122
127
  if (!instruction.typeCompilerWanred) {
@@ -163,16 +168,13 @@ var handleUnion = (schemas, property, instruction) => {
163
168
  type.items[i2] = unwrapRef(type.items[i2]);
164
169
  else type.items = unwrapRef(type.items);
165
170
  }
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
- )}}
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
+ })}}
176
178
  `;
177
179
  }
178
180
  v += `return ${instruction.removeUnknownUnionType ? "undefined" : property}})()`;
@@ -311,17 +313,18 @@ var mirror = (schema, property, instruction) => {
311
313
  }
312
314
  return `${v}return x`;
313
315
  };
314
- var createMirror = (schema, {
316
+ var createMirrorCode = (schema, {
315
317
  TypeCompiler: TypeCompiler2,
316
318
  modules,
317
319
  definitions,
318
320
  sanitize: sanitize2,
319
321
  recursionLimit = 8,
320
- removeUnknownUnionType = false
322
+ removeUnknownUnionType = false,
323
+ name = "mirror"
321
324
  } = {}) => {
322
325
  const unions = [];
323
326
  if (typeof sanitize2 === "function") sanitize2 = [sanitize2];
324
- const f = mirror(schema, "v", {
327
+ let f = mirror(schema, "v", {
325
328
  optionals: [],
326
329
  optionalsInArray: [],
327
330
  array: 0,
@@ -337,24 +340,43 @@ var createMirror = (schema, {
337
340
  recursionLimit,
338
341
  removeUnknownUnionType
339
342
  });
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];
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;
345
351
  }
346
- return Function(
347
- "d",
348
- `return function mirror(v){${f}}`
349
- )({
350
- unions,
351
- ...hof
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
352
371
  });
372
+ if (typeof code === "string") return Function(`return ${code}`)();
373
+ return Function(`return ${code[0]}`)()(code[1]);
353
374
  };
354
375
  var index_default = createMirror;
355
376
  // Annotate the CommonJS export names for ESM import in node:
356
377
  0 && (module.exports = {
357
378
  createMirror,
379
+ createMirrorCode,
358
380
  deepClone,
359
381
  mergeObjectIntersection
360
382
  });
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TypeCompiler, type TypeCheck } from '@sinclair/typebox/compiler';
1
+ import { TypeCompiler } 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: TypeCheck<any>[][];
10
+ unions: string[][];
11
11
  unionKeys: Record<string, 1>;
12
12
  sanitize: MaybeArray<(v: string) => string> | undefined;
13
13
  /**
@@ -38,5 +38,8 @@ 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)[]];
41
44
  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"]);
42
45
  export default createMirror;
package/dist/index.mjs CHANGED
@@ -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 += `d.h${i}(`;
15
+ for (let i = sanitize2 - 1; i >= 0; i--) hof += `sanitize[${i}](`;
16
16
  return hof + key + ")".repeat(sanitize2);
17
17
  };
18
18
  var mergeObjectIntersection = (schema) => {
@@ -90,6 +90,10 @@ 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
+ };
93
97
  var handleUnion = (schemas, property, instruction) => {
94
98
  if (instruction.TypeCompiler === void 0) {
95
99
  if (!instruction.typeCompilerWanred) {
@@ -136,16 +140,13 @@ var handleUnion = (schemas, property, instruction) => {
136
140
  type.items[i2] = unwrapRef(type.items[i2]);
137
141
  else type.items = unwrapRef(type.items);
138
142
  }
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
- )}}
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
+ })}}
149
150
  `;
150
151
  }
151
152
  v += `return ${instruction.removeUnknownUnionType ? "undefined" : property}})()`;
@@ -284,17 +285,18 @@ var mirror = (schema, property, instruction) => {
284
285
  }
285
286
  return `${v}return x`;
286
287
  };
287
- var createMirror = (schema, {
288
+ var createMirrorCode = (schema, {
288
289
  TypeCompiler: TypeCompiler2,
289
290
  modules,
290
291
  definitions,
291
292
  sanitize: sanitize2,
292
293
  recursionLimit = 8,
293
- removeUnknownUnionType = false
294
+ removeUnknownUnionType = false,
295
+ name = "mirror"
294
296
  } = {}) => {
295
297
  const unions = [];
296
298
  if (typeof sanitize2 === "function") sanitize2 = [sanitize2];
297
- const f = mirror(schema, "v", {
299
+ let f = mirror(schema, "v", {
298
300
  optionals: [],
299
301
  optionalsInArray: [],
300
302
  array: 0,
@@ -310,23 +312,42 @@ var createMirror = (schema, {
310
312
  recursionLimit,
311
313
  removeUnknownUnionType
312
314
  });
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];
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;
318
323
  }
319
- return Function(
320
- "d",
321
- `return function mirror(v){${f}}`
322
- )({
323
- unions,
324
- ...hof
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
325
343
  });
344
+ if (typeof code === "string") return Function(`return ${code}`)();
345
+ return Function(`return ${code[0]}`)()(code[1]);
326
346
  };
327
347
  var index_default = createMirror;
328
348
  export {
329
349
  createMirror,
350
+ createMirrorCode,
330
351
  deepClone,
331
352
  index_default as default,
332
353
  mergeObjectIntersection
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exact-mirror",
3
- "version": "0.1.6",
3
+ "version": "0.2.0",
4
4
  "description": "Mirror exact value to TypeBox/OpenAPI model",
5
5
  "license": "MIT",
6
6
  "scripts": {