Skip to content

Commit

Permalink
Return ISODateString instead of date
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
fisehara committed Mar 4, 2024
1 parent 5478179 commit 37e2d44
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/types/date-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
7 changes: 5 additions & 2 deletions src/types/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/types/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
6 changes: 3 additions & 3 deletions test/Date Time.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
6 changes: 3 additions & 3 deletions test/Date.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
5 changes: 4 additions & 1 deletion test/Time.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down

0 comments on commit 37e2d44

Please sign in to comment.