Removed assignLength;

Removed passing of undefined to get the current value;
This commit is contained in:
Alexander Cerutti
2019-06-29 18:09:02 +02:00
parent 003b584221
commit 5babdf6854

View File

@@ -214,11 +214,7 @@ export class Pass implements PassIndexSignature {
* @see https://apple.co/2KOv0OW - Passes support localization * @see https://apple.co/2KOv0OW - Passes support localization
*/ */
localize(lang?: string, translations?: { [key: string]: string }): this | string[] { localize(lang?: string, translations?: { [key: string]: string }): this {
if (lang === undefined && translations === undefined) {
return Object.keys(this.l10nTranslations);
}
if (lang && typeof lang === "string" && (typeof translations === "object" || translations === undefined)) { if (lang && typeof lang === "string" && (typeof translations === "object" || translations === undefined)) {
this.l10nTranslations[lang] = translations || {}; this.l10nTranslations[lang] = translations || {};
} }
@@ -235,11 +231,8 @@ export class Pass implements PassIndexSignature {
*/ */
expiration(date?: Date): this | string { expiration(date?: Date): this | string {
if (date === undefined) { if (date === null) {
return this._props["expirationDate"]; delete this._props["expirationDate"];
}
if (!(date instanceof Date)) {
return this; return this;
} }
@@ -270,10 +263,10 @@ export class Pass implements PassIndexSignature {
* @returns {Pass} * @returns {Pass}
*/ */
beacons(...data: schema.Beacon[]): PassWithLengthField | schema.Beacon[] { beacons(...data: schema.Beacon[] | null): this {
if (data === null) { if (data === null) {
delete this._props["beacons"]; delete this._props["beacons"];
return assignLength<PassWithLengthField>(0, this); return this;
} }
const valid = processRelevancySet("beacons", data); const valid = processRelevancySet("beacons", data);
@@ -282,28 +275,19 @@ export class Pass implements PassIndexSignature {
this._props["beacons"] = valid; this._props["beacons"] = valid;
} }
return [...acc, current]; return this;
}, []);
if (!validBeacons.length) {
return assignLength(0, this);
} }
(this._props["beacons"] || (this._props["beacons"] = [])).push(...validBeacons);
return assignLength(this._props["beacons"].length, this);
}
/** /**
* Sets current pass' relevancy through locations * Sets current pass' relevancy through locations
* @param data * @param data
* @returns {Pass} * @returns {Pass}
*/ */
locations(...data: schema.Location[]): PassWithLengthField | schema.Location[] { locations(...data: schema.Location[]): this {
if (data === null) { if (data === null) {
delete this._props["locations"]; delete this._props["locations"];
return assignLength<PassWithLengthField>(0, this); return this;
} }
const valid = processRelevancySet("locations", data); const valid = processRelevancySet("locations", data);
@@ -312,18 +296,9 @@ export class Pass implements PassIndexSignature {
this._props["locations"] = valid; this._props["locations"] = valid;
} }
return [...acc, current]; return this;
}, []);
if (!validLocations.length) {
return assignLength(0, this);
} }
(this._props["locations"] || (this._props["locations"] = [])).push(...validLocations);
return assignLength(this._props["locations"].length, this);
}
/** /**
* Sets current pass' relevancy through a date * Sets current pass' relevancy through a date
* @param data * @param data
@@ -331,10 +306,6 @@ export class Pass implements PassIndexSignature {
*/ */
relevantDate(date?: Date): this | string { relevantDate(date?: Date): this | string {
if (date === undefined) {
return this._props["relevantDate"];
}
if (date === null) { if (date === null) {
delete this._props["relevantDate"]; delete this._props["relevantDate"];
return this; return this;
@@ -358,22 +329,15 @@ export class Pass implements PassIndexSignature {
* @return {this} Improved this with length property and other methods * @return {this} Improved this with length property and other methods
*/ */
barcode(first?: string | schema.Barcode, ...data: schema.Barcode[]): PassWithBarcodeMethods | schema.Barcode[] { barcode(first?: string | schema.Barcode, ...data: schema.Barcode[]): this {
if (first === undefined && (data === undefined || !data.length)) {
return this._props["barcodes"];
}
if (first === null) { if (first === null) {
delete this._props["barcodes"]; delete this._props["barcodes"];
return assignLength<PassWithBarcodeMethods>(0, this, { return this;
autocomplete: noop,
backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format),
});
} }
const isFirstParameterValid = ( const isFirstParameterValid = (
first && ( first && (
typeof first === "string" && first.length || ( typeof first === "string" || (
typeof first === "object" && typeof first === "object" &&
first.hasOwnProperty("message") first.hasOwnProperty("message")
) )
@@ -381,10 +345,7 @@ export class Pass implements PassIndexSignature {
); );
if (!isFirstParameterValid) { if (!isFirstParameterValid) {
return assignLength(0, this, { return this;
autocomplete: noop,
backward: noop,
});
} }
if (typeof first === "string") { if (typeof first === "string") {
@@ -392,19 +353,13 @@ export class Pass implements PassIndexSignature {
if (!autogen.length) { if (!autogen.length) {
barcodeDebug(formatMessage("BRC_AUTC_MISSING_DATA")); barcodeDebug(formatMessage("BRC_AUTC_MISSING_DATA"));
return assignLength<PassWithBarcodeMethods>(0, this, { return this;
autocomplete: noop,
backward: noop,
});
} }
this._props["barcode"] = autogen[0]; this._props["barcode"] = autogen[0];
this._props["barcodes"] = autogen; this._props["barcodes"] = autogen;
return assignLength<PassWithBarcodeMethods>(autogen.length, this, { return this;
autocomplete: noop,
backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format)
});
} else { } else {
const barcodes = [first, ...(data || [])]; const barcodes = [first, ...(data || [])];
@@ -444,10 +399,7 @@ export class Pass implements PassIndexSignature {
this._props["barcodes"] = valid; this._props["barcodes"] = valid;
} }
return assignLength<PassWithBarcodeMethods>(valid.length, this, { return this;
autocomplete: () => this[barcodesFillMissing](),
backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format),
});
} }
} }