Added setExpirationDate implementation along with tests

This commit is contained in:
Alexander Cerutti
2021-09-21 00:02:03 +02:00
parent 389aa96532
commit 208faee460
4 changed files with 66 additions and 19 deletions

View File

@@ -256,10 +256,17 @@ export default class PKPass extends Bundle {
* @returns
*/
setExpiration(date: Date | null): this {
/**
* @TODO implement
*/
setExpirationDate(date: Date | null): this {
if (date === null) {
delete this[propsSymbol]["expirationDate"];
return this;
}
const parsedDate = processDate("expirationDate", date);
if (parsedDate) {
this[propsSymbol]["expirationDate"] = parsedDate;
}
return this;
}

View File

@@ -16,6 +16,7 @@ import {
getAllFilesWithName,
} from "./utils";
import * as Signature from "./signature";
import { processDate } from "./processDate";
const barcodeDebug = debug("passkit:barcode");
const genericDebug = debug("passkit:generic");
@@ -687,18 +688,3 @@ function getValidInArray<T>(
Object.keys(current).length && Schemas.isValid(current, schemaName),
);
}
function processDate(key: string, date: Date): string | null {
if (!(date instanceof Date)) {
return null;
}
const dateParse = dateToW3CString(date);
if (!dateParse) {
genericDebug(formatMessage(DEBUG.DATE_FORMAT_UNMATCH, key));
return null;
}
return dateParse;
}

17
src/processDate.ts Normal file
View File

@@ -0,0 +1,17 @@
import { dateToW3CString } from "./utils";
import formatMessage, { ERROR, DEBUG } from "./messages";
export function processDate(key: string, date: Date): string | null {
if (!(date instanceof Date)) {
return null;
}
const dateParse = dateToW3CString(date);
if (!dateParse) {
console.warn(formatMessage(DEBUG.DATE_FORMAT_UNMATCH, key));
return null;
}
return dateParse;
}