-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unflat json #62
base: main
Are you sure you want to change the base?
Unflat json #62
Conversation
@@ -81,3 +81,37 @@ describe("flat functon", () => { | |||
expect(flat(data)).toEqual(expected); | |||
}); | |||
}); | |||
|
|||
|
|||
describe("unflat functon", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: unflat function
}); | ||
|
||
// Test case for unflattening a simple object | ||
it("Unflattens a simple object", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lint: remove extra tab
} else { | ||
acc[k] = acc[k] || {}; | ||
} | ||
return acc[k]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What this return doing?
keys.reduce((acc, k, i) => { | ||
if (i === keys.length - 1) { | ||
acc[k] = data[key]; | ||
} else { | ||
acc[k] = acc[k] || {}; | ||
} | ||
return acc[k]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use meaningful argument/variables names.
const unflat = (data, delimiter = ".") => { | ||
return Object.keys(data).reduce((unflattenedObject, key) => { | ||
const keys = key.split(delimiter); | ||
keys.reduce((acc, k, i) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reduce function is being used but its output isn't directly utilized. This approach is valid, but it can be a bit confusing. Using a simple forEach loop might make the intent clearer.
if (i === keys.length - 1) { | ||
acc[k] = data[key]; | ||
} else { | ||
acc[k] = acc[k] || {}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment here to clarify the code's purpose.
const expected = {}; | ||
expect(unflat(data)).toEqual(expected); | ||
}); | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a few more tests that cover scenarios with more deeply nested objects, and also add module tests for cjs and esm.
Description
Related Issue
Add unflat Function to Restore Flattened JSON Structures #48
Checklist