Added function assignLength to wrap new objects with length property

This commit is contained in:
Alexander Cerutti
2019-01-20 18:54:24 +01:00
parent e4b3436b46
commit b32c2efcd0

View File

@@ -289,9 +289,7 @@ class Pass {
let types = ["beacons", "locations", "maxDistance", "relevantDate"]; let types = ["beacons", "locations", "maxDistance", "relevantDate"];
if (!type || !data || !types.includes(type)) { if (!type || !data || !types.includes(type)) {
return Object.assign({ return assignLength(0, this);
length: 0
}, this);
} }
if (type === "beacons" || type === "locations") { if (type === "beacons" || type === "locations") {
@@ -303,9 +301,7 @@ class Pass {
this._props[type] = valid.length ? valid : undefined; this._props[type] = valid.length ? valid : undefined;
return Object.assign({ return assignLength(valid.length, this);
length: valid.length
}, this);
} }
if (type === "maxDistance" && (typeof data === "string" || typeof data === "number")) { if (type === "maxDistance" && (typeof data === "string" || typeof data === "number")) {
@@ -317,9 +313,7 @@ class Pass {
this._props[type] = conv; this._props[type] = conv;
} }
return Object.assign({ return assignLength(Number(!cond), this);
length: Number(!cond)
}, this);
} else if (type === "relevantDate") { } else if (type === "relevantDate") {
let dateParse = dateToW3CString(data, relevanceDateFormat); let dateParse = dateToW3CString(data, relevanceDateFormat);
@@ -329,9 +323,7 @@ class Pass {
this._props[type] = dateParse; this._props[type] = dateParse;
} }
return Object.assign({ return assignLength(Number(!!dateParse), this);
length: Number(!!dateParse)
}, this);
} }
} }
@@ -346,11 +338,10 @@ class Pass {
barcode(data) { barcode(data) {
if (!data) { if (!data) {
return Object.assign({ return assignLength(0, this, {
length: 0,
autocomplete: noop, autocomplete: noop,
backward: noop backward: noop,
}, this); });
} }
if (typeof data === "string" || (data instanceof Object && !data.format && data.message)) { if (typeof data === "string" || (data instanceof Object && !data.format && data.message)) {
@@ -359,11 +350,13 @@ class Pass {
this._props["barcode"] = autogen[0] || {}; this._props["barcode"] = autogen[0] || {};
this._props["barcodes"] = autogen || []; this._props["barcodes"] = autogen || [];
return Object.assign({ // I bind "this" to get a clean context (without these two methods)
length: 4, // when returning from the methods
return assignLength(4, this, {
autocomplete: noop, autocomplete: noop,
backward: this.__barcodeChooseBackward.bind(this) backward: this.__barcodeChooseBackward.bind(this)
}, this); });
} }
if (!(data instanceof Array)) { if (!(data instanceof Array)) {
@@ -384,13 +377,13 @@ class Pass {
this._props["barcodes"] = valid; this._props["barcodes"] = valid;
} }
// I bind "this" to get a clean context (without these two methods) when returning from the methods // I bind "this" to get a clean context (without these two methods)
// when returning from the methods
return Object.assign({ return assignLength(valid.length, this, {
length: valid.length,
autocomplete: this.__barcodeAutocomplete.bind(this), autocomplete: this.__barcodeAutocomplete.bind(this),
backward: this.__barcodeChooseBackward.bind(this) backward: this.__barcodeChooseBackward.bind(this)
}, this); });
} }
/** /**
@@ -432,18 +425,22 @@ class Pass {
let props = this._props["barcodes"]; let props = this._props["barcodes"];
if (props.length === 4 || !props.length) { if (props.length === 4 || !props.length) {
return Object.assign({ // I bind "this" to get a clean context (without these two methods)
length: 0, // when returning from the methods
return assignLength(0, this, {
backward: this.__barcodeChooseBackward.bind(this) backward: this.__barcodeChooseBackward.bind(this)
}, this); });
} }
this._props["barcodes"] = this.__barcodeAutogen(props[0]); this._props["barcodes"] = this.__barcodeAutogen(props[0]);
return Object.assign({ // I bind "this" to get a clean context (without these two methods)
length: 4 - props.length, // when returning from the methods
return assignLength(4 - props.length, this, {
backward: this.__barcodeChooseBackward.bind(this) backward: this.__barcodeChooseBackward.bind(this)
}, this); });
} }
/** /**
@@ -848,4 +845,14 @@ function generateStringFile(lang) {
return Buffer.from(strings.join("\n"), "utf8"); return Buffer.from(strings.join("\n"), "utf8");
} }
/**
* Creates a new object with custom length property
* @param {number} value - the length
* @param {Array<Object<string, any>>} source - the main sources of properties
*/
function assignLength(value, ...sources) {
return Object.assign({ length: value }, ...sources);
}
module.exports = { Pass }; module.exports = { Pass };