Skip to content

Commit

Permalink
feat: getCountryByLocale and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
s1moe2 committed Oct 5, 2024
1 parent b730815 commit 0c8cb44
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 77 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Release

on:
workflow_dispatch:
push:
branches:
- master
Expand Down
3 changes: 2 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
export const locales: () => { [locale: string]: string };
export const getCountryByLocale: (locale: string) => string | undefined;
79 changes: 71 additions & 8 deletions src/index.js
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
}
67 changes: 0 additions & 67 deletions src/lib/index.js

This file was deleted.

15 changes: 14 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const {
validateLocaleCode,
validateLanguageCode,
findCountryLanguages,
findCountryLocales
findCountryLocales,
getCountryByLocale
} = require('../src')

describe('Validation tests.', () => {
Expand Down Expand Up @@ -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)
})
})

0 comments on commit 0c8cb44

Please sign in to comment.