Changed messages format export from default to non-default

This commit is contained in:
Alexander Cerutti
2021-10-23 19:09:38 +02:00
parent ee2038005b
commit 3c706ddbb6
5 changed files with 24 additions and 20 deletions

View File

@@ -1,7 +1,7 @@
import PKPass from "./PKPass"; import type PKPass from "./PKPass";
import * as Schemas from "./schemas"; import * as Schemas from "./schemas";
import * as Utils from "./utils"; import * as Utils from "./utils";
import formatMessage, * as Messages from "./messages"; import * as Messages from "./messages";
/** /**
* Class to represent lower-level keys pass fields * Class to represent lower-level keys pass fields
@@ -48,7 +48,7 @@ export default class FieldsArray extends Array<Schemas.Field> {
if (this[sharedKeysPoolSymbol].has(current.key)) { if (this[sharedKeysPoolSymbol].has(current.key)) {
console.warn( console.warn(
formatMessage( Messages.format(
Messages.FIELDS.REPEATED_KEY, Messages.FIELDS.REPEATED_KEY,
current.key, current.key,
), ),

View File

@@ -1,3 +1,4 @@
import { Stream } from "stream";
import FieldsArray from "./FieldsArray"; import FieldsArray from "./FieldsArray";
import Bundle, { filesSymbol } from "./Bundle"; import Bundle, { filesSymbol } from "./Bundle";
import getModelFolderContents from "./getModelFolderContents"; import getModelFolderContents from "./getModelFolderContents";
@@ -5,8 +6,7 @@ import * as Schemas from "./schemas";
import * as Signature from "./Signature"; import * as Signature from "./Signature";
import * as Strings from "./StringsUtils"; import * as Strings from "./StringsUtils";
import * as Utils from "./utils"; import * as Utils from "./utils";
import { Stream } from "stream"; import * as Messages from "./messages";
import formatMessage, * as Messages from "./messages";
/** Exporting for tests specs */ /** Exporting for tests specs */
export const propsSymbol = Symbol("props"); export const propsSymbol = Symbol("props");
@@ -44,7 +44,7 @@ export default class PKPass extends Bundle {
if (!source) { if (!source) {
throw new TypeError( throw new TypeError(
formatMessage(Messages.FROM.MISSING_SOURCE, source), Messages.format(Messages.FROM.MISSING_SOURCE, source),
); );
} }
@@ -372,7 +372,9 @@ export default class PKPass extends Bundle {
validateJSONBuffer(buffer, Schemas.PassProps), validateJSONBuffer(buffer, Schemas.PassProps),
); );
} catch (err) { } catch (err) {
console.warn(formatMessage(Messages.PASS_SOURCE.INVALID, err)); console.warn(
Messages.format(Messages.PASS_SOURCE.INVALID, err),
);
return; return;
} }
@@ -395,7 +397,9 @@ export default class PKPass extends Bundle {
try { try {
validateJSONBuffer(buffer, Schemas.Personalize); validateJSONBuffer(buffer, Schemas.Personalize);
} catch (err) { } catch (err) {
console.warn(formatMessage(Messages.PERSONALIZE.INVALID, err)); console.warn(
Messages.format(Messages.PERSONALIZE.INVALID, err),
);
return; return;
} }
@@ -576,7 +580,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 (/personalization/.test(fileNames[i])) {
console.warn( console.warn(
formatMessage( Messages.format(
Messages.CLOSE.PERSONALIZATION_REMOVED, Messages.CLOSE.PERSONALIZATION_REMOVED,
fileNames[i], fileNames[i],
), ),
@@ -698,7 +702,7 @@ export default class PKPass extends Bundle {
if (typeof lang !== "string") { if (typeof lang !== "string") {
throw new TypeError( throw new TypeError(
formatMessage(Messages.LANGUAGES.INVALID_LANG, typeof lang), Messages.format(Messages.LANGUAGES.INVALID_LANG, typeof lang),
); );
} }
@@ -723,7 +727,7 @@ export default class PKPass extends Bundle {
if (!translations || !Object.keys(translations).length) { if (!translations || !Object.keys(translations).length) {
console.warn( console.warn(
formatMessage(Messages.LANGUAGES.NO_TRANSLATIONS, lang), Messages.format(Messages.LANGUAGES.NO_TRANSLATIONS, lang),
); );
return; return;
} }
@@ -754,7 +758,7 @@ export default class PKPass extends Bundle {
this[propsSymbol]["expirationDate"] = Utils.processDate(date); this[propsSymbol]["expirationDate"] = Utils.processDate(date);
} catch (err) { } catch (err) {
throw new TypeError( throw new TypeError(
formatMessage(Messages.DATE.INVALID, "expirationDate", date), Messages.format(Messages.DATE.INVALID, "expirationDate", date),
); );
} }
} }
@@ -849,7 +853,7 @@ export default class PKPass extends Bundle {
this[propsSymbol]["relevantDate"] = Utils.processDate(date); this[propsSymbol]["relevantDate"] = Utils.processDate(date);
} catch (err) { } catch (err) {
throw new TypeError( throw new TypeError(
formatMessage(Messages.DATE.INVALID, "relevantDate", date), Messages.format(Messages.DATE.INVALID, "relevantDate", date),
); );
} }
} }

