chore(utils): clean util package deps (#4146)
This commit is contained in:
44
packages/utils/src/common/object-to-string-path.ts
Normal file
44
packages/utils/src/common/object-to-string-path.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { isObject } from "./is-object"
|
||||
|
||||
/**
|
||||
* Converts a structure of find options to an
|
||||
* array of string paths
|
||||
* @example
|
||||
* input: {
|
||||
* test: {
|
||||
* test1: true,
|
||||
* test2: true,
|
||||
* test3: {
|
||||
* test4: true
|
||||
* },
|
||||
* },
|
||||
* test2: true
|
||||
* }
|
||||
* output: ['test.test1', 'test.test2', 'test.test3.test4', 'test2']
|
||||
* @param input
|
||||
*/
|
||||
export function objectToStringPath(input: object = {}): string[] {
|
||||
if (!isObject(input) || !Object.keys(input).length) {
|
||||
return []
|
||||
}
|
||||
|
||||
const output: Set<string> = new Set(Object.keys(input))
|
||||
|
||||
for (const key of Object.keys(input)) {
|
||||
if (input[key] != undefined && typeof input[key] === "object") {
|
||||
const deepRes = objectToStringPath(input[key])
|
||||
|
||||
const items = deepRes.reduce((acc, val) => {
|
||||
acc.push(`${key}.${val}`)
|
||||
return acc
|
||||
}, [] as string[])
|
||||
|
||||
items.forEach((item) => output.add(item))
|
||||
continue
|
||||
}
|
||||
|
||||
output.add(key)
|
||||
}
|
||||
|
||||
return Array.from(output)
|
||||
}
|
||||
Reference in New Issue
Block a user