i18n-iso-countries
Advanced tools
Comparing version 5.5.0 to 6.0.0
export type LocalizedCountryNames = { | ||
[alpha2Key: string]: string | ||
[alpha2Key: string]: string | string[] | ||
}; | ||
@@ -4,0 +4,0 @@ |
47
index.js
@@ -150,4 +150,5 @@ "use strict"; | ||
try { | ||
var d = registeredLocales[lang.toLowerCase()]; | ||
return d[toAlpha2(code)]; | ||
const codeMaps = registeredLocales[lang.toLowerCase()]; | ||
const names = codeMaps[toAlpha2(code)]; | ||
return Array.isArray(names) ? names[0] : names; | ||
} catch (err) { | ||
@@ -176,10 +177,23 @@ return undefined; | ||
exports.getAlpha2Code = function(name, lang) { | ||
const normalizeString = (string) => string.toLowerCase(); | ||
const areSimilar = (a, b) => normalizeString(a) === normalizeString(b); | ||
try { | ||
var p, codenames = registeredLocales[lang.toLowerCase()]; | ||
for (p in codenames) { | ||
if (codenames.hasOwnProperty(p)) { | ||
if (codenames[p].toLowerCase() === name.toLowerCase()) { | ||
const codenames = registeredLocales[lang.toLowerCase()]; | ||
for (const p in codenames) { | ||
if (!codenames.hasOwnProperty(p)) { | ||
continue; | ||
} | ||
if (typeof codenames[p] === "string") { | ||
if (areSimilar(codenames[p], name)) { | ||
return p; | ||
} | ||
} | ||
if (Array.isArray(codenames[p])) { | ||
for (const mappedName of codenames[p]) { | ||
if (areSimilar(mappedName, name)) { | ||
return p; | ||
} | ||
} | ||
} | ||
} | ||
@@ -198,10 +212,23 @@ return undefined; | ||
exports.getSimpleAlpha2Code = function(name, lang) { | ||
const normalizeString = (string) => removeDiacritics(string.toLowerCase()); | ||
const areSimilar = (a, b) => normalizeString(a) === normalizeString(b); | ||
try { | ||
var p, codenames = registeredLocales[lang.toLowerCase()]; | ||
for (p in codenames) { | ||
if (codenames.hasOwnProperty(p)) { | ||
if (removeDiacritics(codenames[p].toLowerCase()) === removeDiacritics(name.toLowerCase())) { | ||
const codenames = registeredLocales[lang.toLowerCase()]; | ||
for (const p in codenames) { | ||
if (!codenames.hasOwnProperty(p)) { | ||
continue; | ||
} | ||
if (typeof codenames[p] === "string") { | ||
if (areSimilar(codenames[p], name)) { | ||
return p; | ||
} | ||
} | ||
if (Array.isArray(codenames[p])) { | ||
for (const mappedName of codenames[p]) { | ||
if (areSimilar(mappedName, name)) { | ||
return p; | ||
} | ||
} | ||
} | ||
} | ||
@@ -208,0 +235,0 @@ return undefined; |
@@ -179,3 +179,3 @@ { | ||
"RO": "Romania", | ||
"RU": "Russian Federation", | ||
"RU": ["Russian Federation", "Russia"], | ||
"RW": "Rwanda", | ||
@@ -182,0 +182,0 @@ "SH": "Saint Helena", |
@@ -72,3 +72,3 @@ { | ||
"ES": "Espanha", | ||
"US": "Estados Unidos", | ||
"US": ["Estados Unidos", "Estados Unidos da América"], | ||
"EE": "Estônia", | ||
@@ -75,0 +75,0 @@ "ET": "Etiópia", |
{ | ||
"name": "i18n-iso-countries", | ||
"version": "5.5.0", | ||
"version": "6.0.0", | ||
"description": "i18n for ISO 3166-1 country codes", | ||
@@ -5,0 +5,0 @@ "typings": "index.d.ts", |
@@ -131,3 +131,3 @@ var assert = require("assert"); | ||
}); | ||
it("missing land", function() { | ||
it("missing lang", function() { | ||
assert.strictEqual(i18niso.getAlpha2Code("Deutschland", "xx"), undefined); | ||
@@ -138,4 +138,4 @@ }); | ||
it("works", function() { | ||
assert.strictEqual(i18niso.getSimpleAlpha2Code("belgie", "nl"), 'BE'); | ||
assert.strictEqual(i18niso.getSimpleAlpha2Code("België", "nl"), 'BE'); | ||
assert.strictEqual(i18niso.getSimpleAlpha2Code("belgie", "nl"), "BE"); | ||
assert.strictEqual(i18niso.getSimpleAlpha2Code("België", "nl"), "BE"); | ||
}); | ||
@@ -145,5 +145,8 @@ it("missing name", function() { | ||
}); | ||
it("missing land", function() { | ||
it("missing lang", function() { | ||
assert.strictEqual(i18niso.getSimpleAlpha2Code("Deutschland", "xx"), undefined); | ||
}); | ||
it("alternative name spellings", function() { | ||
assert.strictEqual(i18niso.getSimpleAlpha2Code("Estados Unidos da América", "pt"), "US"); | ||
}); | ||
}); | ||
@@ -154,3 +157,3 @@ describe("getAlpha3Code", function() { | ||
}); | ||
it("missing land", function() { | ||
it("missing lang", function() { | ||
assert.strictEqual(i18niso.getAlpha3Code("Deutschland", "xx"), undefined); | ||
@@ -161,4 +164,4 @@ }); | ||
it("works", function() { | ||
assert.strictEqual(i18niso.getSimpleAlpha3Code("belgie", "nl"), 'BEL'); | ||
assert.strictEqual(i18niso.getSimpleAlpha3Code("België", "nl"), 'BEL'); | ||
assert.strictEqual(i18niso.getSimpleAlpha3Code("belgie", "nl"), "BEL"); | ||
assert.strictEqual(i18niso.getSimpleAlpha3Code("België", "nl"), "BEL"); | ||
}); | ||
@@ -168,5 +171,8 @@ it("missing name", function() { | ||
}); | ||
it("missing land", function() { | ||
it("missing lang", function() { | ||
assert.strictEqual(i18niso.getSimpleAlpha3Code("Deutschland", "xx"), undefined); | ||
}); | ||
it("alternative name spellings", function() { | ||
assert.strictEqual(i18niso.getSimpleAlpha3Code("Estados Unidos da América", "pt"), "USA"); | ||
}); | ||
}); | ||
@@ -395,2 +401,31 @@ describe("isValid", function() { | ||
}); | ||
describe("pt", function () { | ||
var lang = "pt"; | ||
describe("get Alpha-2 code", function() { | ||
it("nameToAlpha2 Estados Unidos => US", function() { | ||
assert.strictEqual(i18niso.getAlpha2Code("Estados Unidos", lang), "US"); | ||
}); | ||
it("nameToAlpha2 Estados Unidos da América => US", function() { | ||
assert.strictEqual(i18niso.getAlpha2Code("Estados Unidos da América", lang), "US"); | ||
}); | ||
}); | ||
describe("get Alpha-3 code", function() { | ||
it("nameToAlpha3 Estados Unidos => USA", function() { | ||
assert.strictEqual(i18niso.getAlpha3Code("Estados Unidos", lang), "USA"); | ||
}); | ||
it("nameToAlpha3 Estados Unidos da América => USA", function() { | ||
assert.strictEqual(i18niso.getAlpha3Code("Estados Unidos da América", lang), "USA"); | ||
}); | ||
}); | ||
describe("get name", function () { | ||
it("for br => Brasil", function () { | ||
assert.strictEqual(i18niso.getName("br", lang), "Brasil"); | ||
}); | ||
it("for us => Estados Unidos", function () { | ||
assert.strictEqual(i18niso.getName("us", lang), "Estados Unidos"); | ||
}); | ||
}); | ||
}); | ||
describe("unsupported language", function () { | ||
@@ -397,0 +432,0 @@ var lang = "unsupported"; |
436807
14568