Skip to content

Commit

Permalink
fix index
Browse files Browse the repository at this point in the history
  • Loading branch information
xcarpentier committed Oct 7, 2019
1 parent 45bb61f commit e41c67c
Show file tree
Hide file tree
Showing 13 changed files with 8,104 additions and 8,953 deletions.
2 changes: 1 addition & 1 deletion data/cca2.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["AF","AL","DZ","AS","AD","AO","AI","AQ","AG","AR","AM","AW","AU","AT","AZ","BS","BH","BD","BB","BY","BE","BZ","BJ","BM","BT","BO","BA","BW","BV","BR","IO","VG","BN","BG","BF","BI","KH","CM","CA","CV","KY","CF","TD","CL","CN","CX","CC","CO","KM","CK","CR","HR","CU","CW","CY","CZ","CD","DK","DJ","DM","DO","EC","EG","SV","GQ","ER","EE","ET","FK","FO","FJ","FI","FR","GF","PF","TF","GA","GM","GE","DE","GH","GI","GR","GL","GD","GP","GU","GT","GG","GN","GW","GY","HT","HM","HN","HK","HU","IS","IN","ID","IR","IQ","IE","IM","IL","IT","CI","JM","JP","JE","JO","KZ","KE","KI","XK","KW","KG","LA","LV","LB","LS","LR","LY","LI","LT","LU","MO","MK","MG","MW","MY","MV","ML","MT","MH","MQ","MR","MU","YT","MX","FM","MD","MC","MN","ME","MS","MA","MZ","MM","NA","NR","NP","NL","NC","NZ","NI","NE","NG","NU","NF","KP","MP","NO","OM","PK","PW","PS","PA","PG","PY","PE","PH","PN","PL","PT","PR","QA","CG","RO","RU","RW","RE","BL","KN","LC","MF","PM","VC","WS","SM","SA","SN","RS","SC","SL","SG","SX","SK","SI","SB","SO","ZA","GS","KR","SS","ES","LK","SD","SR","SJ","SZ","SE","CH","SY","ST","TW","TJ","TZ","TH","TL","TG","TK","TO","TT","TN","TR","TM","TC","TV","UG","UA","AE","GB","US","UM","VI","UY","UZ","VU","VA","VE","VN","WF","EH","YE","ZM","ZW","AX"]
["AF","AL","DZ","AS","AD","AO","AI","AQ","AG","AR","AM","AW","AU","AT","AZ","BS","BH","BD","BB","BY","BE","BZ","BJ","BM","BT","BO","BA","BW","BV","BR","IO","VG","BN","BG","BF","BI","KH","CM","CA","CV","BQ","KY","CF","TD","CL","CN","CX","CC","CO","KM","CK","CR","HR","CU","CW","CY","CZ","CD","DK","DJ","DM","DO","EC","EG","SV","GQ","ER","EE","SZ","ET","FK","FO","FJ","FI","FR","GF","PF","TF","GA","GM","GE","DE","GH","GI","GR","GL","GD","GP","GU","GT","GG","GN","GW","GY","HT","HM","HN","HU","IS","IN","ID","IR","IQ","IE","IM","IL","IT","CI","JM","JP","JE","JO","KZ","KE","XK","KW","KG","LA","LV","LB","LS","LR","LY","LI","LT","LU","MO","MK","MG","MW","MY","MV","ML","MT","MH","MQ","MR","MU","YT","MX","FM","MD","MC","MN","ME","MS","MA","MZ","MM","NA","NR","NP","NL","NC","NZ","NI","NE","NG","NU","NF","KP","MP","NO","OM","PK","PW","PS","PA","PG","PY","PE","PH","PN","PL","PT","PR","QA","CG","RO","RU","RW","RE","BL","SH","KN","LC","MF","PM","VC","WS","SM","SA","SN","RS","SC","SL","SG","SX","SK","SI","SB","SO","ZA","GS","KR","SS","ES","LK","SD","SR","SJ","SE","CH","SY","ST","TW","TJ","TZ","TH","TL","TG","TK","TO","TT","TN","TR","TM","TC","TV","UG","UA","AE","GB","US","UM","VI","UY","UZ","VU","VA","VE","VN","WF","EH","YE","ZM","ZW","KI","HK","AX"]
2 changes: 1 addition & 1 deletion data/countries-emoji.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion data/countries.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"config:npm": "json -I -f package.json -e 'this.main=\"lib/index.js\"'",
"lint": "tslint --project .",
"test": "TZ=Europe/Paris jest --no-watchman",
"gendata": "rm -rf ./data/countries.json && babel-node ./scripts/transform-world-countries.js > ./data/countries.json",
"gendata-emoji": "rm -rf ./data/countries-emoji.json && babel-node ./scripts/transform-world-countries.js --emoji > ./data/countries-emoji.json",
"gendata-cca2": "rm -rf ./data/cca2.json && babel-node ./scripts/transform-world-countries.js --cca2 > ./data/cca2.json",
"data:image": "rm -rf ./data/countries.json && ./scripts/transform-world-countries.js > ./data/countries.json",
"data:emoji": "rm -rf ./data/countries-emoji.json && ./scripts/transform-world-countries.js --emoji > ./data/countries-emoji.json",
"data:cca2": "rm -rf ./data/cca2.json && ./scripts/transform-world-countries.js --cca2 > ./data/cca2.json",
"cleaning": "rm -rf node_modules && rm -f yarn.lock && yarn cache clean && yarn install && watchman watch-del-all && rm -fr $TMPDIR/react-*",
"prepublishOnly": "yarn lint && yarn build && yarn test && yarn config:npm",
"postpublish": "yarn config:dev"
Expand Down
510 changes: 510 additions & 0 deletions scripts/countryFlags.js

