fix(keys): preserve non-plain object values like Date and RegExp#365
Open
naveentehrpariya wants to merge 1 commit into
Open
fix(keys): preserve non-plain object values like Date and RegExp#365naveentehrpariya wants to merge 1 commit into
naveentehrpariya wants to merge 1 commit into
Conversation
`change-case/keys` recursed into any value where `typeof === "object"`,
including `Date`, `RegExp`, `Map` and class instances. For these, copying
the (empty) own keys onto a freshly created object with the same prototype
dropped the internal state, e.g. `camelCase({ created_at: new Date() })`
produced an object whose `created_at` threw
`TypeError: Method Date.prototype.toISOString called on incompatible receiver`.
Only recurse into plain objects (and arrays); return other objects as-is.
Fixes blakeembrey#356
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #356.
Problem
change-case/keysrecurses into any value wheretypeof value === "object". That includes non-plain objects such asDate,RegExp,Map/Setand class instances. For those, it creates a new object with the same prototype and copies the own enumerable keys — but their state lives in internal slots, not enumerable keys, so the result is a broken object.Fix
Only recurse into plain objects (prototype
Object.prototypeornull) and arrays. Any other object is returned untouched, soDate,RegExp, etc. pass through as-is.Tests
Added cases asserting that a
Date/RegExpvalue is preserved by reference (and remains usable), and that a non-plain object passed as the top-level value is returned untouched. Both fail without this change and pass with it; the full package suite (29 tests) stays green.