ohitsdaniel / country.

countryinfo provides all kinds of data on countries.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Update search country (case insensitive)

AndreiSoroka opened this issue · comments

From discussions #3 (review)
and #3 (comment):

Regarding the name lookup, couldn't we do something along the lines of:

// helpers 
function findByName(name, language) {
        let lowercasedName = name.toLowercase();

	let key = Object.keys(data.code.ISO2).find((element) => {
		if (typeof data.code.ISO2[element].names[language] === 'undefined') {
			throw 'INVALIDLANGUAGE: ' + language + 'not supported.'
		}

		return data.code
		    .ISO2[element]
		    .names[language]
		    .map((name) => { name.toLowerCase() })
		    .includes(lowercasedName);
	});

	return data.code.ISO2[key];
}

@ohitsdaniel
P.s. I think throw is not a good idea.
if it was a promise then it would be catch for throw.

But in the just function, you should use try/catch

Example:

let result;
try { result =  country.findByName('test') } catch (e){}
if (result) alert('try');

/////// or
const result =  country.findByName('test');
if (result) alert('try');