Added setBarcodes implementation along with tests

This commit is contained in:
Alexander Cerutti
2021-09-21 21:34:28 +02:00
parent d3eb66b2c8
commit c49aad4098
2 changed files with 162 additions and 6 deletions

View File

@@ -392,12 +392,51 @@ export default class PKPass extends Bundle {
* @returns
*/
setBarcodes(...barcodes: Schemas.Barcode[]): this {
/**
* @TODO implement
* @TODO implement data completion
* @TODO specify a way to get current ones deleted
*/
setBarcodes(barcodes: null): this;
setBarcodes(message: string): this;
setBarcodes(...barcodes: Schemas.Barcode[]): this;
setBarcodes(...barcodes: (Schemas.Barcode | string | null)[]): this {
if (!barcodes.length) {
return this;
}
if (barcodes[0] === null) {
delete this[propsSymbol]["barcodes"];
return this;
}
let finalBarcodes: Schemas.Barcode[];
if (typeof barcodes[0] === "string") {
/** A string has been received instead of objects. We can only auto-fill them all with the same data. */
const supportedFormats: Array<Schemas.BarcodeFormat> = [
"PKBarcodeFormatQR",
"PKBarcodeFormatPDF417",
"PKBarcodeFormatAztec",
"PKBarcodeFormatCode128",
];
finalBarcodes = supportedFormats.map((format) =>
Schemas.getValidated(
{ format, message: barcodes[0] } as Schemas.Barcode,
Schemas.Barcode,
),
);
} else {
finalBarcodes = Schemas.filterValid(
barcodes as Schemas.Barcode[],
Schemas.Barcode,
);
if (!finalBarcodes.length) {
throw new TypeError(
"Expected Schema.Barcode in setBarcodes but no one is valid.",
);
}
}
this[propsSymbol]["barcodes"] = finalBarcodes;
return this;
}