exact-mirror 0.0.7 → 0.0.9

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/bunfig.toml ADDED
@@ -0,0 +1,2 @@
1
+ [install]
2
+ registry = "https://registry.npmjs.org/"
@@ -1,13 +1,15 @@
1
1
  import { TypeCompiler, type TypeCheck } from '@sinclair/typebox/compiler';
2
2
  import type { TAnySchema } from '@sinclair/typebox';
3
3
  export declare const mergeObjectIntersection: (schema: TAnySchema) => TAnySchema;
4
- interface Instruction {
4
+ type MaybeArray<T> = T | T[];
5
+ export interface Instruction {
5
6
  optionals: string[];
6
7
  optionalsInArray: string[][];
7
8
  parentIsOptional: boolean;
8
9
  array: number;
9
10
  unions: TypeCheck<any>[][];
10
11
  unionKeys: Record<string, 1>;
12
+ sanitize: MaybeArray<(v: string) => string> | undefined;
11
13
  /**
12
14
  * TypeCompiler is required when using Union
13
15
  *
@@ -20,5 +22,5 @@ interface Instruction {
20
22
  typeCompilerWanred?: boolean;
21
23
  definitions: Record<string, TAnySchema>;
22
24
  }
23
- export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, definitions }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions">>) => ((v: T["static"]) => T["static"]);
25
+ export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, definitions, sanitize }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions" | "sanitize">>) => ((v: T["static"]) => T["static"]);
24
26
  export default createMirror;
package/dist/cjs/index.js CHANGED
@@ -27,7 +27,6 @@ __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");
31
30
  var isSpecialProperty = (name) => /(\ |-|\t|\n)/.test(name);
32
31
  var joinProperty = (v1, v2, isOptional = false) => {
33
32
  if (typeof v2 === "number") return `${v1}?.[${v2}]`;
@@ -35,6 +34,12 @@ var joinProperty = (v1, v2, isOptional = false) => {
35
34
  return `${v1}${true ? "?" : ""}.${v2}`;
36
35
  };
37
36
  var encodeProperty = (v) => isSpecialProperty(v) ? `"${v}"` : v;
37
+ var sanitize = (key, sanitize2 = 0, schema) => {
38
+ if (schema.type !== "string" || schema.const || schema.trusted) return key;
39
+ let hof = "";
40
+ for (let i = sanitize2 - 1; i >= 0; i--) hof += `d.h${i}(`;
41
+ return hof + key + ")".repeat(sanitize2);
42
+ };
38
43
  var mergeObjectIntersection = (schema) => {
39
44
  if (!schema.allOf || Kind in schema && (schema[Kind] !== "Intersect" || schema.type !== "object"))
40
45
  return schema;
@@ -118,8 +123,9 @@ var mirror = (schema, property, instruction) => {
118
123
  ...instruction,
119
124
  definitions: Object.assign(instruction.definitions, schema.$defs)
120
125
  });
121
- if (isRoot && schema.type !== "object" && schema.type !== "array" && !schema.anyOf)
122
- return `return v`;
126
+ if (isRoot && schema.type !== "object" && schema.type !== "array" && !schema.anyOf) {
127
+ return `return ${sanitize("v", instruction.sanitize?.length, schema)}`;
128
+ }
123
129
  let v = "";
124
130
  switch (schema.type) {
125
131
  case "object":
@@ -203,7 +209,7 @@ var mirror = (schema, property, instruction) => {
203
209
  v = handleUnion(schema.anyOf, property, instruction);
204
210
  break;
205
211
  }
206
- v = property;
212
+ v = sanitize(property, instruction.sanitize?.length, schema);
207
213
  break;
208
214
  }
209
215
  if (!isRoot) return v;
@@ -222,9 +228,11 @@ var mirror = (schema, property, instruction) => {
222
228
  };
223
229
  var createMirror = (schema, {
224
230
  TypeCompiler: TypeCompiler2,
225
- definitions = {}
231
+ definitions = {},
232
+ sanitize: sanitize2
226
233
  } = {}) => {
227
234
  const unions = [];
235
+ if (typeof sanitize2 === "function") sanitize2 = [sanitize2];
228
236
  const f = mirror(schema, "v", {
229
237
  optionals: [],
230
238
  optionalsInArray: [],
@@ -233,15 +241,21 @@ var createMirror = (schema, {
233
241
  unions,
234
242
  unionKeys: {},
235
243
  TypeCompiler: TypeCompiler2,
236
- definitions
244
+ definitions,
245
+ sanitize: sanitize2
237
246
  });
238
- if (!unions.length) return Function("v", f);
239
- const fn = `return function mirror(v){${f}}`;
247
+ if (!unions.length && !sanitize2?.length) return Function("v", f);
248
+ let hof;
249
+ if (sanitize2?.length) {
250
+ hof = {};
251
+ for (let i = 0; i < sanitize2.length; i++) hof[`h${i}`] = sanitize2[i];
252
+ }
240
253
  return Function(
241
254
  "d",
242
- fn
255
+ `return function mirror(v){${f}}`
243
256
  )({
244
- unions
257
+ unions,
258
+ ...hof
245
259
  });
246
260
  };
247
261
  var index_default = createMirror;
package/dist/index.d.ts CHANGED
@@ -1,13 +1,15 @@
1
1
  import { TypeCompiler, type TypeCheck } from '@sinclair/typebox/compiler';
2
2
  import type { TAnySchema } from '@sinclair/typebox';
3
3
  export declare const mergeObjectIntersection: (schema: TAnySchema) => TAnySchema;
4
- interface Instruction {
4
+ type MaybeArray<T> = T | T[];
5
+ export interface Instruction {
5
6
  optionals: string[];
6
7
  optionalsInArray: string[][];
7
8
  parentIsOptional: boolean;
8
9
  array: number;
9
10
  unions: TypeCheck<any>[][];
10
11
  unionKeys: Record<string, 1>;
12
+ sanitize: MaybeArray<(v: string) => string> | undefined;
11
13
  /**
12
14
  * TypeCompiler is required when using Union
13
15
  *
@@ -20,5 +22,5 @@ interface Instruction {
20
22
  typeCompilerWanred?: boolean;
21
23
  definitions: Record<string, TAnySchema>;
22
24
  }
23
- export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, definitions }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions">>) => ((v: T["static"]) => T["static"]);
25
+ export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, definitions, sanitize }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions" | "sanitize">>) => ((v: T["static"]) => T["static"]);
24
26
  export default createMirror;
package/dist/index.mjs CHANGED
@@ -1,7 +1,6 @@
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");
5
4
  var isSpecialProperty = (name) => /(\ |-|\t|\n)/.test(name);
6
5
  var joinProperty = (v1, v2, isOptional = false) => {
7
6
  if (typeof v2 === "number") return `${v1}?.[${v2}]`;
@@ -9,6 +8,12 @@ var joinProperty = (v1, v2, isOptional = false) => {
9
8
  return `${v1}${true ? "?" : ""}.${v2}`;
10
9
  };
11
10
  var encodeProperty = (v) => isSpecialProperty(v) ? `"${v}"` : v;
11
+ var sanitize = (key, sanitize2 = 0, schema) => {
12
+ if (schema.type !== "string" || schema.const || schema.trusted) return key;
13
+ let hof = "";
14
+ for (let i = sanitize2 - 1; i >= 0; i--) hof += `d.h${i}(`;
15
+ return hof + key + ")".repeat(sanitize2);
16
+ };
12
17
  var mergeObjectIntersection = (schema) => {
13
18
  if (!schema.allOf || Kind in schema && (schema[Kind] !== "Intersect" || schema.type !== "object"))
14
19
  return schema;
@@ -92,8 +97,9 @@ var mirror = (schema, property, instruction) => {
92
97
  ...instruction,
93
98
  definitions: Object.assign(instruction.definitions, schema.$defs)
94
99
  });
95
- if (isRoot && schema.type !== "object" && schema.type !== "array" && !schema.anyOf)
96
- return `return v`;
100
+ if (isRoot && schema.type !== "object" && schema.type !== "array" && !schema.anyOf) {
101
+ return `return ${sanitize("v", instruction.sanitize?.length, schema)}`;
102
+ }
97
103
  let v = "";
98
104
  switch (schema.type) {
99
105
  case "object":
@@ -177,7 +183,7 @@ var mirror = (schema, property, instruction) => {
177
183
  v = handleUnion(schema.anyOf, property, instruction);
178
184
  break;
179
185
  }
180
- v = property;
186
+ v = sanitize(property, instruction.sanitize?.length, schema);
181
187
  break;
182
188
  }
183
189
  if (!isRoot) return v;
@@ -196,9 +202,11 @@ var mirror = (schema, property, instruction) => {
196
202
  };
197
203
  var createMirror = (schema, {
198
204
  TypeCompiler: TypeCompiler2,
199
- definitions = {}
205
+ definitions = {},
206
+ sanitize: sanitize2
200
207
  } = {}) => {
201
208
  const unions = [];
209
+ if (typeof sanitize2 === "function") sanitize2 = [sanitize2];
202
210
  const f = mirror(schema, "v", {
203
211
  optionals: [],
204
212
  optionalsInArray: [],
@@ -207,15 +215,21 @@ var createMirror = (schema, {
207
215
  unions,
208
216
  unionKeys: {},
209
217
  TypeCompiler: TypeCompiler2,
210
- definitions
218
+ definitions,
219
+ sanitize: sanitize2
211
220
  });
212
- if (!unions.length) return Function("v", f);
213
- const fn = `return function mirror(v){${f}}`;
221
+ if (!unions.length && !sanitize2?.length) return Function("v", f);
222
+ let hof;
223
+ if (sanitize2?.length) {
224
+ hof = {};
225
+ for (let i = 0; i < sanitize2.length; i++) hof[`h${i}`] = sanitize2[i];
226
+ }
214
227
  return Function(
215
228
  "d",
216
- fn
229
+ `return function mirror(v){${f}}`
217
230
  )({
218
- unions
231
+ unions,
232
+ ...hof
219
233
  });
220
234
  };
221
235
  var index_default = createMirror;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exact-mirror",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "Mirror exact value to TypeBox/OpenAPI model",
5
5
  "license": "MIT",
6
6
  "scripts": {