Skip to content

Commit f9ebe69

Browse files
committed
feat(utils): add construct function to build nested objects from path-based keys
1 parent 6ac07d5 commit f9ebe69

File tree

1 file changed

+12
-0
lines changed
  • primitives/filed-form /src/utils

1 file changed

+12
-0
lines changed

primitives/filed-form /src/utils/set.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,15 @@ export function unset<T>(obj: T, path: NamePath, options: SetOptions = { safeKey
100100

101101
return unsetImpl(obj, 0) as T;
102102
}
103+
104+
export const construct = <T extends Record<string, any>>(obj: T): T => {
105+
if (!obj) return {} as T;
106+
107+
let acc: Record<string, any> = {};
108+
109+
for (const [path, value] of Object.entries(obj)) {
110+
acc = set(acc, path, value);
111+
}
112+
113+
return acc as T;
114+
};

0 commit comments

Comments
 (0)