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

_.replace does not get rich objects #117

Closed
kyeotic opened this issue Dec 9, 2022 · 4 comments
Closed

_.replace does not get rich objects #117

kyeotic opened this issue Dec 9, 2022 · 4 comments

Comments

@kyeotic
Copy link

kyeotic commented Dec 9, 2022

The docs have this lovely pitch for custom serialization

Likewise, setter functions which take an optional overwrite parameter can also use that parameter to accept a "replacer" function that receives each key and value (yes, nested ones too) as arguments and allow you to provide an alternate means of stringifying the values. This is particularly useful for rich objects like Date types. See MDN's JSON.stringify docs for more information and examples. Alternately, you can set a global replacer to the store._.replace property to handle all set, setAll, add, and transact calls.

But its not true: if you create a function on store._.replace and serialize an object with a Date Key it will receive a Date value that has already been serialized to a string.

Am I missing something, or has this regressed somehow?

Vanilla Sandbox Example

@nbubna
Copy link
Owner

nbubna commented Dec 9, 2022

Hmm. Your sandbox example didn't have code to demonstrate this. But i think i see the problem. It's that we avoid sending functions to JSON.stringify and the _.replace check happens in that expression. I'll try to get a fix up soon.

@nbubna
Copy link
Owner

nbubna commented Dec 9, 2022

Turns out, JSON.stringify appears to be the issue:

Try this:
JSON.stringify({ d: new Date() }, (key, value) => value instanceof Date ? "Date!" : value)

Spoiler: it doesn't return '{"d":"Date!"}'. Not sure why...

@nbubna
Copy link
Owner

nbubna commented Dec 9, 2022

image

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

So it's because Date has a toJSON() method which supersedes the replacer function. Not sure there's any easy way to get the behavior you want, and honestly, i'm not sure i even should do that behavior.

@kyeotic
Copy link
Author

kyeotic commented Dec 9, 2022

Sorry about that, somehow the sandbox got reset to default. Glad you noticed the issue in any case.

I can workaround this problem, but I think the docs should be updated to reflect this. They specifically call out this case and it doesn't work.

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

No branches or pull requests

2 participants