i18n-iso-countries
Advanced tools
Comparing version 0.0.2 to 0.1.0
16
de.js
@@ -18,3 +18,2 @@ var i18n = { | ||
"AW": "Aruba", | ||
"AC": "Ascension", | ||
"AZ": "Aserbaidschan", | ||
@@ -44,8 +43,5 @@ "ET": "Äthiopien", | ||
"BF": "Burkina Faso", | ||
"BU": "Burma", | ||
"BI": "Burundi", | ||
"EA": "Ceuta", | ||
"CL": "Chile", | ||
"CN": "China", | ||
"CP": "Clipperton", | ||
"CK": "Cookinseln", | ||
@@ -57,3 +53,2 @@ "CR": "Costa Rica", | ||
"DE": "Deutschland", | ||
"DG": "Diego Garcia", | ||
"DM": "Dominica", | ||
@@ -66,4 +61,2 @@ "DO": "Dominikanische Republik", | ||
"EE": "Estland", | ||
"CE": "Europäische Gemeinschaft", | ||
"EU": "Europäische Union", | ||
"FK": "Falklandinseln", | ||
@@ -74,3 +67,2 @@ "FO": "Färöer", | ||
"FR": "Frankreich", | ||
"FX": "Frankreich", | ||
"GF": "Französisch-Guayana", | ||
@@ -112,3 +104,2 @@ "PF": "Französisch-Polynesien", | ||
"JO": "Jordanien", | ||
"YU": "Jugoslawien", | ||
"KY": "Kaimaninseln", | ||
@@ -118,3 +109,2 @@ "KH": "Kambodscha", | ||
"CA": "Kanada", | ||
"IC": "Kanarische Inseln", | ||
"CV": "Kap Verde", | ||
@@ -172,6 +162,4 @@ "KZ": "Kasachstan", | ||
"NZ": "Neuseeland", | ||
"NT": "Neutrale Zone", | ||
"NI": "Nicaragua", | ||
"NL": "Niederlande", | ||
"AN": "Niederländische Antillen", | ||
"NE": "Niger", | ||
@@ -216,3 +204,2 @@ "NG": "Nigeria", | ||
"RS": "Serbien", | ||
"CS": "Tschechoslowakei", | ||
"SC": "Seychellen", | ||
@@ -248,3 +235,2 @@ "SL": "Sierra Leone", | ||
"TT": "Trinidad und Tobago", | ||
"TA": "Tristan da Cunha", | ||
"TD": "Tschad", | ||
@@ -257,3 +243,2 @@ "CZ": "Tschechische Republik", | ||
"TV": "Tuvalu", | ||
"SU": "UdSSR", | ||
"UG": "Uganda", | ||
@@ -275,3 +260,2 @@ "UA": "Ukraine", | ||
"EH": "Westsahara", | ||
"ZR": "Zaire", | ||
"CF": "Zentralafrikanische Republik", | ||
@@ -278,0 +262,0 @@ "CY": "Zypern" |
16
en.js
@@ -152,3 +152,2 @@ var i18n = { | ||
"NL": "Netherlands", | ||
"AN": "Netherlands Antilles", | ||
"NC": "New Caledonia", | ||
@@ -191,3 +190,2 @@ "NZ": "New Zealand", | ||
"SN": "Senegal", | ||
"CS": "Serbia and Montenegro", | ||
"SC": "Seychelles", | ||
@@ -242,3 +240,15 @@ "SL": "Sierra Leone", | ||
"ZM": "Zambia", | ||
"ZW": "Zimbabwe" | ||
"ZW": "Zimbabwe", | ||
"AX": "Åland Islands", | ||
"BQ": "Bonaire, Sint Eustatius and Saba", | ||
"CW": "Curaçao", | ||
"GG": "Guernsey", | ||
"IM": "Isle of Man", | ||
"JE": "Jersey", | ||
"ME": "Montenegro", | ||
"BL": "Saint Barthélemy", | ||
"MF": "Saint Martin (French part)", | ||
"RS": "Serbia", | ||
"SX": "Sint Maarten (Dutch part)", | ||
"SS": "South Sudan" | ||
}; | ||
@@ -245,0 +255,0 @@ |
100
index.js
@@ -0,11 +1,74 @@ | ||
var fs = require("fs"), | ||
path = require("path"); | ||
/* | ||
* iso: iso country code to resolve | ||
* lang: language for country name | ||
*/ | ||
exports.getName = function(iso, lang) { | ||
* All codes map to ISO 3166-1 alpha-2 | ||
*/ | ||
var alpha2 = [], | ||
alpha3 = {}, | ||
numeric = {}; | ||
/*jslint stupid: true */ | ||
fs.readFileSync(path.resolve(__dirname, "codes.csv"), {encoding: "utf8"}).replace(/\r/g, "").split("\n").forEach(function(line) { | ||
"use strict"; | ||
var s = line.split(";"); | ||
alpha2.push(s[0]); | ||
alpha3[s[1]] = s[0]; | ||
numeric[parseInt(s[2], 10)] = s[0]; | ||
}); | ||
/*jslint stupid: false */ | ||
/* | ||
* @param code Alpha-3 code | ||
* @return Alpha-2 code or undefined | ||
*/ | ||
function alpha3ToAlpha2(code) { | ||
"use strict"; | ||
return alpha3[code]; | ||
} | ||
exports.alpha3ToAlpha2 = alpha3ToAlpha2; | ||
/* | ||
* @param code Numeric code | ||
* @return Alpha-2 code or undefined | ||
*/ | ||
function numericToAlpha2(code) { | ||
"use strict"; | ||
return numeric[parseInt(code, 10)]; | ||
} | ||
exports.numericToAlpha2 = numericToAlpha2; | ||
/* | ||
* @param code ISO 3166-1 alpha-2, alpha-3 or numeric code | ||
* @return ISO 3166-1 alpha-2 | ||
*/ | ||
function toAlpha2(code) { | ||
"use strict"; | ||
if (typeof code === "string") { | ||
if (/^[0-9]*$/.test(code)) { | ||
return numericToAlpha2(code); | ||
} | ||
if (code.length === 2) { | ||
return code.toUpperCase(); | ||
} | ||
if(code.length === 3) { | ||
return alpha3ToAlpha2(code.toUpperCase()); | ||
} | ||
} | ||
if (typeof code === "number") { | ||
return numericToAlpha2(code); | ||
} | ||
return undefined; | ||
} | ||
exports.toAlpha2 = toAlpha2; | ||
/* | ||
* @param code ISO 3166-1 alpha-2, alpha-3 or numeric code | ||
* @param lang language for country name | ||
* @return name or undefined | ||
*/ | ||
exports.getName = function(code, lang) { | ||
"use strict"; | ||
try { | ||
var l = require("./" + lang.toLowerCase()); | ||
return l.i18n()[iso.toUpperCase()]; | ||
return l.i18n()[toAlpha2(code)]; | ||
} catch (err) { | ||
@@ -16,5 +79,5 @@ return undefined; | ||
/* | ||
* lang: language for country name | ||
* @param lang language for country name | ||
* @return hash | ||
*/ | ||
@@ -31,1 +94,24 @@ exports.getNames = function(lang) { | ||
/* | ||
* @return Array | ||
*/ | ||
exports.getAlpha2Codes = function() { | ||
"use strict"; | ||
return alpha2; | ||
}; | ||
/* | ||
* @return hash | ||
*/ | ||
exports.getAlpha3Codes = function() { | ||
"use strict"; | ||
return alpha3; | ||
}; | ||
/* | ||
* @return hash | ||
*/ | ||
exports.getNumericCodes = function() { | ||
"use strict"; | ||
return numeric; | ||
}; |
{ | ||
"name": "i18n-iso-countries", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "i18n for ISO 3166-1 country codes", | ||
"keywords": [ | ||
"i18n", | ||
"iso", | ||
"3166", | ||
"iso 3166", | ||
"iso 3166-1", | ||
"alpha", | ||
"alpha-2", | ||
"alpha-3", | ||
"numeric", | ||
"country", | ||
@@ -10,0 +16,0 @@ "countries", |
[![Build Status](https://secure.travis-ci.org/cinovo/node-i18n-iso-countries.png)](http://travis-ci.org/cinovo/node-i18n-iso-countries) | ||
[![NPM version](https://badge.fury.io/js/i18n-iso-countries.png)](http://badge.fury.io/js/i18n-iso-countries) | ||
#i18n-iso-countries | ||
# i18n-iso-countries | ||
i18n for ISO 3166-1 country codes | ||
i18n for ISO 3166-1 country codes. We support Alpha-2, Alpha-3 and Numeric codes from http://en.wikipedia.org/wiki/ISO_3166-1#Officially_assigned_code_elements | ||
## usage | ||
## Code to Country | ||
### get the name of a country by it's ISO 3166-1 country code | ||
### Get the name of a country by it's ISO 3166-1 Alpha-2, Alpha-3 or Numeric code | ||
`````javascript | ||
var countries = require("i18n-iso-countries"); | ||
console.log("US (Alpha-2) => " + countries.getName("US", "en")); // United States | ||
console.log("US (Alpha-2) => " + countries.getName("US", "de")); // Vereinigte Staaten von Amerika | ||
console.log("USA (Alpha-3) => " + countries.getName("USA", "en")); // United States | ||
console.log("USA (Numeric) => " + countries.getName("840", "en")); // United States | ||
````` | ||
### Get all names by their ISO 3166-1 Alpha-2 code | ||
`````javascript | ||
var countries = require("i18n-iso-countries"); | ||
console.log("US => " + countries.getName("US", "en")); // US => United States | ||
console.log("US => " + countries.getName("US", "de")); // US => Vereinigte Staaten von Amerika | ||
console.log(countries.getNames("en")); // { 'AF': 'Afghanistan', 'AL': 'Albania', [...], 'ZM': 'Zambia', 'ZW': 'Zimbabwe' } | ||
````` | ||
### Supported languages | ||
* `en`: english | ||
* `de`: german | ||
## Codes | ||
### Convert Alpha-3 to Alpha-2 code | ||
`````javascript | ||
var countries = require("i18n-iso-countries"); | ||
console.log("USA (Alpha-3) => " + countries.alpha3ToAlpha2("USA") + " (Alpha-2)"); // United States | ||
````` | ||
### get the names of all countries by their ISO 3166-1 country code | ||
### Convert Numeric to Alpha-2 code | ||
`````javascript | ||
var countries = require("i18n-iso-countries"); | ||
console.log("840 (Numeric) => " + countries.numericToAlpha2("840") + " (Alpha-2)"); // United States | ||
````` | ||
### Get all Alpha-2 codes | ||
`````javascript | ||
var countries = require("i18n-iso-countries"); | ||
console.log(countries.getNames("en")); // { AF: 'Afghanistan', AL: 'Albania', [...], ZM: 'Zambia', ZW: 'Zimbabwe' } | ||
console.log(countries.getAlpha2Codes()); // [ 'AF', 'AX', [...], 'ZM', 'ZW' } | ||
````` | ||
### Get all Alpha-3 codes | ||
`````javascript | ||
var countries = require("i18n-iso-countries"); | ||
console.log(countries.getAlpha3Codes()); // { 'AFG': 'AF', 'ALA': 'AX', [...], 'ZMB': 'ZM', 'ZWE': 'ZW' } | ||
````` | ||
## supported languages | ||
### Get all Numeric codes | ||
* `en`: english | ||
* `de`: german | ||
`````javascript | ||
var countries = require("i18n-iso-countries"); | ||
console.log(countries.getNumericCodes()); // { '4': 'AF', '8': 'AL', [...], '887': 'YE', '894': 'ZM' } | ||
````` |
@@ -6,32 +6,59 @@ var assert = require("assert-plus"), | ||
"use strict"; | ||
describe("Alpha-3 to Alpha-2 code", function() { | ||
it("toAlpha2 DEU => DE", function() { | ||
assert.equal(i18niso.toAlpha2("DEU"), "DE"); | ||
}); | ||
it("alpha3ToAlpha2 DEU => DE", function() { | ||
assert.equal(i18niso.alpha3ToAlpha2("DEU"), "DE"); | ||
}); | ||
}); | ||
describe("Numeric to Alpha-2 code", function() { | ||
it("toAlpha2 '276' => DE", function() { | ||
assert.equal(i18niso.toAlpha2("276"), "DE"); | ||
}); | ||
it("toAlpha2 '004' => AF", function() { | ||
assert.equal(i18niso.toAlpha2("004"), "AF"); | ||
}); | ||
it("toAlpha2 276 => DE", function() { | ||
assert.equal(i18niso.toAlpha2(276), "DE"); | ||
}); | ||
it("numericToAlpha2 '276' => DE", function() { | ||
assert.equal(i18niso.numericToAlpha2("276"), "DE"); | ||
}); | ||
it("numericToAlpha2 '004' => AF", function() { | ||
assert.equal(i18niso.numericToAlpha2("004"), "AF"); | ||
}); | ||
it("numericToAlpha2 276 => DE", function() { | ||
assert.equal(i18niso.numericToAlpha2(276), "DE"); | ||
}); | ||
}); | ||
describe("de", function () { | ||
var lang = "de"; | ||
describe("get name", function () { | ||
it("for de", function (done) { | ||
assert.string(i18niso.getName("de", lang), "Deutschland"); | ||
done(); | ||
it("for de", function () { | ||
assert.equal(i18niso.getName("de", lang), "Deutschland"); | ||
}); | ||
it("for cl", function (done) { | ||
assert.string(i18niso.getName("cl", lang), "Chile"); | ||
done(); | ||
it("for cl", function () { | ||
assert.equal(i18niso.getName("cl", lang), "Chile"); | ||
}); | ||
it("for CL", function (done) { | ||
assert.string(i18niso.getName("CL", lang), "Chile"); | ||
done(); | ||
it("for CL", function () { | ||
assert.equal(i18niso.getName("CL", lang), "Chile"); | ||
}); | ||
it("for cy", function (done) { | ||
assert.string(i18niso.getName("cy", lang), "Zypern"); | ||
done(); | ||
it("for cy", function () { | ||
assert.equal(i18niso.getName("cy", lang), "Zypern"); | ||
}); | ||
it("for af", function (done) { | ||
assert.string(i18niso.getName("af", lang), "Afghanistan"); | ||
done(); | ||
it("for af", function () { | ||
assert.equal(i18niso.getName("af", lang), "Afghanistan"); | ||
}); | ||
}); | ||
describe("get names", function () { | ||
it("for de", function (done) { | ||
assert.equal(Object.keys(i18niso.getNames(lang)).length, 265); | ||
done(); | ||
it("complete (to less)", function() { | ||
i18niso.getAlpha2Codes().forEach(function(code) { | ||
assert.notEqual(i18niso.getName(code, lang), undefined, "missing entry for " + code); | ||
}); | ||
}); | ||
it("complete (too much)", function() { | ||
Object.keys(i18niso.getNames(lang)).forEach(function(code) { | ||
assert.notEqual(i18niso.getAlpha2Codes().indexOf(code), -1, "entry for " + code + " is too much"); | ||
}); | ||
}); | ||
}); | ||
@@ -41,45 +68,38 @@ describe("en", function () { | ||
describe("get name", function () { | ||
it("for de", function (done) { | ||
assert.string(i18niso.getName("de", lang), "Germany"); | ||
done(); | ||
it("for de", function () { | ||
assert.equal(i18niso.getName("de", lang), "Germany"); | ||
}); | ||
it("for cl", function (done) { | ||
assert.string(i18niso.getName("cl", lang), "Chile"); | ||
done(); | ||
it("for cl", function () { | ||
assert.equal(i18niso.getName("cl", lang), "Chile"); | ||
}); | ||
it("for CL", function (done) { | ||
assert.string(i18niso.getName("CL", lang), "Chile"); | ||
done(); | ||
it("for CL", function () { | ||
assert.equal(i18niso.getName("CL", lang), "Chile"); | ||
}); | ||
it("for cy", function (done) { | ||
assert.string(i18niso.getName("cy", lang), "Cypris"); | ||
done(); | ||
it("for cy", function () { | ||
assert.equal(i18niso.getName("cy", lang), "Cyprus"); | ||
}); | ||
it("for af", function (done) { | ||
assert.string(i18niso.getName("af", lang), "Afghanistan"); | ||
done(); | ||
it("for af", function () { | ||
assert.equal(i18niso.getName("af", lang), "Afghanistan"); | ||
}); | ||
}); | ||
describe("get names", function () { | ||
it("for de", function (done) { | ||
assert.equal(Object.keys(i18niso.getNames(lang)).length, 239); | ||
done(); | ||
it("complete (to less)", function() { | ||
i18niso.getAlpha2Codes().forEach(function(code) { | ||
assert.notEqual(i18niso.getName(code, lang), undefined, "missing entry for " + code); | ||
}); | ||
}); | ||
it("complete (too much)", function() { | ||
Object.keys(i18niso.getNames(lang)).forEach(function(code) { | ||
assert.notEqual(i18niso.getAlpha2Codes().indexOf(code), -1, "entry for " + code + " is too much"); | ||
}); | ||
}); | ||
}); | ||
describe("unsupported language", function () { | ||
var lang = "unsupported"; | ||
describe("get name", function () { | ||
it("for de", function (done) { | ||
assert.equal(i18niso.getName("de", lang), undefined); | ||
done(); | ||
}); | ||
it("get name", function () { | ||
assert.equal(i18niso.getName("de", lang), undefined); | ||
}); | ||
describe("get names", function () { | ||
it("for de", function (done) { | ||
assert.equal(Object.keys(i18niso.getNames(lang)).length, 0); | ||
done(); | ||
}); | ||
it("get names", function () { | ||
assert.equal(Object.keys(i18niso.getNames(lang)).length, 0); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27659
11
717
69
3