Skip to content

feat: add flattenBySeparator and unflattenBySeparator for objects - #320

Open
11gorizont11 wants to merge 1 commit into
metarhia:masterfrom
11gorizont11:feature/object-utils
Open

11gorizont11 wants to merge 1 commit into
metarhia:masterfrom
11gorizont11:feature/object-utils

Conversation

@11gorizont11

Copy link
Copy Markdown
  • tests and linter show no problems (npm t)
  • tests are added/updated for bug fixes and new features
  • code is properly formatted (npm run fix)
  • description of changes is added in CHANGELOG.md
  • update .d.ts typings

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:

  • flattenBySeparator: Converts a nested object into a flat dictionary where keys are paths separated by a specified character (e.g., .).
  • unflattenBySeparator: Reconstructs a nested object from a flat dictionary using the separator to interpret the keys as paths.
    These utilities are particularly useful when:
  1. i18n: You need to "unflatten" translation keys (e.g., user.profile.name) into a nested object structure for consumption by libraries like next-i18next or custom localization logic.
  2. Configuration: Managing environment variables or flat config files that represent nested application settings.
  3. Data Transformation: Handling API responses that use dot-notation for nested fields.

Comment thread lib/objects.js
for (const [key, value] of Object.entries(source)) {
const newKey = parentKey + key;

if (value === null || value === undefined) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks line json field can't contain undefined value

Comment thread lib/objects.js
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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like code duplication and can be covered by recursion

@tshemsedinov

Copy link
Copy Markdown
Member

@timursevimli do we need it in metarhia and what for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants