country-list-js
Advanced tools
Comparing version 3.0.0 to 3.1.0
{ | ||
"name": "country-list-js", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Country list with ISO2, ISO3 code, continent, capital, dialing code", | ||
@@ -38,16 +38,17 @@ "homepage": "https://github.com/i-rocky/country-list-js.git", | ||
"scripts": { | ||
"build": "webpack --config ./webpack.config.js", | ||
"test": "mocha --require babel-core/register test/**/*.test.js" | ||
"build": "gulp", | ||
"test": "mocha test/**/*.test.js" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.3", | ||
"babel-loader": "7", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"browserify": "^16.2.3", | ||
"chai": "^4.0.2", | ||
"gulp": "^3.9.1", | ||
"gulp-rename": "^1.2.2", | ||
"gulp-sourcemaps": "^2.6.0", | ||
"gulp-streamify": "^1.0.2", | ||
"gulp-uglify-es": "^1.0.4", | ||
"mocha": "^3.4.2", | ||
"webpack": "^4.29.6", | ||
"webpack-cli": "^3.3.0" | ||
"vinyl-buffer": "^1.0.1", | ||
"vinyl-source-stream": "^2.0.0" | ||
} | ||
} |
# Country List JS | ||
[![npm version](https://badge.fury.io/js/country-list-js.svg)](https://badge.fury.io/js/country-list-js) | ||
[![Build Status](https://travis-ci.org/i-rocky/country-list-js.svg?branch=master)](https://travis-ci.org/i-rocky/country-list-js) [![Version](https://img.shields.io/npm/v/country-list-js.svg)](https://www.npmjs.com/package/country-list-js) | ||
@@ -86,5 +87,6 @@ [![Total Downloads](https://img.shields.io/npm/dt/country-list-js.svg)](https://www.npmjs.com/package/country-list-js) | ||
and to build the project: | ||
and to build the minified file for web run: | ||
``` | ||
npm run build | ||
``` | ||
and retrieve it from `dist/country.min.js` |
154
src/index.js
'use strict'; | ||
var self = module.exports = { | ||
all: {}, | ||
cache: {}, | ||
findByIso2: code => x(self.all[code]), | ||
findByIso3: code => find('iso3', code), | ||
findByName: name => find('name', name), | ||
findByCapital: name => find('capital', name), | ||
findByCurrency: code => find('currency', code), | ||
findByProvince(name) { | ||
if (!self.cache.province) self.cache.province = {}; | ||
if (self.cache.province[name]) | ||
return self.cache.province[name].map(o => x(o)); | ||
all: {}, | ||
cache: {}, | ||
findByIso2: code => x(self.all[code]), | ||
findByIso3: code => find('iso3', code), | ||
findByName: name => find('name', name), | ||
findByCapital: name => find('capital', name), | ||
findByCurrency: code => find('currency', code), | ||
findByProvince(name) { | ||
if (!self.cache.province) { | ||
self.cache.province = {}; | ||
} | ||
if (self.cache.province[name]) { | ||
return self.cache.province[name].map(o => x(o)); | ||
} | ||
return self.cache.province[name] = Object.keys(self.all) | ||
.map(k => self.all[k]) | ||
.filter(o => o.provinces) | ||
.filter(o => o.provinces.filter( | ||
o => o.name == name || (o.alias || []).indexOf(name) > -1 | ||
).length > 0 | ||
) | ||
.map(o => x(o)) | ||
.unpack(undefined); | ||
}, | ||
findByPhoneNbr(nbr) { | ||
// make sure the phone number is clean | ||
nbr = nbr.replace(/\D/g, ''); | ||
return self.cache.province[name] = Object.keys(self.all) | ||
.map(k => self.all[k]) | ||
.filter(o => o.provinces) | ||
.filter(o => o.provinces.filter( | ||
o => o.name == name || (o.alias || []).indexOf(name) > -1, | ||
).length > 0, | ||
) | ||
.map(o => x(o)) | ||
.unpack(undefined); | ||
}, | ||
findByPhoneNbr(nbr) { | ||
// make sure the phone number is clean | ||
nbr = nbr.replace(/\D/g, ''); | ||
// now match prefixes against the phone number | ||
return phones.filter(o => o.nbr && nbr.startsWith(o.nbr)) | ||
.map(o => x(self.all[o.code])) | ||
.unpack(undefined); | ||
} | ||
// now match prefixes against the phone number | ||
return phones.filter(o => o.nbr && nbr.startsWith(o.nbr)) | ||
.map(o => x(self.all[o.code])) | ||
.unpack(undefined); | ||
}, | ||
}; | ||
@@ -40,26 +43,34 @@ | ||
function x(o) { | ||
if (!o) return; | ||
if (Array.isArray(o)) return o.map(x(o)); | ||
if (!o) { | ||
return; | ||
} | ||
if (Array.isArray(o)) { | ||
return o.map(x(o)); | ||
} | ||
var ret = Object.assign({}, o); | ||
ret.currency = { | ||
code: ret.currency, | ||
symbol: ret.currency_symbol, | ||
decimal: ret.currency_decimal | ||
}; | ||
ret.code = {iso2: ret.iso2, iso3: ret.iso3} | ||
for (var k of 'iso2|iso3|currency_symbol|currency_decimal'.split('|')) | ||
delete ret[k]; | ||
return ret; | ||
var ret = Object.assign({}, o); | ||
ret.currency = { | ||
code: ret.currency, | ||
symbol: ret.currency_symbol, | ||
decimal: ret.currency_decimal, | ||
}; | ||
ret.code = {iso2: ret.iso2, iso3: ret.iso3}; | ||
for (var k of 'iso2|iso3|currency_symbol|currency_decimal'.split('|')) { | ||
delete ret[k]; | ||
} | ||
return ret; | ||
} | ||
function find(prop, val) { | ||
if (!(prop in self.cache)) self.cache[prop] = {}; | ||
if (self.cache[prop][val]) | ||
return x(self.cache[prop][val]); | ||
if (!(prop in self.cache)) { | ||
self.cache[prop] = {}; | ||
} | ||
if (self.cache[prop][val]) { | ||
return x(self.cache[prop][val]); | ||
} | ||
return self.cache[prop][val] = Object.keys(self.all) | ||
.filter(k => self.all[k][prop] == val) | ||
.map(k => x(self.all[k])) | ||
.unpack(undefined); | ||
return self.cache[prop][val] = Object.keys(self.all) | ||
.filter(k => self.all[k][prop] == val) | ||
.map(k => x(self.all[k])) | ||
.unpack(undefined); | ||
} | ||
@@ -80,16 +91,17 @@ | ||
for (var k in iso_alpha_3) | ||
self.all[k] = { | ||
iso2: k, | ||
iso3: iso_alpha_3[k], | ||
name: names[k], | ||
continent: continents[continent[k]], | ||
region: regions[k], | ||
capital: capital[k], | ||
currency: currency[k], | ||
currency_symbol: currency_info[currency[k]].symbol, | ||
currency_decimal: currency_info[currency[k]].decimal, | ||
dialing_code: phone[k], | ||
provinces: provinces[k] | ||
}; | ||
Object.keys(iso_alpha_3).forEach(function(k) { | ||
self.all[k] = { | ||
iso2: k, | ||
iso3: iso_alpha_3[k], | ||
name: names[k], | ||
continent: continents[continent[k]], | ||
region: regions[k], | ||
capital: capital[k], | ||
currency: currency[k], | ||
currency_symbol: currency_info[currency[k]].symbol, | ||
currency_decimal: currency_info[currency[k]].decimal, | ||
dialing_code: phone[k], | ||
provinces: provinces[k], | ||
}; | ||
}); | ||
@@ -105,5 +117,5 @@ // release memory (except for phone) | ||
var phones = []; | ||
for (var k in phone) | ||
phones.push({code: k, nbr: phone[k].replace(/\D/g, '')}) | ||
var phones = Object.keys(phone).map(function(k) { | ||
return {code: k, nbr: phone[k].replace(/\D/g, '')}; | ||
}); | ||
@@ -114,11 +126,11 @@ // we need to match the phone number against the longest | ||
phones.sort((a,b) => a.nbr.length < b.nbr.length ? 1 : -1); | ||
phones.sort((a, b) => a.nbr.length < b.nbr.length ? 1 : -1); | ||
phone = null; | ||
Array.prototype.unpack = function() { | ||
var l = this.length; | ||
return l == 1 ? this[0] | ||
: l == 0 && arguments.length > 0 | ||
? undefined | ||
: this; | ||
var l = this.length; | ||
return l == 1 ? this[0] | ||
: l == 0 && arguments.length > 0 | ||
? undefined | ||
: this; | ||
}; |
'use strict'; | ||
import {expect} from 'chai'; | ||
import country from './../src/index'; | ||
import {ok} from 'assert'; | ||
const expect = require('chai').expect; | ||
const ok = require('assert').ok; | ||
// this prototype tests that property loops in the | ||
// module are safe. the prototype must be installed | ||
// before the module is required | ||
Object.prototype.__test_function__ = () => null; | ||
// require the module | ||
const country = require('../src/index') | ||
describe('Country', () => { | ||
@@ -8,0 +18,0 @@ var DK = { |
Sorry, the diff of this file is too big to display
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
719579
3089
91
10
1
2