exact-mirror 0.1.4 → 0.1.5

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.
package/dist/cjs/index.js CHANGED
@@ -66,7 +66,15 @@ var handleRecord = (schema, property, instruction) => {
66
66
  if (!child) return property;
67
67
  const i = instruction.array;
68
68
  instruction.array++;
69
- return `(()=>{const ar${i}s=Object.keys(${property}),ar${i}v={};for(let i=0;i<ar${i}s.length;i++){const ar${i}p=${property}[ar${i}s[i]];ar${i}v[ar${i}s[i]]=${mirror(child, `ar${i}p`, instruction)}}return ar${i}v})()`;
69
+ let v = `(()=>{const ar${i}s=Object.keys(${property}),ar${i}v={};for(let i=0;i<ar${i}s.length;i++){const ar${i}p=${property}[ar${i}s[i]];ar${i}v[ar${i}s[i]]=${mirror(child, `ar${i}p`, instruction)}`;
70
+ const optionals = instruction.optionalsInArray[i + 1];
71
+ if (optionals)
72
+ for (let oi = 0; oi < optionals.length; oi++) {
73
+ const target = `ar${i}v[ar${i}s[i]].${optionals[oi]}`;
74
+ v += `;if(${target}===undefined)delete ${target}`;
75
+ }
76
+ v += `}return ar${i}v})()`;
77
+ return v;
70
78
  };
71
79
  var handleTuple = (schema, property, instruction) => {
72
80
  const i = instruction.array;
@@ -196,7 +204,11 @@ var mirror = (schema, property, instruction) => {
196
204
  const keys = Object.keys(schema.properties);
197
205
  for (let i2 = 0; i2 < keys.length; i2++) {
198
206
  const key = keys[i2];
199
- let isOptional = schema.required && !schema.required.includes(key) || Array.isArray(schema.properties[key].anyOf);
207
+ let isOptional = (
208
+ // all fields are optional
209
+ !schema.required || // field is explicitly required
210
+ schema.required && !schema.required.includes(key) || Array.isArray(schema.properties[key].anyOf)
211
+ );
200
212
  const name = joinProperty(
201
213
  property,
202
214
  key,
@@ -282,15 +294,19 @@ var mirror = (schema, property, instruction) => {
282
294
  break;
283
295
  }
284
296
  if (!isRoot) return v;
285
- if (schema.type === "array") return `${v}return ar0v`;
286
- v = `const x=${v}
297
+ if (schema.type === "array") {
298
+ v = `${v}const x=ar0v;`;
299
+ } else {
300
+ v = `const x=${v}
287
301
  `;
302
+ }
288
303
  for (let i = 0; i < instruction.optionals.length; i++) {
289
304
  const key = instruction.optionals[i];
290
305
  const prop = key.slice(1);
291
306
  v += `if(${key}===undefined`;
292
307
  if (instruction.unionKeys[key]) v += `||x${prop}===undefined`;
293
- v += `)delete x${prop.charCodeAt(0) !== 63 ? "?" : ""}${prop}
308
+ const shouldQuestion = prop.charCodeAt(0) !== 63 && schema.type !== "array";
309
+ v += `)delete x${shouldQuestion ? "?" : ""}${prop}
294
310
  `;
295
311
  }
296
312
  return `${v}return x`;
package/dist/index.mjs CHANGED
@@ -39,7 +39,15 @@ var handleRecord = (schema, property, instruction) => {
39
39
  if (!child) return property;
40
40
  const i = instruction.array;
41
41
  instruction.array++;
42
- return `(()=>{const ar${i}s=Object.keys(${property}),ar${i}v={};for(let i=0;i<ar${i}s.length;i++){const ar${i}p=${property}[ar${i}s[i]];ar${i}v[ar${i}s[i]]=${mirror(child, `ar${i}p`, instruction)}}return ar${i}v})()`;
42
+ let v = `(()=>{const ar${i}s=Object.keys(${property}),ar${i}v={};for(let i=0;i<ar${i}s.length;i++){const ar${i}p=${property}[ar${i}s[i]];ar${i}v[ar${i}s[i]]=${mirror(child, `ar${i}p`, instruction)}`;
43
+ const optionals = instruction.optionalsInArray[i + 1];
44
+ if (optionals)
45
+ for (let oi = 0; oi < optionals.length; oi++) {
46
+ const target = `ar${i}v[ar${i}s[i]].${optionals[oi]}`;
47
+ v += `;if(${target}===undefined)delete ${target}`;
48
+ }
49
+ v += `}return ar${i}v})()`;
50
+ return v;
43
51
  };
44
52
  var handleTuple = (schema, property, instruction) => {
45
53
  const i = instruction.array;
@@ -169,7 +177,11 @@ var mirror = (schema, property, instruction) => {
169
177
  const keys = Object.keys(schema.properties);
170
178
  for (let i2 = 0; i2 < keys.length; i2++) {
171
179
  const key = keys[i2];
172
- let isOptional = schema.required && !schema.required.includes(key) || Array.isArray(schema.properties[key].anyOf);
180
+ let isOptional = (
181
+ // all fields are optional
182
+ !schema.required || // field is explicitly required
183
+ schema.required && !schema.required.includes(key) || Array.isArray(schema.properties[key].anyOf)
184
+ );
173
185
  const name = joinProperty(
174
186
  property,
175
187
  key,
@@ -255,15 +267,19 @@ var mirror = (schema, property, instruction) => {
255
267
  break;
256
268
  }
257
269
  if (!isRoot) return v;
258
- if (schema.type === "array") return `${v}return ar0v`;
259
- v = `const x=${v}
270
+ if (schema.type === "array") {
271
+ v = `${v}const x=ar0v;`;
272
+ } else {
273
+ v = `const x=${v}
260
274
  `;
275
+ }
261
276
  for (let i = 0; i < instruction.optionals.length; i++) {
262
277
  const key = instruction.optionals[i];
263
278
  const prop = key.slice(1);
264
279
  v += `if(${key}===undefined`;
265
280
  if (instruction.unionKeys[key]) v += `||x${prop}===undefined`;
266
- v += `)delete x${prop.charCodeAt(0) !== 63 ? "?" : ""}${prop}
281
+ const shouldQuestion = prop.charCodeAt(0) !== 63 && schema.type !== "array";
282
+ v += `)delete x${shouldQuestion ? "?" : ""}${prop}
267
283
  `;
268
284
  }
269
285
  return `${v}return x`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exact-mirror",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Mirror exact value to TypeBox/OpenAPI model",
5
5
  "license": "MIT",
6
6
  "scripts": {