-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
Apply keys transformation only on plain objects #357
base: main
Are you sure you want to change the base?
Conversation
packages/change-case/package.json
Outdated
@@ -45,6 +45,9 @@ | |||
"specs": "ts-scripts specs", | |||
"test": "ts-scripts test" | |||
}, | |||
"dependencies": { | |||
"is-plain-obj": "^4.1.0" |
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.
I would inline it to not introduce extra dependencies.
Btw hi dude!
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.
Updated 🙂
Hope you're doing well @TrySound !
@TrySound can this be merged? Btw, I'm curious, from what I can tell this project has been around for nearly 12 years, how is it only now this problem has surfaced? |
It was added in 2023 with #86 (comment). @ben-eb I'm a little busy with a newborn but I'll try to get around to releasing in the next month, thanks for the contribution! |
|
||
if (Array.isArray(object)) { | ||
return object.map((item) => changeKeys(item, depth - 1, options)); | ||
} | ||
|
||
if (!isPlainObject(object)) return 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.
Nit: We access Object.getPrototypeOf
below too, wdyt about accessing it once and making a second function take the prototype to check it? E.g. isObject(x) && isPlainPrototype(x)
?
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.
I could re-use the Object.getPrototypeOf(object)
call and pass it into isPlainObject
;
function isPlainObject(prototype: unknown, value: unknown) {
if (typeof value !== "object" || value === null) {
return false;
}
return (
(prototype === null ||
prototype === Object.prototype ||
Object.getPrototypeOf(prototype) === null) &&
!(Symbol.toStringTag in value) &&
!(Symbol.iterator in value)
);
}
// ...
const prototype = Object.getPrototypeOf(object);
if (!isPlainObject(prototype, object)) return false;
const result = Object.create(prototype);
Not sure about this implementation because it requires the caller to know more about the internals of isPlainObject
, rather than a standalone implementation. It may be slightly more performant though 🙂
This is so that we can test `Object.create(null)`. Currently, that fails with `TypeError: Cannot convert object to primitive value`
Closes #356