@hyperspan/framework 1.0.19 → 1.0.20

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperspan/framework",
3
- "version": "1.0.19",
3
+ "version": "1.0.20",
4
4
  "description": "Hyperspan Web Framework",
5
5
  "main": "src/server.ts",
6
6
  "types": "src/server.ts",
package/src/utils.test.ts CHANGED
@@ -65,6 +65,40 @@ describe('formDataToJSON', () => {
65
65
  colors: ['red', 'green', 'blue'],
66
66
  });
67
67
  });
68
+
69
+ test('formDataToJSON handles array of objects with numeric keys', () => {
70
+ const formData = new FormData();
71
+ formData.append('contact[0][name]', 'John');
72
+ formData.append('contact[0][email]', 'john@example.com');
73
+ formData.append('contact[1][name]', 'Jane');
74
+ formData.append('contact[1][email]', 'jane@example.com');
75
+
76
+ const result = formDataToJSON(formData);
77
+
78
+ expect(result).toEqual({
79
+ contact: [
80
+ { name: 'John', email: 'john@example.com' },
81
+ { name: 'Jane', email: 'jane@example.com' },
82
+ ],
83
+ } as any);
84
+ });
85
+
86
+ test('formDataToJSON keeps object when keys are mixed numeric and text', () => {
87
+ const formData = new FormData();
88
+ formData.append('contact[0][name]', 'John');
89
+ formData.append('contact[0][email]', 'john@example.com');
90
+ formData.append('contact[label]', 'Primary');
91
+
92
+ const result = formDataToJSON(formData);
93
+
94
+ // Not ALL keys are numeric ("label"), so it must stay an object - not an array
95
+ expect(result).toEqual({
96
+ contact: {
97
+ '0': { name: 'John', email: 'john@example.com' },
98
+ label: 'Primary',
99
+ },
100
+ } as any);
101
+ });
68
102
  });
69
103
 
70
104
  describe('parsePath', () => {
package/src/utils.ts CHANGED
@@ -131,11 +131,38 @@ export function formDataToJSON(formData: FormData | URLSearchParams): Record<str
131
131
  }
132
132
  };
133
133
 
134
+ /**
135
+ * Recursively converts objects whose keys are ALL numeric (e.g. "contact[0][name]",
136
+ * "contact[1][name]") into arrays of objects. Objects with any non-numeric key are
137
+ * left as-is.
138
+ */
139
+ const arrayify = (value: any): any => {
140
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
141
+ return value;
142
+ }
143
+
144
+ const keys = Object.keys(value);
145
+ const allNumeric = keys.length > 0 && keys.every((key) => /^\d+$/.test(key));
146
+
147
+ if (allNumeric) {
148
+ const array: any[] = [];
149
+ for (const key of keys.sort((a, b) => Number(a) - Number(b))) {
150
+ array[Number(key)] = arrayify(value[key]);
151
+ }
152
+ return array;
153
+ }
154
+
155
+ for (const key of keys) {
156
+ value[key] = arrayify(value[key]);
157
+ }
158
+ return value;
159
+ };
160
+
134
161
  for (const pair of formData.entries()) {
135
162
  assign(parseKey(pair[0]), pair[1], object);
136
163
  }
137
164
 
138
- return object;
165
+ return arrayify(object);
139
166
  }
140
167
 
141
168
  /**