Skip to content

Commit

Permalink
fix: remove LocaleCode enum
Browse files Browse the repository at this point in the history
This enum often causes tree shaking issues as it’s an Enum Value and it’s only used as a type. Changing in favour of inference based typescript types and an string literal.

Disabled useless auditjs check

BREAKING CHANGE: LocaleCode enum -> LocaleCode string literal
  • Loading branch information
SimeonC committed Jun 25, 2024
1 parent ad7c730 commit cb1f77e
Show file tree
Hide file tree
Showing 22 changed files with 148 additions and 180 deletions.
80 changes: 28 additions & 52 deletions .github/workflows/pull_requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,61 +57,37 @@ jobs:
- name: Run linter
run: npm run lint

audit:
needs: [setup]
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: ${{ needs.setup.outputs.nvmrc }}
cache: npm

- name: Restore npm installs and Lerna setup
run: npm i --legacy-peer-deps

- name: Audit Dependencies
run: npm run audit:ci
env:
OSSI_USERNAME: ${{ secrets.OSSI_USERNAME }}
OSSI_TOKEN: ${{ secrets.OSSI_TOKEN }}

# test:
# needs: [setup]
# runs-on: ubuntu-latest
# steps:
# - name: Checkout 🛎️
# uses: actions/checkout@v2
# with:
# fetch-depth: 0
#
# - name: Setup Node.js
# uses: actions/setup-node@v2
# with:
# node-version: ${{ needs.setup.outputs.nvmrc }}
# cache: npm
#
# - name: Restore npm installs and Lerna setup
# run: npm i --legacy-peer-deps
#
# - name: Run Tests
# run: npm test
#
# - name: Publish Unit Test Results
# uses: EnricoMi/publish-unit-test-result-action@v1
# if: always()
# with:
# files: junit/**/*.xml
# test:
# needs: [setup]
# runs-on: ubuntu-latest
# steps:
# - name: Checkout 🛎️
# uses: actions/checkout@v2
# with:
# fetch-depth: 0
#
# - name: Setup Node.js
# uses: actions/setup-node@v2
# with:
# node-version: ${{ needs.setup.outputs.nvmrc }}
# cache: npm
#
# - name: Restore npm installs and Lerna setup
# run: npm i --legacy-peer-deps
#
# - name: Run Tests
# run: npm test
#
# - name: Publish Unit Test Results
# uses: EnricoMi/publish-unit-test-result-action@v1
# if: always()
# with:
# files: junit/**/*.xml

