-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preapare the field for AppLogBar in my services
- Loading branch information
Showing
23 changed files
with
1,011 additions
and
555 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Transforms a flat object with dot-separated keys into a nested object. | ||
* | ||
* @param {Record<string, any>} input - The flat object to be nested. | ||
* @returns {Record<string, any>} A new object with nested properties. | ||
* | ||
* @example | ||
* const flatObject = { | ||
* "foo.bar": 1, | ||
* "foo.baz": "okay", | ||
* "cool": "yes" | ||
* }; | ||
* const nestedObject = nestObject(flatObject); | ||
* // Output will be: | ||
* // { | ||
* // "foo": { | ||
* // "bar": 1, | ||
* // "baz": "okay" | ||
* // }, | ||
* // "cool": "yes" | ||
* // } | ||
*/ | ||
export function nestObject(input: Record<string, any>): Record<string, any> { | ||
const output: Record<string, any> = {}; | ||
|
||
for (const [key, value] of Object.entries(input)) { | ||
let parts = key.split("."); | ||
let target = output; | ||
|
||
for (let i = 0; i < parts.length; i++) { | ||
const part = parts[i]; | ||
if (i === parts.length - 1) { | ||
target[part] = value; | ||
} else { | ||
if (!target[part] || typeof target[part] !== "object") { | ||
target[part] = {}; | ||
} | ||
target = target[part]; | ||
} | ||
} | ||
} | ||
|
||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.