Large diffs are not rendered by default.

63 changes: 36 additions & 27 deletions scripts/transform-world-countries.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
import countries from 'world-countries';
import flags from '../src/countryFlags';
#!/usr/bin/env node

const isEmoji = process.argv.includes('--emoji');
const isCca2 = process.argv.includes('--cca2');
const countries = require('world-countries')
const flags = require('./countryFlags')

const getCountryNames = (common, translations) => Object
.keys(translations)
const isEmoji = process.argv.includes('--emoji')
const isCca2 = process.argv.includes('--cca2')

const getCountryNames = (common, translations) =>
Object.keys(translations)
.filter(k => k !== 'common')
.map(key => ({ [key]: translations[key].common }))
.reduce((prev, cur) => ({ ...prev, [Object.keys(cur)[0]]: cur[Object.keys(cur)[0]] }), {});
.reduce(
(prev, cur) => ({
...prev,
[Object.keys(cur)[0]]: cur[Object.keys(cur)[0]]
}),
{}
)

const newcountries = countries
.map(
({ cca2, currency, callingCode, name: { common }, translations }) => ({
[cca2]: {
currency: currency[0],
callingCode: callingCode[0],
flag: isEmoji ? `flag-${cca2.toLowerCase()}` : flags[cca2],
name: { common, ...getCountryNames(common, translations) },
},
})
)
.map(({ cca2, currency, callingCode, name: { common }, translations }) => ({
[cca2]: {
currency: currency[0],
callingCode: callingCode[0],
flag: isEmoji ? `flag-${cca2.toLowerCase()}` : flags[cca2],
name: { common, ...getCountryNames(common, translations) }
}
}))
.sort((a, b) => {
if (a[Object.keys(a)[0]].name.common === b[Object.keys(b)[0]].name.common) {
return 0;
} else if (a[Object.keys(a)[0]].name.common < b[Object.keys(b)[0]].name.common) {
return -1;
return 0
} else if (
a[Object.keys(a)[0]].name.common < b[Object.keys(b)[0]].name.common
) {
return -1
}
return 1;
return 1
})
.reduce(
(prev, cur) =>
({
(prev, cur) => ({
...prev,
[Object.keys(cur)[0]]: cur[Object.keys(cur)[0]],
[Object.keys(cur)[0]]: cur[Object.keys(cur)[0]]
}),
{});
{}
)

if (!isCca2) {
console.log(JSON.stringify(newcountries)); // eslint-disable-line
console.log(JSON.stringify(newcountries))
} else {
console.log(JSON.stringify(Object.keys(newcountries))); // eslint-disable-line
console.log(JSON.stringify(Object.keys(newcountries)))
}
9 changes: 5 additions & 4 deletions src/CountryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,12 @@ export const CountryList = (props: CountryListProps) => {
const flatListRef = useRef<FlatList<Country>>(null)
const [letter, setLetter] = useState<string>('')
const { itemHeight } = useTheme()
const indexLetter = data
.map((country: Country) => (country.name as string).substr(0, 1))
.join('')

const scrollTo = (letter: string) => {
const index = data
.map((country: Country) => (country.name as string).substr(0, 1))
.join()
.indexOf(letter)
const index = indexLetter.indexOf(letter)
setLetter(letter)
if (flatListRef.current) {
flatListRef.current!.scrollToIndex({ animated: true, index })
Expand Down
7 changes: 2 additions & 5 deletions src/CountryModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ export const CountryModal = ({
return content
}

CountryModal.defaultProps = {
withModal: true
}

CountryModal.defaultProps = {
animationType: 'slide',
animated: true
animated: true,
withModal: true
}
43 changes: 29 additions & 14 deletions src/CountryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export const getCountryName = (
]
}

const isCountryPresent = (countries: { [key in CountryCode]: Country }) => (
countryCode: CountryCode
) => !!countries[countryCode]

export const getCountries = (
flagType: FlagType,
translation: TranslationLanguageCode = 'common'
Expand All @@ -71,13 +75,17 @@ export const getCountries = (
if (!countriesRaw) {
return []
}
const countries = CountryCodeList.map((cca2: CountryCode) => ({
cca2,
...{
...countriesRaw[cca2],
name: (countriesRaw[cca2].name as TranslationLanguageCodeMap)[translation]
}
}))
const countries = CountryCodeList.filter(isCountryPresent(countriesRaw)).map(
(cca2: CountryCode) => ({
cca2,
...{
...countriesRaw[cca2],
name: (countriesRaw[cca2].name as TranslationLanguageCodeMap)[
translation
]
}
})
)

return countries
}
Expand Down Expand Up @@ -112,11 +120,18 @@ export const search = (
}
const uniq = (arr: any[]) => Array.from(new Set(arr))

export const getLetters = () =>
uniq(
CountryCodeList.map((countryCode: CountryCode) =>
getCountryName(countryCode)
.substr(0, 1)
.toLocaleUpperCase()
).sort()
export const getLetters = () => {
const countriesRaw = loadData()
if (!countriesRaw) {
return []
}
return uniq(
CountryCodeList.filter(isCountryPresent(countriesRaw))
.map((countryCode: CountryCode) =>
getCountryName(countryCode)
.substr(0, 1)
.toLocaleUpperCase()
)
.sort()
)
}
Loading

0 comments on commit e41c67c

Please sign in to comment.