Removed moment for internal implementation

This commit is contained in:
Alexander Cerutti
2021-02-09 14:16:19 +01:00
parent 9de5b3a0c4
commit 3e08d31d14
4 changed files with 38 additions and 23 deletions

5
package-lock.json generated
View File

@@ -179,11 +179,6 @@
"brace-expansion": "^1.1.7"
}
},
"moment": {
"version": "2.29.1",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz",
"integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ=="
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",

View File

@@ -26,7 +26,6 @@
"dependencies": {
"debug": "^4.3.1",
"joi": "^17.3.0",
"moment": "^2.29.1",
"node-forge": "^0.10.0",
"tslib": "^2.1.0",
"yazl": "^2.5.1"

View File

@@ -76,12 +76,11 @@ describe("Passkit-generator", function () {
expect(pass.props["expirationDate"]).toBe(undefined);
});
it("Will set expiration with a Date as argument", () => {
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("expects a Date object as the only argument", () => {
pass.expiration(new Date(2020, 6, 1, 0, 0, 0, 0));
// Month starts from 0 in Date Object when used this way, therefore
// we expect one month more
expect(pass.props["expirationDate"]).toBe("2020-07-01T00:00:00Z");
});
it("An invalid date, will not apply changes", () => {
@@ -96,12 +95,9 @@ describe("Passkit-generator", function () {
});
describe("relevantDate()", () => {
it("A date object will apply changes", () => {
it("expects a Date object as the only argument", () => {
pass.relevantDate(new Date("10-04-2021"));
// 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("2021-10-04T00:00:00");
expect(pass.props["relevantDate"]).toBe("2021-10-04T00:00:00Z");
});
});
@@ -264,8 +260,8 @@ describe("Passkit-generator", function () {
});
it("Will ignore non-Barcodes schema compliant objects", () => {
// @ts-expect-error
pass.barcodes(
// @ts-expect-error
5,
10,
15,

View File

@@ -1,4 +1,3 @@
import moment from "moment";
import { EOL } from "os";
import { PartitionedBundle, BundleUnit } from "./schema";
import { sep } from "path";
@@ -41,13 +40,39 @@ export function dateToW3CString(date: Date) {
return "";
}
const parsedDate = moment(date).format();
if (parsedDate === "Invalid date") {
// if it is NaN, it is "Invalid Date"
if (isNaN(Number(date))) {
return undefined;
}
return parsedDate;
const paddedMonth = padMeTwo(date.getMonth() + 1);
const paddedDay = padMeTwo(date.getDate());
const paddedHour = padMeTwo(date.getHours());
const paddedMinutes = padMeTwo(date.getMinutes());
const paddedSeconds = padMeTwo(date.getSeconds());
/**
* Date.prototype.getTimezoneOffset returns the timezone UTC offset in
* minutes of the local machine.
*
* That value should then be used to calculate the effective timezone as
* string, but still that would be related to the machine and not to the
* specified date.
*
* For this reason we are completing date with "Z" TimeZoneDesignator (TZD)
* to say it to use local timezone.
*
* In the future we might think to integrate another parameter to represent
* a custom timezone.
*
* @see https://www.w3.org/TR/NOTE-datetime
*/
return `${date.getFullYear()}-${paddedMonth}-${paddedDay}T${paddedHour}:${paddedMinutes}:${paddedSeconds}Z`;
}
function padMeTwo(original: string | number) {
return String(original).padStart(2, "0");
}
/**