This repository was archived by the owner on May 17, 2019. It is now read-only.
This repository was archived by the owner on May 17, 2019. It is now read-only.
Date objects do not survive across node and browser boundaries. #171
Open
Description
In fusion js the redux store is stringified and parsed to be passed between client and server. This creates an issue where high level types, including built-in's like Date
break on this line:
fusion-plugin-react-redux/src/browser.js
Line 49 in a465f71
Normal parse doesn't work correctly for ISO8601 date strings, and there is a good example of a workaround I'll reference from here and a robust regex from here
// Problem
const body = `{
"date": "2016-04-26T18:09:16Z"
}`;
const obj = JSON.parse(body);
const { date } = obj;
console.log(typeof date);
// "string"
But we can add a revive function that fixes this issue.
// Solution
function isIso8601(value: string): boolean {
const dateFormat = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
return dateFormat.test(value)
}
function reviver(key, value) {
if (typeof value === "string" && isIso8601(value)) {
return new Date(value);
}
return value;
}
const text = '{ "date": "2016-04-26T18:09:16Z" }';
const obj = JSON.parse(text, reviver);
console.log(typeof obj.date);
// "object"
I'd like to open a PR to implement this reviver.
Metadata
Metadata
Assignees
Labels
No labels