feat: add flattenBySeparator and unflattenBySeparator for objects - #320
Open
11gorizont11 wants to merge 1 commit into
Open
11gorizont11 wants to merge 1 commit into
11gorizont11 wants to merge 1 commit into
Conversation
| for (const [key, value] of Object.entries(source)) { | ||
| const newKey = parentKey + key; | ||
|
|
||
| if (value === null || value === undefined) { |
Member
There was a problem hiding this comment.
It looks line json field can't contain undefined value
Comment on lines
+131
to
+148
| if (item !== null && typeof item === 'object') { | ||
| const nested = flattenBySeparator({ [i]: item }, separator, newKey); | ||
| Object.assign(result, nested); | ||
| } else { | ||
| result[arrayKey] = item; | ||
| } | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (typeof value === 'object') { | ||
| const nested = flattenBySeparator(value, separator, newKey); | ||
| if (Object.keys(nested).length === 0) continue; | ||
| Object.assign(result, nested); | ||
| continue; | ||
| } | ||
|
|
||
| result[newKey] = value; |
Member
There was a problem hiding this comment.
Looks like code duplication and can be covered by recursion
Member
|
@timursevimli do we need it in metarhia and what for? |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
npm t)npm run fix)PR Description: Add flattenBySeparator and unflattenBySeparator for Object Manipulation
Motivation
Working with internationalization (i18n) resources often involves handling JSON translation files. These resources are frequently stored as plain (flat) JSON objects for ease of management in external translation tools or databases. However, within applications, it is often necessary to work with these resources as nested objects to match the logical structure of the UI or component hierarchy.
This PR introduces two new utility functions to metautil to facilitate this conversion:
These utilities are particularly useful when: