From 37e2d446c0ec11f2e3a1ab41b1a205280062bd5d Mon Sep 17 00:00:00 2001 From: fisehara Date: Mon, 4 Mar 2024 15:45:27 +0100 Subject: [PATCH] Return `ISODateString` instead of `date` The internal and external representation of the returned object from pinejs should be consistent. Pinejs internal interface will return date or date time fields as date object. For the external interface the object is JSON.stringified so that date becomes an ISO string anyway. Change-type: major Signed-off-by: fisehara --- src/types/date-time.ts | 7 +++++-- src/types/date.ts | 7 +++++-- src/types/time.ts | 2 +- test/Date Time.js | 6 +++--- test/Date.js | 6 +++--- test/Time.js | 5 ++++- 6 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/types/date-time.ts b/src/types/date-time.ts index 0811ce8..3a6759f 100644 --- a/src/types/date-time.ts +++ b/src/types/date-time.ts @@ -10,10 +10,13 @@ export const types = { }; export const fetchProcessing = (data: any) => { - if (data == null || data instanceof Date) { + if (data == null) { return data; } - return new Date(data); + if (!(data instanceof Date)) { + data = new Date(data); + } + return data.toISOString(); }; export const nativeFactTypes = { diff --git a/src/types/date.ts b/src/types/date.ts index 60c5757..95c04f5 100644 --- a/src/types/date.ts +++ b/src/types/date.ts @@ -10,10 +10,13 @@ export const types = { }; export const fetchProcessing = (data: any) => { - if (data == null || data instanceof Date) { + if (data == null) { return data; } - return new Date(data); + if (!(data instanceof Date)) { + data = new Date(data); + } + return data.toISOString(); }; export const nativeFactTypes = { diff --git a/src/types/time.ts b/src/types/time.ts index 3b54873..1dba8fc 100644 --- a/src/types/time.ts +++ b/src/types/time.ts @@ -12,7 +12,7 @@ export const types = { export const fetchProcessing = (data: any) => { if (data != null) { // We append the date of the epoch so that we can parse this as a valid date. - return new Date('Thu, 01 Jan 1970 ' + data); + return new Date('Thu, 01 Jan 1970 ' + data).toISOString(); } return data; }; diff --git a/test/Date Time.js b/test/Date Time.js index 67d6e6f..c46ead1 100644 --- a/test/Date Time.js +++ b/test/Date Time.js @@ -3,9 +3,9 @@ import * as helpers from './helpers'; helpers.describe('Date Time', function (test) { const now = new Date(); describe('fetchProcessing', function () { - test.fetch(now, now); - test.fetch(now.toString(), now); - test.fetch(now.getTime(), now); + test.fetch(now, now.toISOString()); + test.fetch(now.toString(), new Date(now.toString()).toISOString()); + test.fetch(now.getTime(), new Date(now.getTime()).toISOString()); test.fetch(null, null); }); diff --git a/test/Date.js b/test/Date.js index ec2ae1d..638fbae 100644 --- a/test/Date.js +++ b/test/Date.js @@ -3,9 +3,9 @@ import * as helpers from './helpers'; helpers.describe('Date', function (test) { const now = new Date(); describe('fetchProcessing', function () { - test.fetch(now, now); - test.fetch(now.toString(), now); - test.fetch(now.getTime(), now); + test.fetch(now, now.toISOString()); + test.fetch(now.toString(), new Date(now.toString()).toISOString()); + test.fetch(now.getTime(), new Date(now.getTime()).toISOString()); test.fetch(null, null); }); diff --git a/test/Time.js b/test/Time.js index 7092344..623b8bf 100644 --- a/test/Time.js +++ b/test/Time.js @@ -6,7 +6,10 @@ helpers.describe('Time', function (test) { now.setMonth(0); now.setDate(1); describe('fetchProcessing', function () { - test.fetch(now.toTimeString(), now); + test.fetch( + now.toTimeString(), + new Date('Thu, 01 Jan 1970 ' + now.toTimeString()).toISOString(), + ); test.fetch(null, null); });