Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/functions/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export class Serializer {
if (data === true || data === false) {
return data;
}
if (data instanceof Date) {
return data.toISOString();
}
if (Object.prototype.toString.call(data) === '[object String]') {
return data;
}
Expand Down Expand Up @@ -95,6 +98,14 @@ export class Serializer {
if (typeof json === 'function' || typeof json === 'object') {
return mapValues(json!, x => this.decode(x));
}
if (
typeof json === 'string' &&
/^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?(Z)?$/.test(
json
)
) {
return new Date(json);
}
// Anything else is safe to return.
return json;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/functions/test/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ describe('Serializer', () => {
expect(serializer.decode(1.2)).to.equal(1.2);
});

it('encodes date', () => {
expect(serializer.encode(new Date('2021-02-10T13:26:37.977Z'))).to.equal(
'2021-02-10T13:26:37.977Z'
);
});

it('decodes date', () => {
expect(serializer.decode('2021-02-10T13:26:37.977Z')).to.deep.equal(
new Date('2021-02-10T13:26:37.977Z')
);
});

it('encodes string', () => {
expect(serializer.encode('hello')).to.equal('hello');
});
Expand Down