release:
runs-on: ubuntu-latest
needs: [audit, lint]
# needs: [test, audit, lint]
needs: [lint]
# needs: [test, lint]
steps:
- name: Checkout 🛎️
uses: actions/checkout@v2
Expand Down
10 changes: 6 additions & 4 deletions src/types.ts → src/defineLocale.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum LocaleCode {
export enum LocaleCodeEnum {
Arabic = 'ar',
ChineseSimplified = 'zh-CN',
ChineseTraditional = 'zh-TW',
Expand All @@ -20,15 +20,15 @@ export enum LocaleCode {
Vietnamese = 'vi'
}

export interface Locale {
export function defineLocale<Code extends string>(locale: {

Check failure on line 23 in src/defineLocale.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
/**
* Optional locale ISO codes that should resolve to this one
*/
aliases?: string[];
/**
* The ISO code of the locale
*/
code: LocaleCode;
code: `${Code}`;
/**
* Writing direction, if not defined assume 'ltr'
*/
Expand Down Expand Up @@ -57,6 +57,8 @@ export interface Locale {
* All locale names in this language
*/
translations: {
[key in LocaleCode]: string;
[key: string]: string;
};
}) {
return locale;
}
86 changes: 38 additions & 48 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,32 @@ import { tagalog } from './localeDefinitions/tagalog';
import { thai } from './localeDefinitions/thai';
import { turkish } from './localeDefinitions/turkish';

Check failure on line 18 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

'turkish' is defined but never used
import { vietnamese } from './localeDefinitions/vietnamese';
import { Locale, LocaleCode } from './types';

export * from './types';

/**
* Priority ordered array of locales
*/
export const ordered: Locale[] = [
export const ordered = [
english,
japanese,
korean,
chineseSimplified,
chineseTraditional,
german,
spanish,
french,
italian,
indonesian,
malay,
portuguese,
russian,
tagalog,
thai,
lao,
vietnamese,
arabic
] as const;

export const locales = {
english,
japanese,
korean,
Expand All @@ -43,35 +61,33 @@ export const ordered: Locale[] = [
lao,
vietnamese,
arabic
];
} as const;

export type LocaleCode = typeof ordered[number]['code'];

/**
* Returns the correct default locale
*/
export function getDefaultLocale(selectableLocales?: LocaleCode[]): Locale {
const locales: Locale[] = selectableLocales
? ordered.filter((item: Locale) =>
export function getDefaultLocale(selectableLocales?: LocaleCode[]) {

Check failure on line 71 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
const filteredLocales = selectableLocales
? ordered.filter((item) =>
selectableLocales.find((locale) => locale === item.code)
)
: ordered;

const englishLocale = locales.find(
(locale) => locale.code === LocaleCode.English
);
const englishLocale = filteredLocales.find((locale) => locale.code === 'en');
if (englishLocale) return englishLocale;

const japaneseLocale = locales.find(
(locale) => locale.code === LocaleCode.Japanese
);
const japaneseLocale = filteredLocales.find((locale) => locale.code === 'ja');
if (japaneseLocale) return japaneseLocale;

return locales[0];
return filteredLocales[0];
}

/**
* Priority ordered array of LocaleCodes
* Priority ordered array of ISO 639-1 locale codes
*/
export const orderedLocales: LocaleCode[] = ordered.map(
export const orderedLocaleCodes: LocaleCode[] = ordered.map(
(locale) => locale.code
);

Expand All @@ -80,35 +96,9 @@ export const orderedLocales: LocaleCode[] = ordered.map(
* @param locales
*/
export function orderLocales(locales: LocaleCode[]): LocaleCode[] {

Check failure on line 98 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

'locales' is already declared in the upper scope
return orderedLocales.filter((locale) => locales.indexOf(locale) >= 0);
return orderedLocaleCodes.filter((locale) => locales.indexOf(locale) >= 0);
}

/**
* All locales
* @type {Object} keys are locale codes, values are Locale objects
*/
export const locales: { [key in LocaleCode]: Locale } = {
[LocaleCode.English]: english,
[LocaleCode.Japanese]: japanese,
[LocaleCode.Korean]: korean,
[LocaleCode.ChineseSimplified]: chineseSimplified,
[LocaleCode.ChineseTraditional]: chineseTraditional,
[LocaleCode.German]: german,
[LocaleCode.Spanish]: spanish,
[LocaleCode.French]: french,
[LocaleCode.Italian]: italian,
[LocaleCode.Indonesian]: indonesian,
[LocaleCode.Malay]: malay,
[LocaleCode.Portuguese]: portuguese,
[LocaleCode.Russian]: russian,
[LocaleCode.Tagalog]: tagalog,
[LocaleCode.Thai]: thai,
[LocaleCode.Lao]: lao,
[LocaleCode.Vietnamese]: vietnamese,
[LocaleCode.Arabic]: arabic,
[LocaleCode.Turkish]: turkish
};

/**
* Function to check if a locale is CJK - Chinese/Japanese/Korean.
* Often logic is set to be different in these three language types as they
Expand All @@ -117,9 +107,9 @@ export const locales: { [key in LocaleCode]: Locale } = {
*/
export function isCJKLocale(locale: LocaleCode): boolean {
return (
locale === LocaleCode.Japanese ||
locale === LocaleCode.Korean ||
locale === LocaleCode.ChineseSimplified ||
locale === LocaleCode.ChineseTraditional
locale === japanese.code ||
locale === korean.code ||
locale === chineseSimplified.code ||
locale === chineseTraditional.code
);
}
8 changes: 4 additions & 4 deletions src/localeDefinitions/arabic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Locale, LocaleCode } from '../types';
import { defineLocale, LocaleCodeEnum } from "../defineLocale";

export const arabic: Locale = {
code: LocaleCode.Arabic,
export const arabic = defineLocale({
code: LocaleCodeEnum.Arabic,
facebook: 'ar_AR',
label: 'عربى',
loadingText: 'جار التحميل...',
Expand All @@ -27,4 +27,4 @@ export const arabic: Locale = {
'zh-CN': 'الصينية (المبسيطة)',
'zh-TW': 'الصينية (التقليدية)'
}
};
});
8 changes: 4 additions & 4 deletions src/localeDefinitions/chineseSimplified.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Locale, LocaleCode } from '../types';
import { defineLocale, LocaleCodeEnum } from "../defineLocale";

export const chineseSimplified: Locale = {
code: LocaleCode.ChineseSimplified,
export const chineseSimplified = defineLocale({
code: LocaleCodeEnum.ChineseSimplified,
facebook: 'zh_CN',
label: '简体中文',
loadingText: '上传中・・・',
Expand All @@ -27,4 +27,4 @@ export const chineseSimplified: Locale = {
'zh-CN': '‪简体中文‬',
'zh-TW': '繁体中文'
}
};
});
8 changes: 4 additions & 4 deletions src/localeDefinitions/chineseTraditional.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Locale, LocaleCode } from '../types';
import { defineLocale, LocaleCodeEnum } from "../defineLocale";

export const chineseTraditional: Locale = {
code: LocaleCode.ChineseTraditional,
export const chineseTraditional = defineLocale({
code: LocaleCodeEnum.ChineseTraditional,
facebook: 'zh_TW',
label: '繁体中文',
loadingText: '讀取中・・・',
Expand All @@ -27,4 +27,4 @@ export const chineseTraditional: Locale = {
'zh-CN': '‪簡體中文‬',
'zh-TW': '繁體中文'
}
};
});
8 changes: 4 additions & 4 deletions src/localeDefinitions/english.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Locale, LocaleCode } from '../types';
import { defineLocale, LocaleCodeEnum } from "../defineLocale";

export const english: Locale = {
code: LocaleCode.English,
export const english = defineLocale({
code: LocaleCodeEnum.English,
facebook: 'en_US',
label: 'English',
loadingText: 'Loading...',
Expand All @@ -26,4 +26,4 @@ export const english: Locale = {
'zh-CN': '‪Chinese (Simp.)',
'zh-TW': '‪Chinese (Trad.)'
}
};
});
8 changes: 4 additions & 4 deletions src/localeDefinitions/french.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Locale, LocaleCode } from '../types';
import { defineLocale, LocaleCodeEnum } from "../defineLocale";

export const french: Locale = {
code: LocaleCode.French,
export const french = defineLocale({
code: LocaleCodeEnum.French,
facebook: 'fr_FR',
label: 'Français',
loadingText: 'Chargement...',
Expand All @@ -26,4 +26,4 @@ export const french: Locale = {
'zh-CN': 'Chinois (Simp.)',
'zh-TW': 'Chinois (Trad.)'
}
};
});
8 changes: 4 additions & 4 deletions src/localeDefinitions/german.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Locale, LocaleCode } from '../types';
import { defineLocale, LocaleCodeEnum } from "../defineLocale";

export const german: Locale = {
code: LocaleCode.German,
export const german = defineLocale({
code: LocaleCodeEnum.German,
facebook: 'de_DE',
label: 'Deutsch',
loadingText: 'Laden...',
Expand All @@ -26,4 +26,4 @@ export const german: Locale = {
'zh-CN': '‪Chinesisch (Einf.)',
'zh-TW': '‪Chinesisch (Trad.)'
}
};
});
8 changes: 4 additions & 4 deletions src/localeDefinitions/indonesian.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Locale, LocaleCode } from '../types';
import { defineLocale, LocaleCodeEnum } from "../defineLocale";

export const indonesian: Locale = {
code: LocaleCode.Indonesian,
export const indonesian = defineLocale({
code: LocaleCodeEnum.Indonesian,
facebook: 'id_ID',
label: 'Bahasa Indonesia',
loadingText: 'Pemuatan...',
Expand All @@ -26,4 +26,4 @@ export const indonesian: Locale = {
'zh-CN': 'Bahasa Cina (diseder.)',
'zh-TW': 'Bahasa Cina (trad.)'
}
};
});
8 changes: 4 additions & 4 deletions src/localeDefinitions/italian.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Locale, LocaleCode } from '../types';
import { defineLocale, LocaleCodeEnum } from "../defineLocale";

export const italian: Locale = {
code: LocaleCode.Italian,
export const italian = defineLocale({
code: LocaleCodeEnum.Italian,
facebook: 'it_IT',
label: 'Italiano',
loadingText: 'Caricamento in corso...',
Expand All @@ -26,4 +26,4 @@ export const italian: Locale = {
'zh-CN': '‪Chinese (simp.)',
'zh-TW': '‪Chinese (trad.)'
}
};
});
Loading

0 comments on commit cb1f77e

Please sign in to comment.