exact-mirror 0.0.4 → 0.0.6

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.
@@ -18,6 +18,7 @@ interface Instruction {
18
18
  */
19
19
  TypeCompiler?: typeof TypeCompiler;
20
20
  typeCompilerWanred?: boolean;
21
+ definitions: Record<string, TAnySchema>;
21
22
  }
22
- export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler }?: Pick<Instruction, "TypeCompiler">) => ((v: T["static"]) => T["static"]);
23
+ export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, definitions }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions">>) => ((v: T["static"]) => T["static"]);
23
24
  export default createMirror;
package/dist/cjs/index.js CHANGED
@@ -27,6 +27,7 @@ __export(index_exports, {
27
27
  module.exports = __toCommonJS(index_exports);
28
28
  var import_compiler = require("@sinclair/typebox/compiler");
29
29
  var Kind = Symbol.for("TypeBox.Kind");
30
+ var OptionalKind = Symbol.for("TypeBox.Optional");
30
31
  var isSpecialProperty = (name) => /(\ |-|\t|\n)/.test(name);
31
32
  var joinProperty = (v1, v2, isOptional = false) => {
32
33
  if (typeof v2 === "number") return `${v1}[${v2}]`;
@@ -112,6 +113,11 @@ var handleUnion = (schemas, property, instruction) => {
112
113
  var mirror = (schema, property, instruction) => {
113
114
  if (!schema) return "";
114
115
  const isRoot = property === "v" && !instruction.unions.length;
116
+ if (Kind in schema && schema[Kind] === "Import" && schema.$ref in schema.$defs)
117
+ return mirror(schema.$defs[schema.$ref], property, {
118
+ ...instruction,
119
+ definitions: Object.assign(instruction.definitions, schema.$defs)
120
+ });
115
121
  if (isRoot && schema.type !== "object" && schema.type !== "array" && !schema.anyOf)
116
122
  return `return v`;
117
123
  let v = "";
@@ -169,8 +175,13 @@ var mirror = (schema, property, instruction) => {
169
175
  }
170
176
  const i = instruction.array;
171
177
  instruction.array++;
172
- if (!isRoot) v = `(()=>{`;
173
- v += `const ar${i}s=${property},ar${i}v=new Array(${property}.length);for(let i=0;i<ar${i}s.length;i++){const ar${i}p=ar${i}s[i];ar${i}v[i]=${mirror(schema.items, `ar${i}p`, instruction)}`;
178
+ let reference = property;
179
+ if (isRoot) v = `const ar${i}v=new Array(${property}.length);`;
180
+ else {
181
+ reference = `ar${i}s`;
182
+ v = `((${reference})=>{const ar${i}v=new Array(${reference}.length);`;
183
+ }
184
+ v += `for(let i=0;i<${reference}.length;i++){const ar${i}p=${reference}[i];ar${i}v[i]=${mirror(schema.items, `ar${i}p`, instruction)}`;
174
185
  const optionals = instruction.optionalsInArray[i + 1];
175
186
  if (optionals) {
176
187
  for (let oi = 0; oi < optionals.length; oi++) {
@@ -179,9 +190,15 @@ var mirror = (schema, property, instruction) => {
179
190
  }
180
191
  }
181
192
  v += `}`;
182
- if (!isRoot) v += `return ar${i}v})()`;
193
+ if (!isRoot) v += `return ar${i}v})(${property})`;
183
194
  break;
184
195
  default:
196
+ if (schema.$ref && schema.$ref in instruction.definitions)
197
+ return mirror(
198
+ instruction.definitions[schema.$ref],
199
+ property,
200
+ instruction
201
+ );
185
202
  if (Array.isArray(schema.anyOf)) {
186
203
  v = handleUnion(schema.anyOf, property, instruction);
187
204
  break;
@@ -203,7 +220,10 @@ var mirror = (schema, property, instruction) => {
203
220
  }
204
221
  return `${v}return x`;
205
222
  };
206
- var createMirror = (schema, { TypeCompiler: TypeCompiler2 } = {}) => {
223
+ var createMirror = (schema, {
224
+ TypeCompiler: TypeCompiler2,
225
+ definitions = {}
226
+ } = {}) => {
207
227
  const unions = [];
208
228
  const f = mirror(schema, "v", {
209
229
  optionals: [],
@@ -212,7 +232,8 @@ var createMirror = (schema, { TypeCompiler: TypeCompiler2 } = {}) => {
212
232
  parentIsOptional: false,
213
233
  unions,
214
234
  unionKeys: {},
215
- TypeCompiler: TypeCompiler2
235
+ TypeCompiler: TypeCompiler2,
236
+ definitions
216
237
  });
217
238
  if (!unions.length) return Function("v", f);
218
239
  const fn = `return function mirror(v){${f}}`;
package/dist/index.d.ts CHANGED
@@ -18,6 +18,7 @@ interface Instruction {
18
18
  */
19
19
  TypeCompiler?: typeof TypeCompiler;
20
20
  typeCompilerWanred?: boolean;
21
+ definitions: Record<string, TAnySchema>;
21
22
  }
22
- export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler }?: Pick<Instruction, "TypeCompiler">) => ((v: T["static"]) => T["static"]);
23
+ export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, definitions }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions">>) => ((v: T["static"]) => T["static"]);
23
24
  export default createMirror;
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  // src/index.ts
2
2
  import { TypeCompiler } from "@sinclair/typebox/compiler";
3
3
  var Kind = Symbol.for("TypeBox.Kind");
4
+ var OptionalKind = Symbol.for("TypeBox.Optional");
4
5
  var isSpecialProperty = (name) => /(\ |-|\t|\n)/.test(name);
5
6
  var joinProperty = (v1, v2, isOptional = false) => {
6
7
  if (typeof v2 === "number") return `${v1}[${v2}]`;
@@ -86,6 +87,11 @@ var handleUnion = (schemas, property, instruction) => {
86
87
  var mirror = (schema, property, instruction) => {
87
88
  if (!schema) return "";
88
89
  const isRoot = property === "v" && !instruction.unions.length;
90
+ if (Kind in schema && schema[Kind] === "Import" && schema.$ref in schema.$defs)
91
+ return mirror(schema.$defs[schema.$ref], property, {
92
+ ...instruction,
93
+ definitions: Object.assign(instruction.definitions, schema.$defs)
94
+ });
89
95
  if (isRoot && schema.type !== "object" && schema.type !== "array" && !schema.anyOf)
90
96
  return `return v`;
91
97
  let v = "";
@@ -143,8 +149,13 @@ var mirror = (schema, property, instruction) => {
143
149
  }
144
150
  const i = instruction.array;
145
151
  instruction.array++;
146
- if (!isRoot) v = `(()=>{`;
147
- v += `const ar${i}s=${property},ar${i}v=new Array(${property}.length);for(let i=0;i<ar${i}s.length;i++){const ar${i}p=ar${i}s[i];ar${i}v[i]=${mirror(schema.items, `ar${i}p`, instruction)}`;
152
+ let reference = property;
153
+ if (isRoot) v = `const ar${i}v=new Array(${property}.length);`;
154
+ else {
155
+ reference = `ar${i}s`;
156
+ v = `((${reference})=>{const ar${i}v=new Array(${reference}.length);`;
157
+ }
158
+ v += `for(let i=0;i<${reference}.length;i++){const ar${i}p=${reference}[i];ar${i}v[i]=${mirror(schema.items, `ar${i}p`, instruction)}`;
148
159
  const optionals = instruction.optionalsInArray[i + 1];
149
160
  if (optionals) {
150
161
  for (let oi = 0; oi < optionals.length; oi++) {
@@ -153,9 +164,15 @@ var mirror = (schema, property, instruction) => {
153
164
  }
154
165
  }
155
166
  v += `}`;
156
- if (!isRoot) v += `return ar${i}v})()`;
167
+ if (!isRoot) v += `return ar${i}v})(${property})`;
157
168
  break;
158
169
  default:
170
+ if (schema.$ref && schema.$ref in instruction.definitions)
171
+ return mirror(
172
+ instruction.definitions[schema.$ref],
173
+ property,
174
+ instruction
175
+ );
159
176
  if (Array.isArray(schema.anyOf)) {
160
177
  v = handleUnion(schema.anyOf, property, instruction);
161
178
  break;
@@ -177,7 +194,10 @@ var mirror = (schema, property, instruction) => {
177
194
  }
178
195
  return `${v}return x`;
179
196
  };
180
- var createMirror = (schema, { TypeCompiler: TypeCompiler2 } = {}) => {
197
+ var createMirror = (schema, {
198
+ TypeCompiler: TypeCompiler2,
199
+ definitions = {}
200
+ } = {}) => {
181
201
  const unions = [];
182
202
  const f = mirror(schema, "v", {
183
203
  optionals: [],
@@ -186,7 +206,8 @@ var createMirror = (schema, { TypeCompiler: TypeCompiler2 } = {}) => {
186
206
  parentIsOptional: false,
187
207
  unions,
188
208
  unionKeys: {},
189
- TypeCompiler: TypeCompiler2
209
+ TypeCompiler: TypeCompiler2,
210
+ definitions
190
211
  });
191
212
  if (!unions.length) return Function("v", f);
192
213
  const fn = `return function mirror(v){${f}}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exact-mirror",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Mirror exact value to TypeBox/OpenAPI model",
5
5
  "license": "MIT",
6
6
  "scripts": {