Skip to content

Commit

Permalink
refactor: change ErrorCode naming
Browse files Browse the repository at this point in the history
  • Loading branch information
korvin89 committed Jan 24, 2024
1 parent 106c5a2 commit 2410a72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class I18N {

({text, fallbackText, details} = this.getTranslationData({keysetName, key, params, lang: this.lang}));

if (details && details.code !== ErrorCode.NO_LANGUAGE_DATA) {
if (details && details.code !== ErrorCode.NoLanguageData) {
const message = mapErrorCodeToMessage({
code: details.code,
lang: this.lang,
Expand All @@ -163,7 +163,7 @@ export class I18N {
lang: this.fallbackLang,
}));

if (details && details.code !== ErrorCode.NO_LANGUAGE_DATA) {
if (details && details.code !== ErrorCode.NoLanguageData) {
const message = mapErrorCodeToMessage({
code: details.code,
lang: this.lang,
Expand Down Expand Up @@ -236,13 +236,13 @@ export class I18N {
const languageData = this.getLanguageData(lang);

if (typeof languageData === 'undefined') {
return {details: {code: ErrorCode.NO_LANGUAGE_DATA}};
return {details: {code: ErrorCode.NoLanguageData}};
}

if (Object.keys(languageData).length === 0) {
return {
fallbackText: key,
details: {code: ErrorCode.EMPTY_LANGUAGE_DATA},
details: {code: ErrorCode.EmptyLanguageData},
};
}

Expand All @@ -251,14 +251,14 @@ export class I18N {
if (!keyset) {
return {
fallbackText: key,
details: {code: ErrorCode.KEYSET_NOT_FOUND, keysetName},
details: {code: ErrorCode.KeysetNotFound, keysetName},
};
}

if (Object.keys(keyset).length === 0) {
return {
fallbackText: key,
details: {code: ErrorCode.EMPTY_KEYSET, keysetName},
details: {code: ErrorCode.EmptyKeyset, keysetName},
};
}

Expand All @@ -268,15 +268,15 @@ export class I18N {
if (typeof keyValue === 'undefined') {
return {
fallbackText: key,
details: {code: ErrorCode.MISSING_KEY, keysetName, key},
details: {code: ErrorCode.MissingKey, keysetName, key},
};
}

if (Array.isArray(keyValue)) {
if (keyValue.length < 3) {
return {
fallbackText: key,
details: {code: ErrorCode.MISSING_REQUIRED_PLURALS, keysetName, key},
details: {code: ErrorCode.MissingKeyPlurals, keysetName, key},
};
}

Expand All @@ -285,7 +285,7 @@ export class I18N {
if (Number.isNaN(count)) {
return {
fallbackText: key,
details: {code: ErrorCode.MISSING_KEY_PARAMS_COUNT, keysetName, key},
details: {code: ErrorCode.MissingKeyParamsCount, keysetName, key},
};
}

Expand All @@ -294,7 +294,7 @@ export class I18N {

if (keyValue[PluralForm.None] === undefined) {
result.details = {
code: ErrorCode.MISSING_KEY_FOR_0,
code: ErrorCode.MissingKeyFor0,
keysetName,
key,
};
Expand Down
18 changes: 9 additions & 9 deletions src/translation-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export const ErrorCode = {
NO_LANGUAGE_DATA: 'NO_LANGUAGE_DATA',
EMPTY_LANGUAGE_DATA: 'EMPTY_LANGUAGE_DATA',
KEYSET_NOT_FOUND: 'KEYSET_NOT_FOUND',
EMPTY_KEYSET: 'EMPTY_KEYSET',
MISSING_KEY: 'MISSING_KEY',
MISSING_REQUIRED_PLURALS: 'MISSING_REQUIRED_PLURALS',
MISSING_KEY_PARAMS_COUNT: 'MISSING_KEY_PARAMS_COUNT',
MISSING_KEY_FOR_0: 'MISSING_KEY_FOR_0',
EmptyKeyset: 'EMPTY_KEYSET',
EmptyLanguageData: 'EMPTY_LANGUAGE_DATA',
KeysetNotFound: 'KEYSET_NOT_FOUND',
MissingKey: 'MISSING_KEY',
MissingKeyFor0: 'MISSING_KEY_FOR_0',
MissingKeyParamsCount: 'MISSING_KEY_PARAMS_COUNT',
MissingKeyPlurals: 'MISSING_KEY_PLURALS',
NoLanguageData: 'NO_LANGUAGE_DATA',
} as const;

const codeValues = Object.values(ErrorCode);
Expand Down Expand Up @@ -41,7 +41,7 @@ export function mapErrorCodeToMessage(args: {lang?: string; fallbackLang?: strin
message = 'Missing params.count for key.';
break;
}
case 'MISSING_REQUIRED_PLURALS': {
case 'MISSING_KEY_PLURALS': {
message = 'Missing required plurals';
break;
}
Expand Down

0 comments on commit 2410a72

Please sign in to comment.