Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

country-list-js

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

country-list-js - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

0

gulpfile.js

@@ -0,0 +0,0 @@ let gulp = require('gulp');

12

package.json
{
"name": "country-list-js",
"version": "1.0.0",
"version": "2.0.0",
"description": "Country list with ISO2, ISO3 code, continent, capital, dialing code",

@@ -39,3 +39,3 @@ "homepage": "https://github.com/i-rocky/country-list-js.git",

"build": "gulp",
"test": "mocha --compilers js:babel-core/register test/**/*.test.js"
"test": "mocha --require babel-core/register test/**/*.test.js"
},

@@ -46,2 +46,3 @@ "devDependencies": {

"babel-core": "^6.25.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",

@@ -66,3 +67,8 @@ "babel-preset-stage-1": "^6.24.1",

"presets": [
"es2015"
"env",
{
"targets": {
"node": "current"
}
}
]

@@ -69,0 +75,0 @@ }

@@ -20,3 +20,3 @@ ## Country List JS

```
npm install country-list-js
npm install --save country-list-js
```

@@ -38,2 +38,3 @@ ## Basic Usage

var found = country.find('BDT', country.FIND_BY_CURRENCY);
var found = country.find('880', country.FIND_BY_PHONE_NBR);
```

@@ -51,2 +52,3 @@

country.FIND_BY_CURRENCY = 5
country.FIND_BY_PHONE_NBR = 6
```

@@ -93,2 +95,20 @@

Object destructor supported since `2.0.0`
```javascript
import {findByName} from 'country-list-js';
const country = findByName('Bangladesh');
```
Available functions
* getInstance
* findByISOAlpha2
* findByISOAlpha3
* findByName
* findByCapital
* findByCurrency
* findByPhoneNbr
### npm commands

@@ -95,0 +115,0 @@

@@ -1,2 +0,2 @@

"use strict";
'use strict';

@@ -12,142 +12,221 @@ const continents = require('./../data/continents.json');

