-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: getCountryByLocale and code cleanup
- Loading branch information
Showing
5 changed files
with
88 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
name: Release | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: | ||
- master | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters