Added new method setRelevantDates

This commit is contained in:
Alexander Cerutti
2025-01-09 00:17:19 +01:00
parent c5160c0fc9
commit 6462a852fc
4 changed files with 184 additions and 33 deletions

View File

@@ -956,6 +956,54 @@ export default class PKPass extends Bundle {
);
}
/**
* Allows setting a series of relevancy intervals or
* relevancy entries for the pass.
*
* @param {Schemas.RelevantDate[] | null} relevancyEntries
* @returns {void}
*/
public setRelevantDates(
relevancyEntries: Schemas.RelevantDate[] | null,
): void {
Utils.assertUnfrozen(this);
if (relevancyEntries === null) {
this[propsSymbol]["relevantDates"] = undefined;
return;
}
const processedDateEntries = relevancyEntries.reduce<
Schemas.RelevantDate[]
>((acc, entry) => {
try {
Schemas.validate(Schemas.RelevantDate, entry);
if (isRelevantEntry(entry)) {
acc.push({
relevantDate: Utils.processDate(
new Date(entry.relevantDate),
),
});
return acc;
}
acc.push({
startDate: Utils.processDate(new Date(entry.startDate)),
endDate: Utils.processDate(new Date(entry.endDate)),
});
} catch (err) {
console.warn(new TypeError(Messages.RELEVANT_DATE.INVALID));
}
return acc;
}, []);
this[propsSymbol]["relevantDates"] = processedDateEntries;
}
/**
* Allows setting a relevant date in which the OS
* should show this pass.
@@ -964,6 +1012,13 @@ export default class PKPass extends Bundle {
*
* @param {Date | null} date
* @throws if pass is frozen due to previous export
*
* @warning `relevantDate` property has been deprecated in iOS 18
* in order to get replaced by `relevantDates` array of intervals
* (`relevantDates[].startDate` + `relevantDates[].endDate`)
* or single date (`relevantDates[].relevantDate`). This method will
* set both the original, as the new one will get ignored in older
* iOS versions.
*/
public setRelevantDate(date: Date | null): void {
@@ -1086,3 +1141,9 @@ function validateJSONBuffer(
return Schemas.validate(schema, contentAsJSON);
}
function isRelevantEntry(
entry: Schemas.RelevantDate,
): entry is Schemas.RelevancyEntry {
return Object.prototype.hasOwnProperty.call(entry, "relevantDate");
}