Made all the regex to get created only once

This commit is contained in:
Alexander Cerutti
2022-03-29 22:36:27 +02:00
parent 1900a29d39
commit eeb1978728

View File

@@ -19,6 +19,17 @@ export const closePassSymbol = Symbol("pass.close");
export const passTypeSymbol = Symbol("pass.type"); export const passTypeSymbol = Symbol("pass.type");
export const certificatesSymbol = Symbol("pass.certificates"); export const certificatesSymbol = Symbol("pass.certificates");
const RegExps = {
PASS_JSON: /pass\.json/,
MANIFEST_OR_SIGNATURE: /manifest|signature/,
PERSONALIZATION: {
JSON: /personalization\.json/,
LOGO: /personalizationLogo@(?:.{2})/,
} as const,
PASS_STRINGS: /(?<lang>[a-zA-Z-]{2,}).lproj\/pass\.strings/,
PASS_ICON: /icon(?:@\d{1}x)?/,
} as const;
export default class PKPass extends Bundle { export default class PKPass extends Bundle {
private [certificatesSymbol]: Schemas.CertificatesSchema; private [certificatesSymbol]: Schemas.CertificatesSchema;
private [propsSymbol]: Schemas.PassProps = {}; private [propsSymbol]: Schemas.PassProps = {};
@@ -385,11 +396,11 @@ export default class PKPass extends Bundle {
return; return;
} }
if (/manifest|signature/.test(pathName)) { if (RegExps.MANIFEST_OR_SIGNATURE.test(pathName)) {
return; return;
} }
if (/pass\.json/.test(pathName)) { if (RegExps.PASS_JSON.test(pathName)) {
if (this[filesSymbol]["pass.json"]) { if (this[filesSymbol]["pass.json"]) {
/** /**
* Ignoring any further addition. In a * Ignoring any further addition. In a
@@ -418,7 +429,7 @@ export default class PKPass extends Bundle {
return super.addBuffer(pathName, Buffer.alloc(0)); return super.addBuffer(pathName, Buffer.alloc(0));
} }
if (/personalization\.json/.test(pathName)) { if (RegExps.PERSONALIZATION.JSON.test(pathName)) {
/** /**
* We are still allowing `personalizationLogo@XX.png` * We are still allowing `personalizationLogo@XX.png`
* to be added to the bundle, but we'll delete it * to be added to the bundle, but we'll delete it
@@ -450,12 +461,9 @@ export default class PKPass extends Bundle {
* its translations for later * its translations for later
*/ */
const translationsFileRegexp =
/(?<lang>[a-zA-Z-]{2,}).lproj\/pass\.strings/;
let match: RegExpMatchArray | null; let match: RegExpMatchArray | null;
if ((match = normalizedPathName.match(translationsFileRegexp))) { if ((match = normalizedPathName.match(RegExps.PASS_STRINGS))) {
const [, lang] = match; const [, lang] = match;
const parsedTranslations = Strings.parse(buffer).translations; const parsedTranslations = Strings.parse(buffer).translations;
@@ -571,8 +579,7 @@ export default class PKPass extends Bundle {
const passJson = Buffer.from(JSON.stringify(this[propsSymbol])); const passJson = Buffer.from(JSON.stringify(this[propsSymbol]));
super.addBuffer("pass.json", passJson); super.addBuffer("pass.json", passJson);
const ICON_REGEX = /icon(?:@\d{1}x)?/; if (!fileNames.some((fileName) => RegExps.PASS_ICON.test(fileName))) {
if (!fileNames.some((fileName) => ICON_REGEX.test(fileName))) {
console.warn(Messages.CLOSE.MISSING_ICON); console.warn(Messages.CLOSE.MISSING_ICON);
} }
@@ -600,7 +607,7 @@ export default class PKPass extends Bundle {
this[propsSymbol]["nfc"] && this[propsSymbol]["nfc"] &&
this[filesSymbol]["personalization.json"] && this[filesSymbol]["personalization.json"] &&
fileNames.find((file) => fileNames.find((file) =>
/personalizationLogo@(?:.{2})/.test(file), RegExps.PERSONALIZATION.LOGO.test(file),
), ),
); );
@@ -611,7 +618,7 @@ export default class PKPass extends Bundle {
*/ */
for (let i = 0; i < fileNames.length; i++) { for (let i = 0; i < fileNames.length; i++) {
if (/personalization/.test(fileNames[i])) { if (fileNames[i].includes("personalization")) {
console.warn( console.warn(
Messages.format( Messages.format(
Messages.CLOSE.PERSONALIZATION_REMOVED, Messages.CLOSE.PERSONALIZATION_REMOVED,