mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 18:25:24 +00:00
Splitted relevance method in beacons, locations, relevantDate and maxDistance as override
This commit is contained in:
89
src/pass.ts
89
src/pass.ts
@@ -233,59 +233,82 @@ export class Pass implements PassIndexSignature {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks and sets data for "beacons", "locations", "maxDistance" and "relevantDate" keys
|
* Sets current pass' relevancy through beacons
|
||||||
*
|
* @param data
|
||||||
* @method relevance
|
* @returns {Pass}
|
||||||
* @params type - one of the key above
|
|
||||||
* @params data - the data to be pushed to the property
|
|
||||||
* @return {Number} The quantity of data pushed
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
relevance(type: string, data: any) {
|
beacons(...data: schema.Beacon[]): this {
|
||||||
let types = ["beacons", "locations", "maxDistance", "relevantDate"];
|
if (!data.length) {
|
||||||
|
|
||||||
if (!type || !data || !types.includes(type)) {
|
|
||||||
return assignLength(0, this);
|
return assignLength(0, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === "beacons" || type === "locations") {
|
const validBeacons = data.reduce<schema.Beacon[]>((acc, current) => {
|
||||||
if (!(data instanceof Array)) {
|
if (!(Object.keys(current).length && schema.isValid(current, "locations"))) {
|
||||||
data = [data];
|
return acc;
|
||||||
}
|
}
|
||||||
|
|
||||||
let valid = data.filter(d => schema.isValid(d, type + "Dict"));
|
return [...acc, current];
|
||||||
|
}, []);
|
||||||
|
|
||||||
this._props[type] = valid.length ? valid : undefined;
|
if (!validBeacons.length) {
|
||||||
|
return assignLength(0, this);
|
||||||
return assignLength(valid.length, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === "maxDistance" && (typeof data === "string" || typeof data === "number")) {
|
this._props["beacons"] = validBeacons;
|
||||||
let conv = Number(data);
|
|
||||||
// condition to proceed
|
|
||||||
let cond = isNaN(conv);
|
|
||||||
|
|
||||||
if (!cond) {
|
return assignLength(validBeacons.length, this);
|
||||||
this._props[type] = conv;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets current pass' relevancy through locations
|
||||||
|
* @param data
|
||||||
|
* @returns {Pass}
|
||||||
|
*/
|
||||||
|
|
||||||
|
locations(...data: schema.Location[]): this {
|
||||||
|
if (!data.length) {
|
||||||
|
return assignLength(0, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return assignLength(Number(!cond), this);
|
const validLocations = data.reduce<schema.Location[]>((acc, current) => {
|
||||||
} else if (type === "relevantDate") {
|
if (!(Object.keys(current).length && schema.isValid(current, "locations"))) {
|
||||||
if (!(data instanceof Date)) {
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [...acc, current];
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (!validLocations.length) {
|
||||||
|
return assignLength(0, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._props["locations"] = validLocations;
|
||||||
|
|
||||||
|
return assignLength(validLocations.length, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets current pass' relevancy through a date
|
||||||
|
* @param data
|
||||||
|
* @returns {Pass}
|
||||||
|
*/
|
||||||
|
|
||||||
|
relevantDate(date: Date): this {
|
||||||
|
if (!(date instanceof Date)) {
|
||||||
genericDebug(formatMessage("DATE_FORMAT_UNMATCH", "Relevant Date"));
|
genericDebug(formatMessage("DATE_FORMAT_UNMATCH", "Relevant Date"));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
let dateParse = dateToW3CString(data);
|
const parsedDate = dateToW3CString(date);
|
||||||
|
|
||||||
if (!dateParse) {
|
if (!parsedDate) {
|
||||||
genericDebug(formatMessage("DATE_FORMAT_UNMATCH", "Relevant Date"));
|
// @TODO: create message "Unable to format date"
|
||||||
} else {
|
return this;
|
||||||
this._props[type] = dateParse;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return assignLength(Number(!!dateParse), this);
|
this._props["relevantDate"] = parsedDate;
|
||||||
}
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ interface OverridesSupportedOptions {
|
|||||||
labelColor?: string;
|
labelColor?: string;
|
||||||
groupingIdentifier?: string;
|
groupingIdentifier?: string;
|
||||||
suppressStripShine?: boolean;
|
suppressStripShine?: boolean;
|
||||||
|
maxDistance?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const supportedOptions = Joi.object().keys({
|
const supportedOptions = Joi.object().keys({
|
||||||
@@ -92,6 +93,7 @@ const supportedOptions = Joi.object().keys({
|
|||||||
groupingIdentifier: Joi.string(),
|
groupingIdentifier: Joi.string(),
|
||||||
suppressStripShine: Joi.boolean(),
|
suppressStripShine: Joi.boolean(),
|
||||||
logoText: Joi.string(),
|
logoText: Joi.string(),
|
||||||
|
maxDistance: Joi.number().positive(),
|
||||||
}).with("webServiceURL", "authenticationToken");
|
}).with("webServiceURL", "authenticationToken");
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user