Skip to content

Commit

Permalink
Date: support invalid+use shorter epoch time
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens committed Oct 1, 2024
1 parent 8d89732 commit c622102
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/qwik/src/core/shared/shared-serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ const allocate = <T>(container: DeserializeContainer, typeId: number, value: unk
case TypeIds.URL:
return new URL(value as string);
case TypeIds.Date:
return new Date(value as string);
return new Date(value as number);
case TypeIds.Regex:
const idx = (value as string).lastIndexOf('/');
return new RegExp((value as string).slice(1, idx), (value as string).slice(idx + 1));
Expand Down Expand Up @@ -1080,7 +1080,7 @@ function serialize(serializationContext: SerializationContext): void {
} else if (value instanceof URL) {
output(TypeIds.URL, value.href);
} else if (value instanceof Date) {
output(TypeIds.Date, value.toJSON());
output(TypeIds.Date, Number.isNaN(value.valueOf()) ? '' : value.valueOf());
} else if (value instanceof RegExp) {
output(TypeIds.Regex, value.toString());
} else if (value instanceof Error) {
Expand Down
13 changes: 9 additions & 4 deletions packages/qwik/src/core/shared/shared-serialization.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,15 @@ describe('shared-serialization', () => {
`);
});
it(title(TypeIds.Date), async () => {
expect(await dump(new Date('2020-01-02T12:34'))).toMatchInlineSnapshot(`
expect(await dump(new Date('2020-01-02T12:34Z'))).toMatchInlineSnapshot(`
"
0 Date "2020-01-02T11:34:00.000Z"
(30 chars)"
0 Date 1577968440000
(17 chars)"
`);
expect(await dump(new Date('invalid'))).toMatchInlineSnapshot(`
"
0 Date ""
(6 chars)"
`);
});
it(title(TypeIds.Regex), async () => {
Expand Down Expand Up @@ -463,7 +468,7 @@ describe('shared-serialization', () => {
expect(url.toString()).toBe('http://example.com/');
});
it(title(TypeIds.Date), async () => {
const objs = await serialize(new Date('2020-01-02T12:34Z"'));
const objs = await serialize(new Date(1234567890000));
const date = deserialize(objs)[0] as Date;
expect(date).toBeInstanceOf(Date);
expect(date.toISOString()).toBe('2009-02-13T23:31:30.000Z');
Expand Down

0 comments on commit c622102

Please sign in to comment.