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 CHANGES:
LocaleCode enum -> LocaleCode string literal
locales.en -> locales.english
orderedLocales -> orderedLocaleCodes
  • Loading branch information
SimeonC committed Jun 25, 2024
1 parent ad7c730 commit 66c1cf4
Show file tree
Hide file tree
Showing 23 changed files with 176 additions and 206 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
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ jobs:
- name: Install 🔧
run: npm ci --legacy-peer-deps

# - name: Run Tests
# run: npm test
# - name: Run Tests
# run: npm test

- name: Create Release ✨
env:
Expand Down
95 changes: 44 additions & 51 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ import { russian } from './localeDefinitions/russian';
import { spanish } from './localeDefinitions/spanish';
import { tagalog } from './localeDefinitions/tagalog';
import { thai } from './localeDefinitions/thai';
import { turkish } from './localeDefinitions/turkish';
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,
Expand All @@ -43,72 +39,69 @@ export const ordered: Locale[] = [
lao,
vietnamese,
arabic
];
] as const;

export const locales = {
english,
japanese,
korean,
chineseSimplified,
chineseTraditional,
german,
spanish,
french,
italian,
indonesian,
malay,
portuguese,
russian,
tagalog,
thai,
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[]
): (typeof ordered)[number] {
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
);

/**
* Re-order and filter a list of locales to match the default order
* @param locales
* @param localesToOrder
*/
export function orderLocales(locales: LocaleCode[]): LocaleCode[] {
return orderedLocales.filter((locale) => locales.indexOf(locale) >= 0);
export function orderLocales(localesToOrder: LocaleCode[]): LocaleCode[] {
return orderedLocaleCodes.filter(
(locale) => localesToOrder.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 +110,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
);
}
9 changes: 5 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 { localeTypeCheck } from '../types';

export const arabic: Locale = {
code: LocaleCode.Arabic,
export const arabic = {
code: 'ar',
facebook: 'ar_AR',
label: 'عربى',
loadingText: 'جار التحميل...',
Expand All @@ -27,4 +27,5 @@ export const arabic: Locale = {
'zh-CN': 'الصينية (المبسيطة)',
'zh-TW': 'الصينية (التقليدية)'
}
};
} as const;
localeTypeCheck(arabic);
9 changes: 5 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 { localeTypeCheck } from '../types';

export const chineseSimplified: Locale = {
code: LocaleCode.ChineseSimplified,
export const chineseSimplified = {
code: 'zh-CN',
facebook: 'zh_CN',
label: '简体中文',
loadingText: '上传中・・・',
Expand All @@ -27,4 +27,5 @@ export const chineseSimplified: Locale = {
'zh-CN': '‪简体中文‬',
'zh-TW': '繁体中文'
}
};
} as const;
localeTypeCheck(chineseSimplified);
9 changes: 5 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 { localeTypeCheck } from '../types';

export const chineseTraditional: Locale = {
code: LocaleCode.ChineseTraditional,
export const chineseTraditional = {
code: 'zh-TW',
facebook: 'zh_TW',
label: '繁体中文',
loadingText: '讀取中・・・',
Expand All @@ -27,4 +27,5 @@ export const chineseTraditional: Locale = {
'zh-CN': '‪簡體中文‬',
'zh-TW': '繁體中文'
}
};
} as const;
localeTypeCheck(chineseTraditional);
9 changes: 5 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 { localeTypeCheck } from '../types';

export const english: Locale = {
code: LocaleCode.English,
export const english = {
code: 'en',
facebook: 'en_US',
label: 'English',
loadingText: 'Loading...',
Expand All @@ -26,4 +26,5 @@ export const english: Locale = {
'zh-CN': '‪Chinese (Simp.)',
'zh-TW': '‪Chinese (Trad.)'
}
};
} as const;
localeTypeCheck(english);
9 changes: 5 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 { localeTypeCheck } from '../types';

export const french: Locale = {
code: LocaleCode.French,
export const french = {
code: 'fr',
facebook: 'fr_FR',
label: 'Français',
loadingText: 'Chargement...',
Expand All @@ -26,4 +26,5 @@ export const french: Locale = {
'zh-CN': 'Chinois (Simp.)',
'zh-TW': 'Chinois (Trad.)'
}
};
} as const;
localeTypeCheck(french);
9 changes: 5 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 { localeTypeCheck } from '../types';

export const german: Locale = {
code: LocaleCode.German,
export const german = {
code: 'de',
facebook: 'de_DE',
label: 'Deutsch',
loadingText: 'Laden...',
Expand All @@ -26,4 +26,5 @@ export const german: Locale = {
'zh-CN': '‪Chinesisch (Einf.)',
'zh-TW': '‪Chinesisch (Trad.)'
}
};
} as const;
localeTypeCheck(german);
9 changes: 5 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 { localeTypeCheck } from '../types';

export const indonesian: Locale = {
code: LocaleCode.Indonesian,
export const indonesian = {
code: 'id',
facebook: 'id_ID',
label: 'Bahasa Indonesia',
loadingText: 'Pemuatan...',
Expand All @@ -26,4 +26,5 @@ export const indonesian: Locale = {
'zh-CN': 'Bahasa Cina (diseder.)',
'zh-TW': 'Bahasa Cina (trad.)'
}
};
} as const;
localeTypeCheck(indonesian);
9 changes: 5 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 { localeTypeCheck } from '../types';

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

0 comments on commit 66c1cf4

Please sign in to comment.