Skip to content
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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ben-eb
Copy link

@ben-eb ben-eb commented Feb 17, 2025

Closes #356

@@ -45,6 +45,9 @@
"specs": "ts-scripts specs",
"test": "ts-scripts test"
},
"dependencies": {
"is-plain-obj": "^4.1.0"
Copy link
Contributor

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!

Copy link
Author

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 !

@Revadike
Copy link

Revadike commented Mar 9, 2025

@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?

@blakeembrey
Copy link
Owner

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;
Copy link
Owner

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)?

Copy link
Author

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 🙂

ben-eb added 4 commits March 13, 2025 10:39
This is so that we can test `Object.create(null)`. Currently,
that fails with `TypeError: Cannot convert object to primitive value`
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.

change-case/keys fails on Date objects
4 participants