gigobyte / purify

Functional programming library for TypeScript - https://gigobyte.github.io/purify/
ISC License
1.52k stars 59 forks source link

Just Date gives unexpected result when applying JSON.stringify() #613

Closed mfredix closed 1 year ago

mfredix commented 1 year ago

Given this code

const myDate = new Date();
const justDate = Maybe.of(myDate);
const myObject = {
    field: justDate
};
const myNestedObject = {
    field: Maybe.of( {nested: myDate})
};

console.log(JSON.stringify(myDate));
console.log(JSON.stringify(justDate));
console.log(JSON.stringify(myObject));
console.log(JSON.stringify(myNestedObject));
console.log(JSON.stringify(justDate) === JSON.stringify(Maybe.of(new Date('1995-12-17T03:24:00'))));

, using purify-ts 1.3.0. the output is something like

"2022-10-28T16:09:09.534Z"
{}
{"field":{}}
{"field":{"nested":"2022-10-28T16:09:09.534Z"}}
true  

I guess one can argue about what the expected JSON should be, but dropping the timestamp info is confusing and could generate subtle bugs.

gigobyte commented 1 year ago

This is indeed some very strange behaviour in toJSON. I've added an explicit check for dates because I couldn't find any other workaround.