country-state-picker
Advanced tools
Comparing version 1.0.5 to 1.0.6
export declare function getCountries(): any; | ||
export declare function getCountry(arg: string): any; | ||
export declare function getFilteredCountries(args: any): any; | ||
export declare function getStates(countryCode: string): any; |
@@ -8,2 +8,6 @@ "use strict"; | ||
var states_1 = __importDefault(require("./data/states")); | ||
var compare = function (country, arg) { | ||
arg = arg.toLowerCase(); | ||
return country.code.toLowerCase() == arg || country.name.toLowerCase() == arg || country.dial_code.toLowerCase() == arg; | ||
}; | ||
function getCountries() { | ||
@@ -16,19 +20,24 @@ return countries_1.default; | ||
for (var i = 0; i < countries.length; i++) { | ||
if (countries[i].code.toLowerCase() == arg) { | ||
if (compare(countries[i], arg)) { | ||
return countries[i]; | ||
} | ||
} | ||
for (var i = 0; i < countries.length; i++) { | ||
if (countries[i].name.toLowerCase() == arg) { | ||
return countries[i]; | ||
return null; | ||
} | ||
exports.getCountry = getCountry; | ||
function getFilteredCountries(args) { | ||
var countries = getCountries(); | ||
var filteredCountries = []; | ||
for (var _i = 0, args_1 = args; _i < args_1.length; _i++) { | ||
var arg = args_1[_i]; | ||
for (var i = 0; i < countries.length; i++) { | ||
if (compare(countries[i], arg)) { | ||
filteredCountries.push(countries[i]); | ||
break; | ||
} | ||
} | ||
} | ||
for (var i = 0; i < countries.length; i++) { | ||
if (countries[i].dial_code.toLowerCase() == arg) { | ||
return countries[i]; | ||
} | ||
} | ||
return null; | ||
return filteredCountries; | ||
} | ||
exports.getCountry = getCountry; | ||
exports.getFilteredCountries = getFilteredCountries; | ||
function getStates(countryCode) { | ||
@@ -35,0 +44,0 @@ try { |
29
index.js
@@ -0,1 +1,6 @@ | ||
compare = function (country, arg) { | ||
arg = arg.toLowerCase(); | ||
return country.code.toLowerCase() == arg || country.name.toLowerCase() == arg || country.dial_code.toLowerCase() == arg | ||
}; | ||
exports.getCountries = function () { | ||
@@ -10,3 +15,3 @@ let countries = require('./data/countries.json'); | ||
for (let i = 0; i < countries.length; i++) { | ||
if (countries[i].code.toLowerCase() == arg) { | ||
if (compare(countries[i], arg)) { | ||
return countries[i]; | ||
@@ -16,15 +21,19 @@ } | ||
for (let i = 0; i < countries.length; i++) { | ||
if (countries[i].name.toLowerCase() == arg) { | ||
return countries[i]; | ||
} | ||
} | ||
return null; | ||
}; | ||
for (let i = 0; i < countries.length; i++) { | ||
if (countries[i].dial_code.toLowerCase() == arg) { | ||
return countries[i]; | ||
exports.getFilteredCountries = function (args) { | ||
let countries = require('./data/countries.json'); | ||
let filteredCountries = []; | ||
for (let arg of args) { | ||
for (let i = 0; i < countries.length; i++) { | ||
if (compare(countries[i], arg)) { | ||
filteredCountries.push(countries[i]); | ||
break; | ||
} | ||
} | ||
} | ||
return null; | ||
return filteredCountries; | ||
}; | ||
@@ -31,0 +40,0 @@ |
import countries from "./data/countries"; | ||
import states from "./data/states"; | ||
const compare = function (country: any, arg: string) { | ||
arg = arg.toLowerCase(); | ||
return country.code.toLowerCase() == arg || country.name.toLowerCase() == arg || country.dial_code.toLowerCase() == arg | ||
}; | ||
export function getCountries(): any { | ||
@@ -12,3 +17,3 @@ return countries; | ||
for (let i = 0; i < countries.length; i++) { | ||
if (countries[i].code.toLowerCase() == arg) { | ||
if (compare(countries[i], arg)) { | ||
return countries[i]; | ||
@@ -18,15 +23,19 @@ } | ||
for (let i = 0; i < countries.length; i++) { | ||
if (countries[i].name.toLowerCase() == arg) { | ||
return countries[i]; | ||
} | ||
} | ||
return null; | ||
} | ||
for (let i = 0; i < countries.length; i++) { | ||
if (countries[i].dial_code.toLowerCase() == arg) { | ||
return countries[i]; | ||
export function getFilteredCountries(args: any): any { | ||
let countries = getCountries(); | ||
let filteredCountries = []; | ||
for (let arg of args) { | ||
for (let i = 0; i < countries.length; i++) { | ||
if (compare(countries[i], arg)) { | ||
filteredCountries.push(countries[i]); | ||
break; | ||
} | ||
} | ||
} | ||
return null; | ||
return filteredCountries; | ||
} | ||
@@ -33,0 +42,0 @@ |
{ | ||
"name": "country-state-picker", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "NPM package to get the list of countries and their states.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -5,2 +5,4 @@ # Country State Picker | ||
[![npm version](https://badge.fury.io/js/country-state-picker.svg)](https://badge.fury.io/js/country-state-picker) | ||
## Installation | ||
@@ -126,1 +128,37 @@ | ||
``` | ||
### `getFilteredCountries([<country_name | country_code | dial_code>])` | ||
This function will receive an array of arguments and return the array of countries corresponding to the argument. | ||
### Example | ||
To get only India, USA and Australia country objects, the function call will look like: | ||
```javascript | ||
let filteredCountries = getFilteredCountries(['+91', 'us', 'Australia']); | ||
console.log(filteredCountries) | ||
``` | ||
#### Output | ||
```javascript | ||
[ | ||
{ | ||
"name": "India", | ||
"code": "in", | ||
"dial_code": "+91" | ||
}, | ||
{ | ||
"name": "United States of America", | ||
"code": "us", | ||
"dial_code": "+1" | ||
}, | ||
{ | ||
"name": "Australia", | ||
"code": "au", | ||
"dial_code": "+61" | ||
} | ||
] | ||
``` |
'use strict'; | ||
const expect = require('chai').expect; | ||
const index = require('../dist/index.js'); | ||
describe('Country-state-picker function test', () => { | ||
it('should return Indian states', () => { | ||
// console.log(index.getCountries()); | ||
it('should return India country object', () => { | ||
const country1 = index.getCountry("in"); | ||
@@ -14,4 +13,5 @@ expect(JSON.stringify(country1)).to.equal(JSON.stringify({ | ||
})); | ||
}); | ||
it('should return India country object', () => { | ||
const country2 = index.getCountry("india"); | ||
@@ -23,6 +23,7 @@ expect(JSON.stringify(country2)).to.equal(JSON.stringify({ | ||
})); | ||
}); | ||
it('should return India country object', () => { | ||
const country3 = index.getCountry("+91"); | ||
expect(JSON.stringify(country2)).to.equal(JSON.stringify({ | ||
expect(JSON.stringify(country3)).to.equal(JSON.stringify({ | ||
"name": "India", | ||
@@ -32,3 +33,23 @@ "code": "in", | ||
})); | ||
}); | ||
it('should return India, USA and Australia country object', () => { | ||
const filteredCountries = index.getFilteredCountries(["+91", "us", "Australia"]); | ||
expect(JSON.stringify(filteredCountries)).to.equal(JSON.stringify([{ | ||
"name": "India", | ||
"code": "in", | ||
"dial_code": "+91" | ||
}, { | ||
"name": "United States of America", | ||
"code": "us", | ||
"dial_code": "+1" | ||
}, { | ||
"name": "Australia", | ||
"code": "au", | ||
"dial_code": "+61" | ||
} | ||
])); | ||
}); | ||
it('should return array of Indian states', () => { | ||
const states = index.getStates("in"); | ||
@@ -35,0 +56,0 @@ expect(JSON.stringify(states)).to.equal(JSON.stringify([ |
@@ -7,3 +7,6 @@ { | ||
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ | ||
"lib": ["es2015"], /* Specify library files to be included in the compilation. */ | ||
"lib": [ | ||
"es2015", | ||
"dom" | ||
], /* Specify library files to be included in the compilation. */ | ||
// "allowJs": true, /* Allow javascript files to be compiled. */ | ||
@@ -10,0 +13,0 @@ // "checkJs": true, /* Report errors in .js files. */ |
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
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
369027
16006
163