Skip to content

Commit 4aa8ed4

Browse files
committed
feat(search): implemented locale code search; improved tests
1 parent fd414b3 commit 4aa8ed4

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const {
22
validateLocaleCode,
33
findCountryLanguages,
4+
findCountryLocales,
45
} = require('./lib')
56

67
module.exports = {
78
validateLocaleCode,
89
findCountryLanguages,
10+
findCountryLocales,
911
}

src/lib/index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,41 @@ const validateLocaleCode = (localeCode) => {
1010
return _.findIndex(data, (l) => l.code === localeCode) !== -1
1111
}
1212

13+
/**
14+
* Takes a country code and returns the list of languages valid for it
15+
* @param {string} countryCode - Country code (e.g. "PT")
16+
* @return {string[]} Languages list
17+
*/
1318
const findCountryLanguages = (countryCode) => {
14-
return _.filter(data, (l) => l.countryCode === countryCode)
19+
const countryEntries = _.filter(data, (l) => l.countryCode === countryCode)
20+
const langSet = new Set()
21+
22+
for (let i = 0; i < countryEntries.length; i++) {
23+
langSet.add(countryEntries[i].langCode)
24+
}
25+
26+
return Array.from(langSet)
1527
}
1628

29+
/**
30+
* Takes a country code and returns the list of locales valid for it
31+
* @param {string} countryCode - Country code (e.g. "PT")
32+
* @return {string[]} Locales list
33+
*/
34+
const findCountryLocales = (countryCode) => {
35+
const countryEntries = _.filter(data, (l) => l.countryCode === countryCode)
36+
const localeSet = new Set()
37+
38+
for (let i = 0; i < countryEntries.length; i++) {
39+
localeSet.add(countryEntries[i].code)
40+
}
41+
42+
return Array.from(localeSet)
43+
}
44+
45+
1746
module.exports = {
1847
validateLocaleCode,
1948
findCountryLanguages,
49+
findCountryLocales,
2050
}

test/index.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { assert, expect } = require('chai')
44
const {
55
validateLocaleCode,
66
findCountryLanguages,
7+
findCountryLocales,
78
} = require('../src')
89

910
describe('Validation tests.', () => {
@@ -24,11 +25,12 @@ describe('Validation tests.', () => {
2425
})
2526
})
2627

27-
describe('Code search tests.', () => {
28+
describe('Language code by country search tests.', () => {
2829
it('should return a list of languages', (done) => {
2930
const res = findCountryLanguages('US')
3031
expect(res).to.be.an('array')
3132
expect(res).to.have.lengthOf(2)
33+
expect(res).to.deep.equal(['en', 'es'])
3234
done()
3335
})
3436

@@ -47,3 +49,50 @@ describe('Code search tests.', () => {
4749
})
4850
})
4951

52+
describe('Locale code by country search tests.', () => {
53+
it('should return a list of locales', (done) => {
54+
const res = findCountryLocales('BE')
55+
expect(res).to.be.an('array')
56+
expect(res).to.have.lengthOf(4)
57+
expect(res).to.deep.equal(['de-BE', 'en-BE', 'fr-BE', 'nl-BE'])
58+
done()
59+
})
60+
61+
it('should return an empty list', (done) => {
62+
const res = findCountryLocales('YYY')
63+
expect(res).to.be.an('array')
64+
expect(res).to.have.lengthOf(0)
65+
done()
66+
})
67+
68+
it('should return an empty list again', (done) => {
69+
const res = findCountryLocales(77236)
70+
expect(res).to.be.an('array')
71+
expect(res).to.have.lengthOf(0)
72+
done()
73+
})
74+
})
75+
76+
describe('Locale code by country search tests.', () => {
77+
it('should return a list of locales', (done) => {
78+
const res = findCountryLocales('BE')
79+
expect(res).to.be.an('array')
80+
expect(res).to.have.lengthOf(4)
81+
expect(res).to.deep.equal(['de-BE', 'en-BE', 'fr-BE', 'nl-BE'])
82+
done()
83+
})
84+
85+
it('should return an empty list', (done) => {
86+
const res = findCountryLocales('YYY')
87+
expect(res).to.be.an('array')
88+
expect(res).to.have.lengthOf(0)
89+
done()
90+
})
91+
92+
it('should return an empty list again', (done) => {
93+
const res = findCountryLocales(77236)
94+
expect(res).to.be.an('array')
95+
expect(res).to.have.lengthOf(0)
96+
done()
97+
})
98+
})

0 commit comments

Comments
 (0)