Added support to fetching current values of non-overrides props

This commit is contained in:
Alexander Cerutti
2019-06-27 23:43:29 +02:00
parent 9f924bbdcd
commit 05193aa32a
2 changed files with 67 additions and 20 deletions

View File

@@ -214,7 +214,11 @@ 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 { localize(lang?: string, translations?: { [key: string]: string }): this | string[] {
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 || {};
} }
@@ -230,7 +234,11 @@ export class Pass implements PassIndexSignature {
* @returns {this} * @returns {this}
*/ */
expiration(date: Date): this { expiration(date?: Date): this | string {
if (date === undefined) {
return this._props["expirationDate"];
}
if (!(date instanceof Date)) { if (!(date instanceof Date)) {
return this; return this;
} }
@@ -265,8 +273,13 @@ export class Pass implements PassIndexSignature {
* @returns {Pass} * @returns {Pass}
*/ */
beacons(...data: schema.Beacon[]): PassWithLengthField { beacons(...data: schema.Beacon[]): PassWithLengthField | schema.Beacon[] {
if (!data || !data.length) { if (data === undefined) {
return this._props["beacons"];
}
if (!data.length) {
this._props["beacons"] = [];
return assignLength(0, this); return assignLength(0, this);
} }
@@ -282,9 +295,9 @@ export class Pass implements PassIndexSignature {
return assignLength(0, this); return assignLength(0, this);
} }
this._props["beacons"] = validBeacons; (this._props["beacons"] || (this._props["beacons"] = [])).push(...validBeacons);
return assignLength(validBeacons.length, this); return assignLength(this._props["beacons"].length, this);
} }
/** /**
@@ -293,8 +306,13 @@ export class Pass implements PassIndexSignature {
* @returns {Pass} * @returns {Pass}
*/ */
locations(...data: schema.Location[]): PassWithLengthField { locations(...data: schema.Location[]): PassWithLengthField | schema.Location[] {
if (data === undefined) {
return this._props["locations"];
}
if (!data.length) { if (!data.length) {
this._props["locations"] = [];
return assignLength(0, this); return assignLength(0, this);
} }
@@ -310,9 +328,11 @@ export class Pass implements PassIndexSignature {
return assignLength(0, this); return assignLength(0, this);
} }
this._props["locations"] = validLocations; console.log("Locations:", this._props["locations"]);
return assignLength(validLocations.length, this); (this._props["locations"] || (this._props["locations"] = [])).push(...validLocations);
return assignLength(this._props["locations"].length, this);
} }
/** /**
@@ -321,7 +341,16 @@ export class Pass implements PassIndexSignature {
* @returns {Pass} * @returns {Pass}
*/ */
relevantDate(date: Date): this { relevantDate(date?: Date): this | string {
if (date === undefined) {
return this._props["relevantDate"];
}
if (date === null) {
delete this._props["relevantDate"];
return this;
}
if (!(date instanceof Date)) { if (!(date instanceof Date)) {
genericDebug(formatMessage("DATE_FORMAT_UNMATCH", "Relevant Date")); genericDebug(formatMessage("DATE_FORMAT_UNMATCH", "Relevant Date"));
return this; return this;
@@ -347,7 +376,20 @@ 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 { barcode(first?: string | schema.Barcode, ...data: schema.Barcode[]): PassWithBarcodeMethods | schema.Barcode[] {
console.log(first, data);
if (first === undefined && (data === undefined || !data.length)) {
return this._props["barcodes"];
}
if (first === null) {
delete this._props["barcodes"];
return assignLength<PassWithBarcodeMethods>(0, this, {
autocomplete: noop,
backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format),
});
}
const isFirstParameterValid = ( const isFirstParameterValid = (
first && ( first && (
typeof first === "string" && first.length || ( typeof first === "string" && first.length || (
@@ -369,7 +411,7 @@ 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(0, this, { return assignLength<PassWithBarcodeMethods>(0, this, {
autocomplete: noop, autocomplete: noop,
backward: noop, backward: noop,
}); });
@@ -378,7 +420,7 @@ export class Pass implements PassIndexSignature {
this._props["barcode"] = autogen[0]; this._props["barcode"] = autogen[0];
this._props["barcodes"] = autogen; this._props["barcodes"] = autogen;
return assignLength(autogen.length, this, { return assignLength<PassWithBarcodeMethods>(autogen.length, this, {
autocomplete: noop, autocomplete: noop,
backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format) backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format)
}); });
@@ -421,7 +463,7 @@ export class Pass implements PassIndexSignature {
this._props["barcodes"] = valid; this._props["barcodes"] = valid;
} }
return assignLength(valid.length, this, { return assignLength<PassWithBarcodeMethods>(valid.length, this, {
autocomplete: () => this[barcodesFillMissing](), autocomplete: () => this[barcodesFillMissing](),
backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format), backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format),
}); });
@@ -437,7 +479,7 @@ export class Pass implements PassIndexSignature {
* @returns {this} Improved this, with length property and retroCompatibility method. * @returns {this} Improved this, with length property and retroCompatibility method.
*/ */
private [barcodesFillMissing](): this { private [barcodesFillMissing](): PassWithBarcodeMethods {
const { barcodes } = this._props; const { barcodes } = this._props;
if (barcodes.length === 4 || !barcodes.length) { if (barcodes.length === 4 || !barcodes.length) {
@@ -449,7 +491,7 @@ export class Pass implements PassIndexSignature {
this._props["barcodes"] = barcodesFromUncompleteData(barcodes[0].message); this._props["barcodes"] = barcodesFromUncompleteData(barcodes[0].message);
return assignLength(4 - barcodes.length, this, { return assignLength<PassWithBarcodeMethods>(4 - barcodes.length, this, {
autocomplete: noop, autocomplete: noop,
backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format) backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format)
}); });
@@ -466,10 +508,10 @@ export class Pass implements PassIndexSignature {
*/ */
private [barcodesSetBackward](chosenFormat: schema.BarcodeFormat | null): this { private [barcodesSetBackward](chosenFormat: schema.BarcodeFormat | null): this {
let { barcode, barcodes } = this._props; let { barcodes } = this._props;
if (chosenFormat === null) { if (chosenFormat === null) {
barcode = undefined; this._props["barcode"] = undefined;
return this; return this;
} }
@@ -491,7 +533,7 @@ export class Pass implements PassIndexSignature {
return this; return this;
} }
barcode = barcodes[index]; this._props["barcode"] = barcodes[index];
return this; return this;
} }
@@ -503,7 +545,11 @@ export class Pass implements PassIndexSignature {
* @returns {this} * @returns {this}
*/ */
nfc(data: schema.NFC): this { nfc(data?: schema.NFC): this | schema.NFC {
if (data === undefined) {
return this._props["nfc"];
}
if (!(typeof data === "object" && !Array.isArray(data) && schema.isValid(data, "nfcDict"))) { if (!(typeof data === "object" && !Array.isArray(data) && schema.isValid(data, "nfcDict"))) {
genericDebug("Invalid NFC data provided"); genericDebug("Invalid NFC data provided");
return this; return this;

View File

@@ -75,6 +75,7 @@ export interface OverridesSupportedOptions {
labelColor?: string; labelColor?: string;
groupingIdentifier?: string; groupingIdentifier?: string;
suppressStripShine?: boolean; suppressStripShine?: boolean;
logoText?: string;
maxDistance?: number; maxDistance?: number;
} }