Adding support for native Dates (#32) by Ianbale

This commit is contained in:
Ian Bale
2019-06-07 11:42:28 +01:00
committed by Alexander Cerutti
parent 0e46d855e4
commit 4ce889d655
3 changed files with 24 additions and 3 deletions

View File

@@ -91,6 +91,14 @@ describe("Node-Passkit-generator", function () {
expect(noTimeZoneDateTime).toBe("2021-04-10T00:00:00"); expect(noTimeZoneDateTime).toBe("2021-04-10T00:00:00");
}); });
it("A date as a Date object will apply changes", () => {
pass.expiration(new Date(2020,5,1,0,0,0));
// this is made to avoid problems with winter and summer time:
// we focus only on the date and time for the tests.
let noTimeZoneDateTime = pass._props["expirationDate"].split("+")[0];
expect(noTimeZoneDateTime).toBe("2020-06-01T00:00:00");
});
it("An invalid date, will not apply changes", () => { it("An invalid date, will not apply changes", () => {
pass.expiration("32/18/228317"); pass.expiration("32/18/228317");
expect(pass._props["expirationDate"]).toBe(undefined); expect(pass._props["expirationDate"]).toBe(undefined);
@@ -125,6 +133,14 @@ describe("Node-Passkit-generator", function () {
let noTimeZoneDateTime = pass._props["relevantDate"].split("+")[0]; let noTimeZoneDateTime = pass._props["relevantDate"].split("+")[0];
expect(noTimeZoneDateTime).toBe("2021-04-10T00:00:00"); expect(noTimeZoneDateTime).toBe("2021-04-10T00:00:00");
}); });
it("A date as a Date object will apply changes", () => {
pass.relevance("relevantDate",new Date(2020,5,1,0,0,0));
// this is made to avoid problems with winter and summer time:
// we focus only on the date and time for the tests.
let noTimeZoneDateTime = pass._props["relevantDate"].split("+")[0];
expect(noTimeZoneDateTime).toBe("2020-06-01T00:00:00");
});
}); });
describe("relevance('maxDistance')", () => { describe("relevance('maxDistance')", () => {

View File

@@ -259,7 +259,7 @@ class Pass {
*/ */
expiration(date, format) { expiration(date, format) {
if (typeof date !== "string") { if (typeof date !== "string" && !(date instanceof Date)) {
return this; return this;
} }
@@ -327,6 +327,11 @@ class Pass {
return assignLength(Number(!cond), this); return assignLength(Number(!cond), this);
} else if (type === "relevantDate") { } else if (type === "relevantDate") {
if (typeof data !== "string" && !(data instanceof Date)) {
genericDebug(formatMessage("DATE_FORMAT_UNMATCH", "Relevant Date"));
return this;
}
let dateParse = dateToW3CString(data, relevanceDateFormat); let dateParse = dateToW3CString(data, relevanceDateFormat);
if (!dateParse) { if (!dateParse) {

View File

@@ -34,11 +34,11 @@ function isValidRGB(value) {
*/ */
function dateToW3CString(date, format) { function dateToW3CString(date, format) {
if (typeof date !== "string") { if (typeof date !== "string" && !(date instanceof Date)) {
return ""; return "";
} }
const parsedDate = moment(date.replace(/\//g, "-"), format || ["MM-DD-YYYY hh:mm:ss", "DD-MM-YYYY hh:mm:ss"]).format(); const parsedDate = date instanceof Date ? moment(date).format() : moment(date.replace(/\//g, "-"), format || ["MM-DD-YYYY hh:mm:ss", "DD-MM-YYYY hh:mm:ss"]).format();
if (parsedDate === "Invalid date") { if (parsedDate === "Invalid date") {
return undefined; return undefined;