function Country () {
let countryInfo = null;
function Country() {
let countryInfo = null;
this.FIND_BY_ISO_ALPHA_2 = 1;
this.FIND_BY_ISO_ALPHA_3 = 2;
this.FIND_BY_NAME = 3;
this.FIND_BY_CAPITAL = 4;
this.FIND_BY_CURRENCY = 5;
this.FIND_BY_ISO_ALPHA_2 = 1;
this.FIND_BY_ISO_ALPHA_3 = 2;
this.FIND_BY_NAME = 3;
this.FIND_BY_CAPITAL = 4;
this.FIND_BY_CURRENCY = 5;
this.FIND_BY_PHONE_NBR = 6;
this.all = function () {
const all = [];
for(const key in names) {
if(names.hasOwnProperty(key)) {
all.push(pullDataForCountry(key));
}
}
return all;
};
this.all = function() {
const all = [];
for (const key in names) {
if (names.hasOwnProperty(key)) {
all.push(pullDataForCountry(key));
}
}
return all;
};
/**
* Looks up the needle according to flag and returns found result
*
* @param needle
* @param flag
* @returns {*}
*/
this.find = function (needle, flag = 1) {
let number = parseInt(flag);
if (number === this.FIND_BY_ISO_ALPHA_2) {
if (needle === null || needle === undefined) {
countryInfo = null;
return null;
}
countryInfo = pullDataForCountry(needle);
return countryInfo;
} else if (number === this.FIND_BY_ISO_ALPHA_3) {
return findByISO_ALPHA_3(needle);
} else if (number === this.FIND_BY_NAME) {
return findByName(needle);
} else if (number === this.FIND_BY_CAPITAL) {
return findByCapital(needle);
} else if (number === this.FIND_BY_CURRENCY) {
return findByCurrency(needle);
} else {
countryInfo = pullDataForCountry(needle);
return countryInfo;
}
};
/**
* Pulls data from previous result
*
* @param hook
* @returns {*}
*/
this.info = function (hook=null) {
if(countryInfo===null) {
return null;
}
if (hook === "iso_alpha_2") {
return countryInfo.code.iso_alpha_2;
} else if (hook === "iso_alpha_3") {
return countryInfo.code.iso_alpha_3;
} else if (hook === "name") {
return countryInfo.name;
} else if (hook === "continent") {
return countryInfo.continent;
} else if (hook === "capital") {
return countryInfo.capital;
} else if (hook === "currency") {
return countryInfo.currency;
} else if (hook === "dialing_code") {
return countryInfo.dialing_code;
} else {
return countryInfo;
}
};
/**
* Populates country data
*
* @param code
* @returns {*}
*/
function pullDataForCountry(code) {
if(names[code]===undefined)
return null;
const currencyInfo = currency_info[currency[code]];
return {
continent: continents[continent[code]],
name: names[code],
code: {
iso_alpha_2: code,
iso_alpha_3: iso_alpha_3[code]
},
capital: capital[code],
currency: {
code: currency[code],
symbol: currencyInfo.symbol,
decimal: currencyInfo.decimal
},
dialing_code: phone[code]
};
/**
* Looks up the needle according to flag and returns found result
*
* @param needle
* @param flag
* @returns {*}
*/
this.find = function(needle, flag = 1) {
let number = parseInt(flag);
if (number === this.FIND_BY_ISO_ALPHA_2) {
if (needle === null || needle === undefined) {
countryInfo = null;
return null;
}
countryInfo = pullDataForCountry(needle);
return countryInfo;
}
else if (number === this.FIND_BY_ISO_ALPHA_3) {
return findByISO_ALPHA_3(needle);
}
else if (number === this.FIND_BY_NAME) {
return findByName(needle);
}
else if (number === this.FIND_BY_CAPITAL) {
return findByCapital(needle);
}
else if (number === this.FIND_BY_CURRENCY) {
return findByCurrency(needle);
}
else if (number === this.FIND_BY_PHONE_NBR) {
return findByPhoneNbr(needle);
}
else {
countryInfo = pullDataForCountry(needle);
return countryInfo;
}
};
function findByISO_ALPHA_3(needle) {
return findInObject(iso_alpha_3, needle);
/**
* Pulls data from previous result
*
* @param hook
* @returns {*}
*/
this.info = function(hook = null) {
if (countryInfo === null) {
return null;
}
function findByName(needle) {
return findInObject(names, needle);
if (hook === 'iso_alpha_2') {
return countryInfo.code.iso_alpha_2;
}
function findByCapital(needle) {
return findInObject(capital, needle);
else if (hook === 'iso_alpha_3') {
return countryInfo.code.iso_alpha_3;
}
function findByCurrency(needle) {
return findInObject(currency, needle);
else if (hook === 'name') {
return countryInfo.name;
}
else if (hook === 'continent') {
return countryInfo.continent;
}
else if (hook === 'capital') {
return countryInfo.capital;
}
else if (hook === 'currency') {
return countryInfo.currency;
}
else if (hook === 'dialing_code') {
return countryInfo.dialing_code;
}
else {
return countryInfo;
}
};
/**
* Finds for a needle in haystack
*
* @param haystack
* @param needle
* @returns {*}
*/
function findInObject(haystack, needle) {
for(const key in haystack) {
if(haystack.hasOwnProperty(key)) {
//check if we have a match
if(haystack[key].toLowerCase()===needle.toLowerCase()) {
countryInfo = pullDataForCountry(key);
return countryInfo;
}
}
/**
* Populates country data
*
* @param code
* @returns {*}
*/
function pullDataForCountry(code) {
if (names[code] === undefined) {
return null;
}
const currencyInfo = currency_info[currency[code]];
return {
continent: continents[continent[code]],
name: names[code],
code: {
iso_alpha_2: code,
iso_alpha_3: iso_alpha_3[code],
},
capital: capital[code],
currency: {
code: currency[code],
symbol: currencyInfo.symbol,
decimal: currencyInfo.decimal,
},
dialing_code: phone[code],
};
}
function findByISO_ALPHA_3(needle) {
return findInObject(iso_alpha_3, needle);
}
function findByName(needle) {
return findInObject(names, needle);
}
function findByCapital(needle) {
return findInObject(capital, needle);
}
function findByCurrency(needle) {
return findInObject(currency, needle);
}
function findByPhoneNbr(needle) {
// make sure the phone number is clean
var nbr = needle.replace(/\D/g, '');
// create an equally clean list of prefixes
var ls = [];
for (const key in phone) {
if (phone.hasOwnProperty(key)) {
ls.push({code: key, nbr: phone[key].replace(/\D/g, '')});
}
}
// we need to match the phone number against the longest
// prefix we can find. that avoids matching against 1 (US)
// when we should be matching against 1246 (Barbados)
ls.sort((a, b) => a.nbr.length < b.nbr.length ? 1 : -1);
// now match prefixes against the phone number
for (var i = 0; i < ls.length; i++) {
if (nbr.startsWith(ls[i].nbr)) {
return pullDataForCountry(ls[i].code);
}
}
}
/**
* Finds for a needle in haystack
*
* @param haystack
* @param needle
* @returns {*}
*/
function findInObject(haystack, needle) {
for (const key in haystack) {
if (haystack.hasOwnProperty(key)) {
//check if we have a match
if (haystack[key].toLowerCase() === needle.toLowerCase()) {
countryInfo = pullDataForCountry(key);
return countryInfo;
}
countryInfo = null;
return countryInfo;
}
}
countryInfo = null;
return countryInfo;
}
}
module.exports = Country;
const instance = new Country;
export function getInstance() {
return instance;
}
export function findByISOALpha2(code) {
return getInstance().find(code, getInstance().FIND_BY_ISO_ALPHA_2);
}
export function findByISOAlpha3(code) {
return getInstance().find(code, getInstance().FIND_BY_ISO_ALPHA_3);
}
export function findByName(name) {
return getInstance().find(name, getInstance().FIND_BY_NAME);
}
export function findByCapital(capital) {
return getInstance().find(capital, getInstance().FIND_BY_CAPITAL);
}
export function findByCurrency(currency) {
return getInstance().find(currency, getInstance().FIND_BY_CURRENCY);
}
export function findByPhoneNbr(phone) {
return getInstance().find(capital, getInstance().FIND_BY_PHONE_NBR);
}
export default Country;
"use strict";
import { expect} from 'chai';
import Country from "./../src/index";
import Country, {findByCapital, findByName} from './../src/index';

@@ -42,2 +42,5 @@ describe('Country', ()=>{

});
it('Find by phone number', function() {
validate_result(country.find('+8805551212', country.FIND_BY_PHONE_NBR));
});

@@ -79,2 +82,7 @@ function validate_result(info) {

it('object destructor should work', function() {
validate_result(findByName('Bangladesh'));
validate_result(findByCapital('Dhaka'));
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc