-
Notifications
You must be signed in to change notification settings - Fork 12
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
feat: add transformKeysToSnake #39
feat: add transformKeysToSnake #39
Conversation
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.
rest, lgtm
src/transformKeysToSnake.js
Outdated
* @param {Object} obj | ||
* @returns {Object} | ||
*/ | ||
const transformKeysToSnakeCase = (obj) => { |
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.
should we have a deep
/recursive
flag to inform that nested objects should also be applied?
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 do you propose, the following?
const transformKeysToSnakeCase = (obj, { recursive } = {}) => {
if (Array.isArray(obj)) {
return obj.map(item => transformKeysToSnakeCase(item))
}
if (!isPlainObject(obj)) {
return obj
}
return Object.fromEntries(
Object.entries(obj).map(([k, v]) => [
snakeCase(k),
recursive ? transformKeysToSnakeCase(v) : v
])
)
}
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.
yeah, I give the consumer more control
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.
need to update package-lock.json as well
Description:
New features:
PR status: