Small Improvements to comments and code

This commit is contained in:
Alexander Cerutti
2019-06-23 16:45:06 +02:00
parent 18bacc8f47
commit 91c6ff1b94
2 changed files with 56 additions and 45 deletions

View File

@@ -62,14 +62,14 @@ export class Pass implements PassIndexSignature {
this.bundle = { ...options.model.bundle }; this.bundle = { ...options.model.bundle };
// Parsing the options and extracting only the valid ones. // Parsing the options and extracting only the valid ones.
const validOvverrides = schema.getValidated(options.overrides || {}, "supportedOptions") as schema.OverridesSupportedOptions; const validOverrides = schema.getValidated(options.overrides || {}, "supportedOptions") as schema.OverridesSupportedOptions;
if (validOvverrides === null) { if (validOverrides === null) {
throw new Error(formatMessage("OVV_KEYS_BADFORMAT")) throw new Error(formatMessage("OVV_KEYS_BADFORMAT"))
} }
if (Object.keys(validOvverrides).length) { if (Object.keys(validOverrides).length) {
this._props = { ...validOvverrides }; this._props = { ...validOverrides };
} }
try { try {
@@ -106,9 +106,8 @@ export class Pass implements PassIndexSignature {
/** /**
* Generates the pass Stream * Generates the pass Stream
* *
* @async
* @method generate * @method generate
* @return {Promise<Stream>} A Promise containing the stream of the generated pass. * @return A Stream of the generated pass.
*/ */
generate(): Stream { generate(): Stream {
@@ -117,6 +116,10 @@ export class Pass implements PassIndexSignature {
const finalBundle = { ...this.bundle } as schema.BundleUnit; const finalBundle = { ...this.bundle } as schema.BundleUnit;
/**
* Iterating through languages and generating pass.string file
*/
Object.keys(this.l10nTranslations).forEach(lang => { Object.keys(this.l10nTranslations).forEach(lang => {
const strings = generateStringFile(this.l10nTranslations[lang]); const strings = generateStringFile(this.l10nTranslations[lang]);
@@ -132,7 +135,7 @@ export class Pass implements PassIndexSignature {
} }
this.l10nBundles[lang]["pass.strings"] = Buffer.concat([ this.l10nBundles[lang]["pass.strings"] = Buffer.concat([
this.l10nBundles[lang]["pass.strings"] || Buffer.from("", "utf8"), this.l10nBundles[lang]["pass.strings"] || Buffer.alloc(0),
strings strings
]); ]);
} }
@@ -190,7 +193,8 @@ export class Pass implements PassIndexSignature {
* @method localize * @method localize
* @params lang - the ISO 3166 alpha-2 code for the language * @params lang - the ISO 3166 alpha-2 code for the language
* @params translations - key/value pairs where key is the * @params translations - key/value pairs where key is the
* string appearing in pass.json and value the translated string * placeholder in pass.json localizable strings
* and value the real translated string.
* @returns {this} * @returns {this}
* *
* @see https://apple.co/2KOv0OW - Passes support localization * @see https://apple.co/2KOv0OW - Passes support localization
@@ -205,7 +209,7 @@ export class Pass implements PassIndexSignature {
} }
/** /**
* Sets expirationDate property to the W3C date * Sets expirationDate property to a W3C-formatted date
* *
* @method expiration * @method expiration
* @params date * @params date
@@ -248,7 +252,7 @@ export class Pass implements PassIndexSignature {
*/ */
beacons(...data: schema.Beacon[]): PassWithLengthField { beacons(...data: schema.Beacon[]): PassWithLengthField {
if (!data.length) { if (!data || !data.length) {
return assignLength(0, this); return assignLength(0, this);
} }
@@ -367,9 +371,11 @@ export class Pass implements PassIndexSignature {
} else { } else {
const barcodes = [first, ...(data || [])]; const barcodes = [first, ...(data || [])];
// Stripping from the array not-object elements /**
// and the ones that does not pass validation. * Stripping from the array not-object elements
// Validation assign default value to missing parameters (if any). * and the ones that does not pass validation.
* Validation assign default value to missing parameters (if any).
*/
const valid = barcodes.reduce<schema.Barcode[]>((acc, current) => { const valid = barcodes.reduce<schema.Barcode[]>((acc, current) => {
if (!(current && current instanceof Object)) { if (!(current && current instanceof Object)) {
@@ -386,10 +392,12 @@ export class Pass implements PassIndexSignature {
}, []); }, []);
if (valid.length) { if (valid.length) {
// With this check, we want to avoid that /**
// PKBarcodeFormatCode128 gets chosen automatically * With this check, we want to avoid that
// if it is the first. If true, we'll get 1 * PKBarcodeFormatCode128 gets chosen automatically
// (so not the first index) * if it is the first. If true, we'll get 1
* (so not the first index)
*/
const barcodeFirstValidIndex = Number(valid[0].format === "PKBarcodeFormatCode128"); const barcodeFirstValidIndex = Number(valid[0].format === "PKBarcodeFormatCode128");
if (valid.length > 0 && valid[barcodeFirstValidIndex]) { if (valid.length > 0 && valid[barcodeFirstValidIndex]) {
@@ -416,18 +424,18 @@ export class Pass implements PassIndexSignature {
*/ */
private [barcodesFillMissing](): this { private [barcodesFillMissing](): this {
const props = this._props["barcodes"]; const { barcodes } = this._props;
if (props.length === 4 || !props.length) { if (barcodes.length === 4 || !barcodes.length) {
return assignLength(0, this, { return assignLength(0, this, {
autocomplete: noop, autocomplete: noop,
backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format) backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format)
}); });
} }
this._props["barcodes"] = barcodesFromUncompleteData(props[0].message); this._props["barcodes"] = barcodesFromUncompleteData(barcodes[0].message);
return assignLength(4 - props.length, this, { return assignLength(4 - barcodes.length, this, {
autocomplete: noop, autocomplete: noop,
backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format) backward: (format: schema.BarcodeFormat) => this[barcodesSetBackward](format)
}); });
@@ -443,31 +451,33 @@ export class Pass implements PassIndexSignature {
* @return {this} * @return {this}
*/ */
private [barcodesSetBackward](format: schema.BarcodeFormat | null): this { private [barcodesSetBackward](chosenFormat: schema.BarcodeFormat | null): this {
if (format === null) { let { barcode, barcodes } = this._props;
this._props["barcode"] = undefined;
if (chosenFormat === null) {
barcode = undefined;
return this; return this;
} }
if (typeof format !== "string") { if (typeof chosenFormat !== "string") {
barcodeDebug(formatMessage("BRC_FORMATTYPE_UNMATCH")); barcodeDebug(formatMessage("BRC_FORMATTYPE_UNMATCH"));
return this; return this;
} }
if (format === "PKBarcodeFormatCode128") { if (chosenFormat === "PKBarcodeFormatCode128") {
barcodeDebug(formatMessage("BRC_BW_FORMAT_UNSUPPORTED")); barcodeDebug(formatMessage("BRC_BW_FORMAT_UNSUPPORTED"));
return this; return this;
} }
// Checking which object among barcodes has the same format of the specified one. // Checking which object among barcodes has the same format of the specified one.
let index = this._props["barcodes"].findIndex(b => b.format.toLowerCase().includes(format.toLowerCase())); const index = barcodes.findIndex(b => b.format.toLowerCase().includes(chosenFormat.toLowerCase()));
if (index === -1) { if (index === -1) {
barcodeDebug(formatMessage("BRC_NOT_SUPPORTED")); barcodeDebug(formatMessage("BRC_NOT_SUPPORTED"));
return this; return this;
} }
this._props["barcode"] = this._props["barcodes"][index]; barcode = barcodes[index];
return this; return this;
} }
@@ -499,7 +509,7 @@ export class Pass implements PassIndexSignature {
*/ */
private _sign(manifest: { [key: string]: string }): Buffer { private _sign(manifest: { [key: string]: string }): Buffer {
let signature = forge.pkcs7.createSignedData(); const signature = forge.pkcs7.createSignedData();
signature.content = forge.util.createBuffer(JSON.stringify(manifest), "utf8"); signature.content = forge.util.createBuffer(JSON.stringify(manifest), "utf8");
@@ -565,9 +575,12 @@ export class Pass implements PassIndexSignature {
const passFile = JSON.parse(passCoreBuffer.toString()); const passFile = JSON.parse(passCoreBuffer.toString());
if (Object.keys(this._props).length) { if (Object.keys(this._props).length) {
// We filter the existing (in passFile) and non-valid keys from /*
// the below array keys that accept rgb values * We filter the existing (in passFile) and non-valid keys from
// and then delete it from the passFile. * the below array keys that accept rgb values
* and then delete it from the passFile.
*/
["backgroundColor", "foregroundColor", "labelColor"] ["backgroundColor", "foregroundColor", "labelColor"]
.filter(v => this._props[v] && !isValidRGB(this._props[v])) .filter(v => this._props[v] && !isValidRGB(this._props[v]))
.forEach(v => delete this._props[v]); .forEach(v => delete this._props[v]);
@@ -596,13 +609,14 @@ export class Pass implements PassIndexSignature {
return Buffer.from(JSON.stringify(passFile)); return Buffer.from(JSON.stringify(passFile));
} }
set transitType(v: string) { set transitType(value: string) {
if (schema.isValid(v, "transitType")) { if (!schema.isValid(value, "transitType")) {
this[transitType] = v; genericDebug(formatMessage("TRSTYPE_NOT_VALID", value));
} else {
genericDebug(formatMessage("TRSTYPE_NOT_VALID", v));
this[transitType] = this[transitType] || ""; this[transitType] = this[transitType] || "";
return;
} }
this[transitType] = value;
} }
get transitType(): string { get transitType(): string {
@@ -613,12 +627,9 @@ 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 barcodesFromUncompleteData
* @params data - common info, may be object or the message itself * @params message - the content to be placed inside "message" field
* @params data.message - the content to be placed inside "message" field * @return Array of barcodeDict compliant
* @params [data.altText=data.message] - alternativeText, is message content if not overwritten
* @params [data.messageEncoding=iso-8859-1] - the encoding
* @return Object array barcodeDict compliant
*/ */
function barcodesFromUncompleteData(message: string): schema.Barcode[] { function barcodesFromUncompleteData(message: string): schema.Barcode[] {

View File

@@ -13,7 +13,7 @@ export interface Certificates {
} }
export interface FactoryOptions { export interface FactoryOptions {
model: { [key: string]: Buffer } | string; model: BundleUnit | string;
certificates: Certificates; certificates: Certificates;
overrides?: Object; overrides?: Object;
} }