Improvements to barcodes signature and generation (removed overhead)

This commit is contained in:
Alexander Cerutti
2019-12-14 16:46:29 +01:00
parent f72f2a08b0
commit d26af1004b

View File

@@ -293,7 +293,7 @@ export class Pass {
beacons(resetFlag: null): this; beacons(resetFlag: null): this;
beacons(...data: schema.Beacon[]): this beacons(...data: schema.Beacon[]): this
beacons(...data: (schema.Beacon|null)[]){ beacons(...data: (schema.Beacon|null)[]): this {
if (data[0] === null) { if (data[0] === null) {
delete this[passProps]["beacons"]; delete this[passProps]["beacons"];
return this; return this;
@@ -363,27 +363,17 @@ export class Pass {
* @return {this} Improved this with length property and other methods * @return {this} Improved this with length property and other methods
*/ */
barcodes(first: null | string | schema.Barcode, ...data: schema.Barcode[]): this { barcodes(resetFlag: null): this;
if (first === null) { barcodes(message: string): this;
barcodes(...data: schema.Barcode[]): this;
barcodes(...data: (schema.Barcode|null|string)[]): this {
if (data[0] === null) {
delete this[passProps]["barcodes"]; delete this[passProps]["barcodes"];
return this; return this;
} }
const isFirstParameterValid = ( if (typeof data[0] === "string") {
first && ( const autogen = barcodesFromUncompleteData(data[0]);
typeof first === "string" || (
typeof first === "object" &&
first.hasOwnProperty("message")
)
)
);
if (!isFirstParameterValid) {
return this;
}
if (typeof first === "string") {
const autogen = barcodesFromUncompleteData(first);
if (!autogen.length) { if (!autogen.length) {
barcodeDebug(formatMessage("BRC_AUTC_MISSING_DATA")); barcodeDebug(formatMessage("BRC_AUTC_MISSING_DATA"));
@@ -394,15 +384,13 @@ export class Pass {
return this; return this;
} else { } else {
const barcodes = [first, ...(data || [])];
/** /**
* Stripping from the array not-object elements * Stripping from the array not-object elements
* and the ones that does not pass validation. * and the ones that does not pass validation.
* Validation assign default value to missing parameters (if any). * Validation assign default value to missing parameters (if any).
*/ */
const valid = barcodes.reduce<schema.Barcode[]>((acc, current) => { const validBarcodes = data.reduce<schema.Barcode[]>((acc, current) => {
if (!(current && current instanceof Object)) { if (!(current && current instanceof Object)) {
return acc; return acc;
} }
@@ -416,8 +404,8 @@ export class Pass {
return [...acc, validated] as schema.Barcode[]; return [...acc, validated] as schema.Barcode[];
}, []); }, []);
if (valid.length) { if (validBarcodes.length) {
this[passProps]["barcodes"] = valid; this[passProps]["barcodes"] = validBarcodes;
} }
return this; return this;