From b410a435dfff630fe9e20bf22964f6e12cfb8dda Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sat, 29 Jun 2019 17:50:50 +0200 Subject: [PATCH] Unified date processing of expiration and relevantDate --- src/pass.ts | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/pass.ts b/src/pass.ts index 7320639..790ec85 100644 --- a/src/pass.ts +++ b/src/pass.ts @@ -243,15 +243,12 @@ export class Pass implements PassIndexSignature { return this; } - const dateParse = dateToW3CString(date); + const parsedDate = processDate("expirationDate", date); - if (!dateParse) { - genericDebug(formatMessage("DATE_FORMAT_UNMATCH", "Expiration date")); - return this; + if (parsedDate) { + this._props["expirationDate"] = parsedDate; } - this._props["expirationDate"] = dateParse; - return this; } @@ -349,19 +346,12 @@ export class Pass implements PassIndexSignature { return this; } - if (!(date instanceof Date)) { - genericDebug(formatMessage("DATE_FORMAT_UNMATCH", "Relevant Date")); - return this; + const parsedDate = processDate("relevandDate", date); + + if (parsedDate) { + this._props["relevantDate"] = parsedDate; } - const parsedDate = dateToW3CString(date); - - if (!parsedDate) { - // @TODO: create message "Unable to format date" - return this; - } - - this._props["relevantDate"] = parsedDate; return this; } @@ -693,3 +683,18 @@ function barcodesFromUncompleteData(message: string): schema.Barcode[] { "PKBarcodeFormatCode128" ].map(format => schema.getValidated({ format, message }, "barcode")); } + +function processDate(key: string, date: Date): string | null { + if (!(date instanceof Date)) { + return null; + } + + const dateParse = dateToW3CString(date); + + if (!dateParse) { + genericDebug(formatMessage("DATE_FORMAT_UNMATCH", key)); + return null; + } + + return dateParse; +}