From 769d75cb88408c8285d5a9580d0d1aab2029a2dc Mon Sep 17 00:00:00 2001 From: alexandercerutti Date: Sun, 19 Aug 2018 15:57:14 +0200 Subject: [PATCH] Added expiration() and void() methods and dateToW3CString() function to convert date in W3C format --- index.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/index.js b/index.js index 1ba8e98..6a4a088 100644 --- a/index.js +++ b/index.js @@ -160,6 +160,40 @@ class Pass { return Buffer.from(strings.join("\n"), "utf8"); } + /** + * Sets expirationDate property to the W3C date + * + * @method expiration + * @params {String} date - the date in string + * @returns {this} + */ + + expiration(date) { + if (typeof date !== "string") { + return this; + } + + const convDate = dateToW3CString(date); + + if (convDate) { + this.props.expirationDate = convDate; + } + + return this; + } + + /** + * Sets voided property to true + * + * @method void + * @return {this} + */ + + void() { + this.props.voided = true; + return this; + } + /** * Checks if pass model type is one of the supported ones * @@ -406,6 +440,27 @@ class Pass { } } +/** + * Converts a date to W3C Standard format + * + * @function dateToW3Cstring + * @params {String} + */ + +function dateToW3CString(date) { + if (typeof date !== "string") { + return ""; + } + + let parsedDate = moment(date, ["MM-DD-YYYY"]).format(); + + if (parsedDate === "Invalid date") { + return ""; + } + + return parsedDate; +} + /** * Apply a filter to arg0 to remove hidden files names (starting with dot) * @function removeHidden