value-lookup
v1.0.2
Published
lookup country codes and more
Downloads
3
Maintainers
Readme
#value-lookup
The "value-lookup" utility is a compilation of ISO lookups in order to make it easy to lookup countries, currencies, languages and much more based on character codes.
####Built-in support for:
- ISO_639-1
- ISO_3166-1_A2
- ISO_3166-1_A3
- ISO_4127
- UN_M.49
- DMA
- IAB
Getting Started
Install value-lookup
using npm
Installation:
$ npm install value-lookup
--
Basic usage in Javascript:
Server-side (local):
var valueLookup = require('value-lookup');
var builtInLookups = valueLookup.generator.node();
var lookupObj = valueLookup(builtInLookups);
console.log(lookupObj('ISO_3166_1_A2').lookup('AF'));
// -> "Afghanistan"
Server-side (specify lookup location):
var valueLookup = require('value-lookup');
var lookupsFromLocation = valueLookup.generator.node("path/to/lookups/");
var lookupObj = valueLookup(lookupsFromLocation)('ISO_3166_1_A2');
console.log(lookupObj.lookup('AF'));
// -> "Afghanistan"
Client-side:
var valueLookup = require('value-lookup');
var localToWebsite = valueLookup.generator.client("/path/to/lookups")
var lookupObj = valueLookup(localToWebsite)('ISO_3166_1_A2');
console.log(lookupObj.lookup('AF'));
// -> "Afghanistan"
If you choose to use a lookup
which doesn't exist in the built-in
lookups, just pass in 'custom'
as the lookup
and either supply
the JSON and use the generator.custom(JSON
), or write your own.
Pass in your own function for retrieving a file:
var valueLookup = require('value-lookup');
var customFunc = (function () {
return (function (path, callback) {
$.getJSON(path, (function (json) {
callback(null, json);
return;
}));
});
});
var remoteJson = customFunc('www.example.com/iso_3166_1_a2.json')
lookupObj = valueLookup(remoteJson)('custom');
console.log(lookupObj.lookup('AF'));
// -> "Afghanistan"
Pass in raw JSON for the lookup:
var valueLookup = require('value-lookup');
var customJson = valueLookup.generator.custom({
"AF":"Afghanistan",
"AX":"Åland Islands",
"AL":"Albania",
"DZ":"Algeria"
});
var lookupObj = valueLookup(customJson)('custom');
console.log(lookupObj.lookup('AF'));
// -> "Afghanistan"
The keys of any lookup object can be retrieved by calling keys()
.
- Using
lookupObj
from the previous example:
console.log(lookupObj.keys());
// -> ["AF", "AX", "AL", "DZ"]