View File

@@ -1,6 +1,6 @@
import * as path from "path"; import * as path from "path";
import * as Utils from "./utils"; import * as Utils from "./utils";
import formatMessage, * as Messages from "./messages"; import * as Messages from "./messages";
import { promises as fs } from "fs"; import { promises as fs } from "fs";
/** /**
@@ -54,7 +54,7 @@ export default async function getModelFolderContents(
if (err.syscall === "open") { if (err.syscall === "open") {
// file opening failed // file opening failed
throw new Error( throw new Error(
formatMessage( Messages.format(
Messages.MODELS.FILE_NO_OPEN, Messages.MODELS.FILE_NO_OPEN,
JSON.stringify(err), JSON.stringify(err),
), ),
@@ -62,7 +62,7 @@ export default async function getModelFolderContents(
} else if (err.syscall === "scandir") { } else if (err.syscall === "scandir") {
// directory reading failed // directory reading failed
throw new Error( throw new Error(
formatMessage(Messages.MODELS.DIR_NOT_FOUND, err.path), Messages.format(Messages.MODELS.DIR_NOT_FOUND, err.path),
); );
} }
} }

View File

@@ -94,7 +94,7 @@ export const PACK = {
* @param values * @param values
*/ */
export default function format(messageName: string, ...values: any[]) { export function format(messageName: string, ...values: any[]) {
// reversing because it is better popping than shifting. // reversing because it is better popping than shifting.
const replaceValues = values.reverse(); const replaceValues = values.reverse();
return messageName.replace(/%s/g, () => replaceValues.pop()); return messageName.replace(/%s/g, () => replaceValues.pop());

View File

@@ -18,7 +18,7 @@ import { PassFields, TransitType } from "./PassFields";
import { Semantics } from "./Semantics"; import { Semantics } from "./Semantics";
import { CertificatesSchema } from "./Certificates"; import { CertificatesSchema } from "./Certificates";
import formatMessage, * as Messages from "../messages"; import * as Messages from "../messages";
const RGB_COLOR_REGEX = const RGB_COLOR_REGEX =
/rgb\(\s*(?:[01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\s*,\s*(?:[01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\s*,\s*(?:[01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\s*\)/; /rgb\(\s*(?:[01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\s*,\s*(?:[01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\s*,\s*(?:[01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\s*\)/;
@@ -204,7 +204,7 @@ export function assertValidity<T>(
if (customErrorMessage) { if (customErrorMessage) {
console.warn(validation.error); console.warn(validation.error);
throw new TypeError( throw new TypeError(
`${validation.error.name} happened. ${formatMessage( `${validation.error.name} happened. ${Messages.format(
customErrorMessage, customErrorMessage,
validation.error.message, validation.error.message,
)}`, )}`,
@@ -253,7 +253,7 @@ export function filterValid<T extends Object>(
try { try {
return [...acc, validate(schema, current)]; return [...acc, validate(schema, current)];
} catch (err) { } catch (err) {
console.warn(formatMessage(Messages.FILTER_VALID.INVALID, err)); console.warn(Messages.format(Messages.FILTER_VALID.INVALID, err));
return [...acc]; return [...acc];
} }
}, []); }, []);