1 import { isObject } from "@medusajs/framework/utils"
2 import { OPERATOR_MAP } from "./query-builder"
3
4 /**
5 * Flatten object keys
6 * @example
7 * input: {
8 * a: 1,
9 * b: {
10 * c: 2,
11 * d: 3,
12 * },
13 * e: 4,
14 * }
15 *
16 * output: {
17 * a: 1,
18 * b.c: 2,
19 * b.d: 3,
20 * e: 4,
21 * }
22 *
23 * @param input
24 */
25 export function flattenObjectKeys(input: Record<string, any>) {
26 const result: Record<string, any> = {}
27
28 function flatten(obj: Record<string, any>, path?: string) {
29 for (const key in obj) {
30 const isOperatorMap = !!OPERATOR_MAP[key]
31
32 if (isOperatorMap) {
33 result[path ?? key] = obj
34 continue
35 }
36
37 const newPath = path ? `${path}.${key}` : key
38
39 if (obj[key] === null) {
40 result[newPath] = null
41 continue
42 }
43
44 if (Array.isArray(obj[key])) {
45 result[newPath] = obj[key]
46 continue
47 }
48
49 if (isObject(obj[key]) && !isOperatorMap) {
50 flatten(obj[key], newPath)
51 continue
52 }
53
54 result[newPath] = obj[key]
55 }
56 }
57
58 flatten(input)
59 return result
60 }