diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index b206a56..8a6f5cb 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -1,6 +1,7 @@ name: Release on: + workflow_dispatch: push: branches: - master diff --git a/src/index.d.ts b/src/index.d.ts index 2281293..42a6eb0 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -2,4 +2,5 @@ export const validateLocaleCode: (localeCode: string) => boolean; export const validateLanguageCode: (languageCode: string) => boolean; export const findCountryLanguages: (countryCode: string) => string[]; export const findCountryLocales: (countryCode: string) => string[]; -export const locales: () => { [locale: string]: string }; \ No newline at end of file +export const locales: () => { [locale: string]: string }; +export const getCountryByLocale: (locale: string) => string | undefined; \ No newline at end of file diff --git a/src/index.js b/src/index.js index fe9157d..727dddd 100644 --- a/src/index.js +++ b/src/index.js @@ -1,15 +1,78 @@ -const { - locales, - validateLocaleCode, - validateLanguageCode, - findCountryLanguages, - findCountryLocales -} = require('./lib') +const data = require('./data.json') + +/** + * Takes a locale code and checks if exists in the data dictionary + * @param {string} localeCode - Language code (e.g. "en-GB") + * @return {boolean} + */ +const validateLocaleCode = (localeCode) => { + return data.findIndex((l) => l.code === localeCode) !== -1 +} + +/** + * Takes a language code and checks if exists in the data dictionary + * @param {string} languageCode - Language code (e.g. "en") + * @return {boolean} + */ +const validateLanguageCode = (languageCode) => { + return data.findIndex((l) => l.langCode === languageCode) !== -1 +} + +/** + * Takes a country code and returns the list of languages valid for it + * @param {string} countryCode - Country code (e.g. "PT") + * @return {string[]} Languages list + */ +const findCountryLanguages = (countryCode) => { + const countryEntries = data.filter((l) => l.countryCode === countryCode) + const langSet = new Set() + + countryEntries.forEach((e, i) => langSet.add(countryEntries[i].langCode)) + + return Array.from(langSet) +} + +/** + * Takes a country code and returns the list of locales valid for it + * @param {string} countryCode - Country code (e.g. "PT") + * @return {string[]} Locales list + */ +const findCountryLocales = (countryCode) => { + const countryEntries = data.filter((l) => l.countryCode === countryCode) + const localeSet = new Set() + + countryEntries.forEach((e, i) => localeSet.add(countryEntries[i].code)) + + return Array.from(localeSet) +} + +/** + * Returns a list with a map of all existing locale codes and the respective country+language + * @return {Object} Locales list in the form of a map: ['pt-PT'] => 'Portuguese (PT)' + */ +const locales = () => { + const map = {} + for (const l of data) { + map[l.code] = `${l.language} (${l.countryCode})` + } + return map +} + +/** + * Returns the country name for a given locale code + * @param {string} localeCode - Locale code (e.g. "en-GB") + * @return {string} Country name or undefined if not found + */ +const getCountryByLocale = (localeCode) => { + const entry = data.find((l) => l.code === localeCode) + return entry?.country +} module.exports = { locales, validateLocaleCode, validateLanguageCode, findCountryLanguages, - findCountryLocales + findCountryLocales, + getCountryByLocale } diff --git a/src/lib/index.js b/src/lib/index.js deleted file mode 100644 index da6e2fe..0000000 --- a/src/lib/index.js +++ /dev/null @@ -1,67 +0,0 @@ -const data = require('../data.json') - -/** - * Takes a locale code and checks if exists in the data dictionary - * @param {string} localeCode - Language code (e.g. "en-GB") - * @return {boolean} - */ -const validateLocaleCode = (localeCode) => { - return data.findIndex((l) => l.code === localeCode) !== -1 -} - -/** - * Takes a language code and checks if exists in the data dictionary - * @param {string} languageCode - Language code (e.g. "en") - * @return {boolean} - */ -const validateLanguageCode = (languageCode) => { - return data.findIndex((l) => l.langCode === languageCode) !== -1 -} - -/** - * Takes a country code and returns the list of languages valid for it - * @param {string} countryCode - Country code (e.g. "PT") - * @return {string[]} Languages list - */ -const findCountryLanguages = (countryCode) => { - const countryEntries = data.filter((l) => l.countryCode === countryCode) - const langSet = new Set() - - countryEntries.forEach((e, i) => langSet.add(countryEntries[i].langCode)) - - return Array.from(langSet) -} - -/** - * Takes a country code and returns the list of locales valid for it - * @param {string} countryCode - Country code (e.g. "PT") - * @return {string[]} Locales list - */ -const findCountryLocales = (countryCode) => { - const countryEntries = data.filter((l) => l.countryCode === countryCode) - const localeSet = new Set() - - countryEntries.forEach((e, i) => localeSet.add(countryEntries[i].code)) - - return Array.from(localeSet) -} - -/** - * Returns a list with a map of all existing locale codes and the respective country+language - * @return {Object} Locales list in the form of a map: ['pt-PT'] => 'Portuguese (PT)' - */ -const locales = () => { - const map = {} - for (const l of data) { - map[l.code] = `${l.language} (${l.countryCode})` - } - return map -} - -module.exports = { - locales, - validateLocaleCode, - validateLanguageCode, - findCountryLanguages, - findCountryLocales -} diff --git a/test/index.js b/test/index.js index 2fc8831..de27f33 100644 --- a/test/index.js +++ b/test/index.js @@ -6,7 +6,8 @@ const { validateLocaleCode, validateLanguageCode, findCountryLanguages, - findCountryLocales + findCountryLocales, + getCountryByLocale } = require('../src') describe('Validation tests.', () => { @@ -92,3 +93,15 @@ describe('Locales map', () => { assert.strictEqual(res['pt-PT'], 'Portuguese (PT)') }) }) + +describe('Country by locale search', () => { + it('should return the country name', () => { + const res = getCountryByLocale('en-GB') + assert.strictEqual(res, 'United Kingdom') + }) + + it('should return undefined', () => { + const res = getCountryByLocale('xx-XX') + assert.strictEqual(res, undefined) + }) +})