exact-mirror 0.0.0 → 0.0.1

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/README.md CHANGED
@@ -4,6 +4,18 @@ Enforce value to TypeBox/OpenAPI model
4
4
 
5
5
  By providing model ahead of time, the library will generate a function to mirror a value to an exact type
6
6
 
7
+ ```
8
+ $ bun benchmarks/small
9
+
10
+ clk: ~3.13 GHz
11
+ cpu: Apple M1 Max
12
+ runtime: bun 1.2.4 (arm64-darwin)
13
+
14
+ summary
15
+ Exact Mirror
16
+ 556.23x faster than TypeBox Value.Clean
17
+ ```
18
+
7
19
  ## Installation
8
20
 
9
21
  ```bash
package/dist/cjs/index.js CHANGED
@@ -28,9 +28,9 @@ module.exports = __toCommonJS(index_exports);
28
28
  var Kind = Symbol.for("TypeBox.Kind");
29
29
  var OptionalKind = Symbol.for("TypeBox.Optional");
30
30
  var isSpecialProperty = (name) => /(\ |-|\t|\n)/.test(name);
31
- var joinProperty = (v1, v2) => {
32
- if (isSpecialProperty(v2)) return `${v1}["${v2}"]`;
33
- return `${v1}.${v2}`;
31
+ var joinProperty = (v1, v2, isOptional = false) => {
32
+ if (isSpecialProperty(v2)) return `${v1}${isOptional ? "?." : ""}["${v2}"]`;
33
+ return `${v1}${isOptional ? "?" : ""}.${v2}`;
34
34
  };
35
35
  var encodeProperty = (v) => isSpecialProperty(v) ? `"${v}"` : v;
36
36
  var mergeObjectIntersection = (schema) => {
@@ -66,13 +66,19 @@ var mirror = (schema, property, instruction) => {
66
66
  const keys = Object.keys(schema.properties);
67
67
  for (let i2 = 0; i2 < keys.length; i2++) {
68
68
  const key = keys[i2];
69
- const name = joinProperty(property, key);
70
- if (!schema.required.includes(key)) {
69
+ let isOptional = schema.required && !schema.required.includes(key);
70
+ const name = joinProperty(
71
+ property,
72
+ key,
73
+ instruction.parentIsOptional
74
+ );
75
+ if (isOptional) {
71
76
  const index = instruction.array;
72
77
  if (property.startsWith("ar")) {
78
+ const refName = name.slice(name.indexOf(".") + 1);
73
79
  const array = instruction.optionalsInArray;
74
- if (array[index]) array[index].push(name.slice(5));
75
- else array[index] = [name.slice(5)];
80
+ if (array[index]) array[index].push(refName);
81
+ else array[index] = [refName];
76
82
  } else {
77
83
  instruction.optionals.push(name);
78
84
  }
@@ -81,7 +87,14 @@ var mirror = (schema, property, instruction) => {
81
87
  if (schema.additionalProperties && child.type !== "object")
82
88
  continue;
83
89
  if (i2 !== 0) v += ",";
84
- v += `${encodeProperty(key)}:${mirror(child, name, instruction)}`;
90
+ v += `${encodeProperty(key)}:${isOptional ? `${name}===undefined?undefined:` : ""}${mirror(
91
+ child,
92
+ name,
93
+ {
94
+ ...instruction,
95
+ parentIsOptional: isOptional
96
+ }
97
+ )}`;
85
98
  }
86
99
  v += "}";
87
100
  break;
@@ -93,17 +106,16 @@ var mirror = (schema, property, instruction) => {
93
106
  break;
94
107
  }
95
108
  if (!isRoot) v = `(()=>{`;
96
- 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)};`;
109
+ 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)}`;
97
110
  const optionals = instruction.optionalsInArray[i + 1];
98
111
  if (optionals) {
99
112
  for (let oi = 0; oi < optionals.length; oi++) {
100
- const key = `ar${i}v[i].${optionals[oi]}`;
101
- if (oi !== 0) v += ";";
102
- v += `if(${key}===undefined)delete ${key}`;
113
+ const target = `ar${i}v[i].${optionals[oi]}`;
114
+ v += `;if(${target}===undefined)delete ${target}`;
103
115
  }
104
116
  }
105
117
  v += `}`;
106
- if (!isRoot) v += `}return ar${i}v})()`;
118
+ if (!isRoot) v += `return ar${i}v})()`;
107
119
  break;
108
120
  default:
109
121
  v = property;
@@ -123,7 +135,8 @@ var createMirror = (schema) => {
123
135
  const f = mirror(schema, "v", {
124
136
  optionals: [],
125
137
  optionalsInArray: [],
126
- array: 0
138
+ array: 0,
139
+ parentIsOptional: false
127
140
  });
128
141
  return Function("v", f);
129
142
  };
package/dist/index.mjs CHANGED
@@ -2,9 +2,9 @@
2
2
  var Kind = Symbol.for("TypeBox.Kind");
3
3
  var OptionalKind = Symbol.for("TypeBox.Optional");
4
4
  var isSpecialProperty = (name) => /(\ |-|\t|\n)/.test(name);
5
- var joinProperty = (v1, v2) => {
6
- if (isSpecialProperty(v2)) return `${v1}["${v2}"]`;
7
- return `${v1}.${v2}`;
5
+ var joinProperty = (v1, v2, isOptional = false) => {
6
+ if (isSpecialProperty(v2)) return `${v1}${isOptional ? "?." : ""}["${v2}"]`;
7
+ return `${v1}${isOptional ? "?" : ""}.${v2}`;
8
8
  };
9
9
  var encodeProperty = (v) => isSpecialProperty(v) ? `"${v}"` : v;
10
10
  var mergeObjectIntersection = (schema) => {
@@ -40,13 +40,19 @@ var mirror = (schema, property, instruction) => {
40
40
  const keys = Object.keys(schema.properties);
41
41
  for (let i2 = 0; i2 < keys.length; i2++) {
42
42
  const key = keys[i2];
43
- const name = joinProperty(property, key);
44
- if (!schema.required.includes(key)) {
43
+ let isOptional = schema.required && !schema.required.includes(key);
44
+ const name = joinProperty(
45
+ property,
46
+ key,
47
+ instruction.parentIsOptional
48
+ );
49
+ if (isOptional) {
45
50
  const index = instruction.array;
46
51
  if (property.startsWith("ar")) {
52
+ const refName = name.slice(name.indexOf(".") + 1);
47
53
  const array = instruction.optionalsInArray;
48
- if (array[index]) array[index].push(name.slice(5));
49
- else array[index] = [name.slice(5)];
54
+ if (array[index]) array[index].push(refName);
55
+ else array[index] = [refName];
50
56
  } else {
51
57
  instruction.optionals.push(name);
52
58
  }
@@ -55,7 +61,14 @@ var mirror = (schema, property, instruction) => {
55
61
  if (schema.additionalProperties && child.type !== "object")
56
62
  continue;
57
63
  if (i2 !== 0) v += ",";
58
- v += `${encodeProperty(key)}:${mirror(child, name, instruction)}`;
64
+ v += `${encodeProperty(key)}:${isOptional ? `${name}===undefined?undefined:` : ""}${mirror(
65
+ child,
66
+ name,
67
+ {
68
+ ...instruction,
69
+ parentIsOptional: isOptional
70
+ }
71
+ )}`;
59
72
  }
60
73
  v += "}";
61
74
  break;
@@ -67,17 +80,16 @@ var mirror = (schema, property, instruction) => {
67
80
  break;
68
81
  }
69
82
  if (!isRoot) v = `(()=>{`;
70
- 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)};`;
83
+ 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)}`;
71
84
  const optionals = instruction.optionalsInArray[i + 1];
72
85
  if (optionals) {
73
86
  for (let oi = 0; oi < optionals.length; oi++) {
74
- const key = `ar${i}v[i].${optionals[oi]}`;
75
- if (oi !== 0) v += ";";
76
- v += `if(${key}===undefined)delete ${key}`;
87
+ const target = `ar${i}v[i].${optionals[oi]}`;
88
+ v += `;if(${target}===undefined)delete ${target}`;
77
89
  }
78
90
  }
79
91
  v += `}`;
80
- if (!isRoot) v += `}return ar${i}v})()`;
92
+ if (!isRoot) v += `return ar${i}v})()`;
81
93
  break;
82
94
  default:
83
95
  v = property;
@@ -97,7 +109,8 @@ var createMirror = (schema) => {
97
109
  const f = mirror(schema, "v", {
98
110
  optionals: [],
99
111
  optionalsInArray: [],
100
- array: 0
112
+ array: 0,
113
+ parentIsOptional: false
101
114
  });
102
115
  return Function("v", f);
103
116
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exact-mirror",
3
- "version": "0.0.0",
3
+ "version": "0.0.1",
4
4
  "description": "Mirror exact value to TypeBox/OpenAPI model",
5
5
  "license": "MIT",
6
6
  "scripts": {