Recreated Barcodes functions to accept a string and an object

This commit is contained in:
Alexander Cerutti
2019-06-15 01:21:26 +02:00
parent f9206bafac
commit 5c5e573fde

View File

@@ -280,28 +280,38 @@ export class Pass implements PassIndexSignature {
/** /**
* Adds barcodes to "barcode" and "barcodes" properties. * Adds barcodes to "barcode" and "barcodes" properties.
* It will let later to add the missing versions * It will let to add the missing versions later.
* *
* @method barcode * @method barcode
* @params {Object|String} data - the data to be added * @params data - the data to be added
* @return {this} Improved this with length property and other methods * @return {this} Improved this with length property and other methods
*/ */
barcode(data) { barcode(first: string | schema.Barcode, ...data: schema.Barcode[]): this {
if (!data) { const isFirstParameterValid = (
first && (
typeof first === "string" && first.length || (
typeof first === "object" &&
first.hasOwnProperty("message")
)
)
);
if (!isFirstParameterValid) {
return assignLength(0, this, { return assignLength(0, this, {
autocomplete: noop, autocomplete: noop,
backward: noop, backward: noop,
}); });
} }
if (typeof data === "string" || (data instanceof Object && !Array.isArray(data) && !data.format && data.message)) { if (typeof first === "string") {
const autogen = barcodesFromUncompleteData(data instanceof Object ? data : { message: data }); const autogen = barcodesFromUncompleteData(first);
if (!autogen.length) { if (!autogen.length) {
barcodeDebug(formatMessage("BRC_AUTC_MISSING_DATA"));
return assignLength(0, this, { return assignLength(0, this, {
autocomplete: noop, autocomplete: noop,
backward: noop backward: noop,
}); });
} }
@@ -310,20 +320,17 @@ export class Pass implements PassIndexSignature {
return assignLength(autogen.length, this, { return assignLength(autogen.length, this, {
autocomplete: noop, autocomplete: noop,
backward: (format) => this[barcodesSetBackward](format) backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format)
}); });
} } else {
const barcodes = [first, ...(data || [])];
if (!(data instanceof Array)) { // Stripping from the array not-object elements
data = [data];
}
// Stripping from the array not-object elements, objects with no message
// 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 = data.reduce((acc, current) => { const valid = barcodes.reduce<schema.Barcode[]>((acc, current) => {
if (!(current && current instanceof Object && current.hasOwnProperty("message"))) { if (!(current && current instanceof Object)) {
return acc; return acc;
} }
@@ -333,7 +340,7 @@ export class Pass implements PassIndexSignature {
return acc; return acc;
} }
return [...acc, validated]; return [...acc, validated] as schema.Barcode[];
}, []); }, []);
if (valid.length) { if (valid.length) {
@@ -343,7 +350,7 @@ export class Pass implements PassIndexSignature {
// (so not the first index) // (so not the first index)
const barcodeFirstValidIndex = Number(valid[0].format === "PKBarcodeFormatCode128"); const barcodeFirstValidIndex = Number(valid[0].format === "PKBarcodeFormatCode128");
if (valid.length > 0) { if (valid.length > 0 && valid[barcodeFirstValidIndex]) {
this._props["barcode"] = valid[barcodeFirstValidIndex]; this._props["barcode"] = valid[barcodeFirstValidIndex];
} }
@@ -352,9 +359,10 @@ export class Pass implements PassIndexSignature {
return assignLength(valid.length, this, { return assignLength(valid.length, this, {
autocomplete: () => this[barcodesFillMissing](), autocomplete: () => this[barcodesFillMissing](),
backward: (format) => this[barcodesSetBackward](format), backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format),
}); });
} }
}
/** /**
* Given an already compiled props["barcodes"] with missing objects * Given an already compiled props["barcodes"] with missing objects
@@ -366,20 +374,20 @@ export class Pass implements PassIndexSignature {
*/ */
[barcodesFillMissing]() { [barcodesFillMissing]() {
let props = this._props["barcodes"]; const props = this._props["barcodes"];
if (props.length === 4 || !props.length) { if (props.length === 4 || !props.length) {
return assignLength(0, this, { return assignLength(0, this, {
autocomplete: noop, autocomplete: noop,
backward: (format) => this[barcodesSetBackward](format) backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format)
}); });
} }
this._props["barcodes"] = barcodesFromUncompleteData(props[0]); this._props["barcodes"] = barcodesFromUncompleteData(props[0].message);
return assignLength(4 - props.length, this, { return assignLength(4 - props.length, this, {
autocomplete: noop, autocomplete: noop,
backward: (format) => this[barcodesSetBackward](format) backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format)
}); });
} }
@@ -389,11 +397,11 @@ export class Pass implements PassIndexSignature {
* property "barcode". * property "barcode".
* *
* @method Symbol/barcodesSetBackward * @method Symbol/barcodesSetBackward
* @params {String} format - the format, or part of it, to be used * @params format - the format to be used
* @return {this} * @return {this}
*/ */
[barcodesSetBackward](format) { [barcodesSetBackward](format: schema.BarcodeFormat | null): this {
if (format === null) { if (format === null) {
this._props["barcode"] = undefined; this._props["barcode"] = undefined;
return this; return this;
@@ -564,16 +572,15 @@ export class Pass implements PassIndexSignature {
* Automatically generates barcodes for all the types given common info * Automatically generates barcodes for all the types given common info
* *
* @method barcodesFromMessage * @method barcodesFromMessage
* @params {Object} data - common info, may be object or the message itself * @params data - common info, may be object or the message itself
* @params {String} data.message - the content to be placed inside "message" field * @params data.message - the content to be placed inside "message" field
* @params {String} [data.altText=data.message] - alternativeText, is message content if not overwritten * @params [data.altText=data.message] - alternativeText, is message content if not overwritten
* @params {String} [data.messageEncoding=iso-8859-1] - the encoding * @params [data.messageEncoding=iso-8859-1] - the encoding
* @return {Object[]} Object array barcodeDict compliant * @return Object array barcodeDict compliant
*/ */
function barcodesFromUncompleteData(origin: schema.Barcode): schema.Barcode[] { function barcodesFromUncompleteData(message: string): schema.Barcode[] {
if (!(origin.message && typeof origin.message === "string")) { if (!(message && typeof message === "string")) {
barcodeDebug(formatMessage("BRC_AUTC_MISSING_DATA"));
return []; return [];
} }
@@ -582,10 +589,5 @@ function barcodesFromUncompleteData(origin: schema.Barcode): schema.Barcode[] {
"PKBarcodeFormatPDF417", "PKBarcodeFormatPDF417",
"PKBarcodeFormatAztec", "PKBarcodeFormatAztec",
"PKBarcodeFormatCode128" "PKBarcodeFormatCode128"
].map(format => ].map(format => schema.getValidated({ format, message }, "barcode"));
schema.getValidated(
Object.assign({}, origin, { format }),
"barcode"
)
);
} }