|
1
|
import { isObject } from "./is-object" |
|
2
|
|
|
3
|
type NestedObject = { |
|
4
|
[key: string]: any |
|
5
|
} |
|
6
|
|
|
7
|
/* |
|
8
|
Given a deeply nested object that can be arrays or objects, this function will flatten |
|
9
|
it to an object that is 1 level deep. |
|
10
|
|
|
11
|
Given an object: testObject = { |
|
12
|
product: { |
|
13
|
id: "test-product", |
|
14
|
name: "Test Product", |
|
15
|
categories: [{ |
|
16
|
id: "test-category", |
|
17
|
name: "Test Category" |
|
18
|
}] |
|
19
|
} |
|
20
|
} |
|
21
|
|
|
22
|
flattenObjectToKeyValuePairs(testObject) will return |
|
23
|
|
|
24
|
{ |
|
25
|
"product.id": "test-product", |
|
26
|
"product.name": "Test Product", |
|
27
|
"product.categories.id": ["test-category"], |
|
28
|
"product.categories.name": ["Test Category"] |
|
29
|
} |
|
30
|
|
|
31
|
Null and undefined values are excluded from the result. |
|
32
|
*/ |
|
33
|
export function flattenObjectToKeyValuePairs(obj: NestedObject): NestedObject { |
|
34
|
const result: NestedObject = {} |
|
35
|
|
|
36
|
function shouldPreserveArray(value: any[], path: string[]): boolean { |
|
37
|
if (!Array.isArray(value) || value.length === 0) { |
|
38
|
return false |
|
39
|
} |
|
40
|
|
|
41
|
if (value.some((item) => isObject(item) && !Array.isArray(item))) { |
|
42
|
return true |
|
43
|
} |
|
44
|
|
|
45
|
if (value.some((item) => Array.isArray(item))) { |
|
46
|
return true |
|
47
|
} |
|
48
|
|
|
49
|
if (path.length > 1) { |
|
50
|
return true |
|
51
|
} |
|
52
|
|
|
53
|
if (value.length > 1) { |
|
54
|
const firstType = typeof value[0] |
|
55
|
const allSameType = value.every( |
|
56
|
(item) => |
|
57
|
typeof item === firstType && !isObject(item) && !Array.isArray(item) |
|
58
|
) |
|
59
|
if (allSameType) { |
|
60
|
return true |
|
61
|
} |
|
62
|
} |
|
63
|
|
|
64
|
return false |
|
65
|
} |
|
66
|
|
|
67
|
/** |
|
68
|
* Normalize array values - unwrap single-item arrays and handle empty arrays |
|
69
|
*/ |
|
70
|
function normalizeArrayValue(value: any, path: string[]): any { |
|
71
|
if (!Array.isArray(value)) { |
|
72
|
return value |
|
73
|
} |
|
74
|
|
|
75
|
if ( |
|
76
|
value.length === 1 && |
|
77
|
Array.isArray(value[0]) && |
|
78
|
value[0].length === 0 |
|
79
|
) { |
|
80
|
return [] |
|
81
|
} |
|
82
|
|
|
83
|
if (shouldPreserveArray(value, path)) { |
|
84
|
return value |
|
85
|
} |
|
86
|
|
|
87
|
if (value.length === 1 && !isObject(value[0]) && !Array.isArray(value[0])) { |
|
88
|
return value[0] |
|
89
|
} |
|
90
|
|
|
91
|
return value |
|
92
|
} |
|
93
|
|
|
94
|
/** |
|
95
|
* Recursively process an object/array and flatten it |
|
96
|
*/ |
|
97
|
function processPath(value: any, currentPath: string[] = []): void { |
|
98
|
// Handle null, undefined, or primitive values |
|
99
|
if (!value || typeof value !== "object") { |
|
100
|
if (value !== null && value !== undefined && currentPath.length > 0) { |
|
101
|
result[currentPath.join(".")] = value |
|
102
|
} |
|
103
|
return |
|
104
|
} |
|
105
|
|
|
106
|
if (Array.isArray(value)) { |
|
107
|
if (value.length === 0) { |
|
108
|
if (currentPath.length > 0) { |
|
109
|
result[currentPath.join(".")] = [] |
|
110
|
} |
|
111
|
return |
|
112
|
} |
|
113
|
|
|
114
|
if (value.some((item) => isObject(item) && !Array.isArray(item))) { |
|
115
|
extractPropertiesFromArray(value, currentPath) |
|
116
|
} else if (value.some((item) => Array.isArray(item))) { |
|
117
|
const allValues: any[] = [] |
|
118
|
const flattenedObjects: Record<string, any>[] = [] |
|
119
|
|
|
120
|
const flattenArray = (arr: any[], collector: any[] = []): void => { |
|
121
|
arr.forEach((item) => { |
|
122
|
if (Array.isArray(item)) { |
|
123
|
flattenArray(item, collector) |
|
124
|
} else if (isObject(item)) { |
|
125
|
collector.push(item) |
|
126
|
} else if (item !== null && item !== undefined) { |
|
127
|
allValues.push(item) |
|
128
|
} |
|
129
|
}) |
|
130
|
} |
|
131
|
|
|
132
|
flattenArray(value, flattenedObjects) |
|
133
|
|
|
134
|
if (flattenedObjects.length > 0) { |
|
135
|
extractPropertiesFromArray(flattenedObjects, currentPath) |
|
136
|
} else if (allValues.length > 0) { |
|
137
|
result[currentPath.join(".")] = normalizeArrayValue( |
|
138
|
allValues, |
|
139
|
currentPath |
|
140
|
) |
|
141
|
} |
|
142
|
} else { |
|
143
|
const cleanedValues = value.filter((v) => v !== null && v !== undefined) |
|
144
|
if (cleanedValues.length > 0) { |
|
145
|
result[currentPath.join(".")] = normalizeArrayValue( |
|
146
|
cleanedValues, |
|
147
|
currentPath |
|
148
|
) |
|
149
|
} |
|
150
|
} |
|
151
|
return |
|
152
|
} |
|
153
|
|
|
154
|
Object.entries(value).forEach(([key, propValue]) => { |
|
155
|
const newPath = [...currentPath, key] |
|
156
|
|
|
157
|
if (propValue === null || propValue === undefined) { |
|
158
|
return |
|
159
|
} else if (Array.isArray(propValue)) { |
|
160
|
if (propValue.length === 0) { |
|
161
|
result[newPath.join(".")] = [] |
|
162
|
} else { |
|
163
|
processPath(propValue, newPath) |
|
164
|
} |
|
165
|
} else if (isObject(propValue)) { |
|
166
|
processPath(propValue, newPath) |
|
167
|
} else { |
|
168
|
result[newPath.join(".")] = propValue |
|
169
|
} |
|
170
|
}) |
|
171
|
} |
|
172
|
|
|
173
|
/** |
|
174
|
* Extract all properties from an array of objects and store them |
|
175
|
*/ |
|
176
|
function extractPropertiesFromArray( |
|
177
|
array: any[], |
|
178
|
basePath: string[] = [] |
|
179
|
): void { |
|
180
|
if (!array.length) return |
|
181
|
|
|
182
|
// Collect all unique keys from all objects in the array |
|
183
|
const allKeys = new Set<string>() |
|
184
|
array.forEach((item) => { |
|
185
|
if (isObject(item) && !Array.isArray(item)) { |
|
186
|
Object.keys(item).forEach((key) => allKeys.add(key)) |
|
187
|
} |
|
188
|
}) |
|
189
|
|
|
190
|
allKeys.forEach((key) => { |
|
191
|
const valuePath = [...basePath, key] |
|
192
|
const values: any[] = [] |
|
193
|
|
|
194
|
array.forEach((item) => { |
|
195
|
if (isObject(item) && !Array.isArray(item) && key in item) { |
|
196
|
const itemValue = item[key] |
|
197
|
if (itemValue !== null && itemValue !== undefined) { |
|
198
|
values.push(itemValue) |
|
199
|
} |
|
200
|
} |
|
201
|
}) |
|
202
|
|
|
203
|
if (values.length === 0) return |
|
204
|
|
|
205
|
if (values.every((v) => isObject(v) && !Array.isArray(v))) { |
|
206
|
extractNestedObjectProperties(values, valuePath) |
|
207
|
} else if (values.some((v) => Array.isArray(v))) { |
|
208
|
if (values.every((v) => Array.isArray(v) && v.length === 0)) { |
|
209
|
result[valuePath.join(".")] = [] |
|
210
|
} else { |
|
211
|
const flattenedArray: any[] = [] |
|
212
|
for (const arrayValue of values) { |
|
213
|
if (Array.isArray(arrayValue)) { |
|
214
|
if (arrayValue.some((v) => isObject(v) && !Array.isArray(v))) { |
|
215
|
extractPropertiesFromArray(arrayValue, valuePath) |
|
216
|
} else { |
|
217
|
flattenedArray.push(...arrayValue) |
|
218
|
} |
|
219
|
} else { |
|
220
|
flattenedArray.push(arrayValue) |
|
221
|
} |
|
222
|
} |
|
223
|
|
|
224
|
if ( |
|
225
|
flattenedArray.length > 0 && |
|
226
|
!flattenedArray.some((v) => isObject(v) && !Array.isArray(v)) |
|
227
|
) { |
|
228
|
result[valuePath.join(".")] = normalizeArrayValue( |
|
229
|
flattenedArray, |
|
230
|
valuePath |
|
231
|
) |
|
232
|
} |
|
233
|
} |
|
234
|
} else { |
|
235
|
result[valuePath.join(".")] = normalizeArrayValue(values, valuePath) |
|
236
|
} |
|
237
|
}) |
|
238
|
} |
|
239
|
|
|
240
|
/** |
|
241
|
* Extract properties from nested objects and add them to the result |
|
242
|
*/ |
|
243
|
function extractNestedObjectProperties( |
|
244
|
objects: any[], |
|
245
|
basePath: string[] = [] |
|
246
|
): void { |
|
247
|
if (!objects.length) return |
|
248
|
|
|
249
|
// Collect all unique keys from all objects |
|
250
|
const allNestedKeys = new Set<string>() |
|
251
|
objects.forEach((obj) => { |
|
252
|
if (isObject(obj) && !Array.isArray(obj)) { |
|
253
|
Object.keys(obj).forEach((key) => allNestedKeys.add(key)) |
|
254
|
} |
|
255
|
}) |
|
256
|
|
|
257
|
allNestedKeys.forEach((nestedKey) => { |
|
258
|
const nestedPath = [...basePath, nestedKey] |
|
259
|
const nestedValues: any[] = [] |
|
260
|
|
|
261
|
objects.forEach((obj) => { |
|
262
|
if (isObject(obj) && !Array.isArray(obj) && nestedKey in obj) { |
|
263
|
const nestedValue = obj[nestedKey] |
|
264
|
if (nestedValue !== null && nestedValue !== undefined) { |
|
265
|
nestedValues.push(nestedValue) |
|
266
|
} |
|
267
|
} |
|
268
|
}) |
|
269
|
|
|
270
|
if (nestedValues.length === 0) return |
|
271
|
|
|
272
|
if (nestedValues.every((v) => isObject(v) && !Array.isArray(v))) { |
|
273
|
extractNestedObjectProperties(nestedValues, nestedPath) |
|
274
|
} else if (nestedValues.some((v) => Array.isArray(v))) { |
|
275
|
if (nestedValues.every((v) => Array.isArray(v) && v.length === 0)) { |
|
276
|
result[nestedPath.join(".")] = [] |
|
277
|
} else { |
|
278
|
const allArrayItems: any[] = [] |
|
279
|
for (const arrayValue of nestedValues) { |
|
280
|
if (Array.isArray(arrayValue)) { |
|
281
|
allArrayItems.push(...arrayValue) |
|
282
|
} else { |
|
283
|
allArrayItems.push(arrayValue) |
|
284
|
} |
|
285
|
} |
|
286
|
|
|
287
|
if ( |
|
288
|
allArrayItems.some((item) => isObject(item) && !Array.isArray(item)) |
|
289
|
) { |
|
290
|
extractPropertiesFromArray(allArrayItems, nestedPath) |
|
291
|
} else { |
|
292
|
result[nestedPath.join(".")] = normalizeArrayValue( |
|
293
|
allArrayItems, |
|
294
|
nestedPath |
|
295
|
) |
|
296
|
} |
|
297
|
} |
|
298
|
} else { |
|
299
|
result[nestedPath.join(".")] = normalizeArrayValue( |
|
300
|
nestedValues, |
|
301
|
nestedPath |
|
302
|
) |
|
303
|
} |
|
304
|
}) |
|
305
|
} |
|
306
|
|
|
307
|
processPath(obj) |
|
308
|
|
|
309
|
return result |
|
310
|
} |