country-state-city
Advanced tools
Comparing version 2.0.0-beta-1 to 2.0.0
import { ICountry, ICity, IState } from './src/interface'; | ||
export { ICountry, ICity, IState } from './src/interface'; | ||
declare const _default: { | ||
getCountryById: (id: string) => ICountry; | ||
getStateById: (id: string) => IState; | ||
getCityById: (id: string) => ICity; | ||
getStatesOfCountry: (countryId: string) => IState[]; | ||
getCitiesOfState: (stateId: string) => ICity[]; | ||
getAllCountries: () => ICountry[]; | ||
getStatesOfCountry(countryCode: string): IState[]; | ||
getCitiesOfState(countryCode: string, stateCode: string): ICity[]; | ||
getCitiesOfCountry(countryCode: string): ICity[]; | ||
getAllCountries(): ICountry[]; | ||
getAllStates(): IState[]; | ||
getAllCities(): ICity[]; | ||
getCountryByCode(isoCode: string): ICountry; | ||
getStateByCode(isoCode: string): IState; | ||
}; | ||
export default _default; |
@@ -9,42 +9,52 @@ "use strict"; | ||
var city_json_1 = __importDefault(require("./lib/city.json")); | ||
var _findEntryByCode = function (source, code) { | ||
if (code && source != null) { | ||
var codex = source.findIndex(function (c) { | ||
return c.isoCode === code; | ||
}); | ||
return codex !== -1 ? source[codex] : ''; | ||
} | ||
return ''; | ||
}; | ||
var compare = function (a, b) { | ||
if (a.name < b.name) | ||
return -1; | ||
if (a.name > b.name) | ||
return 1; | ||
return 0; | ||
}; | ||
exports.default = { | ||
getCountryById: function (id) { | ||
return _findEntry(country_json_1.default, id); | ||
}, | ||
getStateById: function (id) { | ||
return _findEntry(state_json_1.default, id); | ||
}, | ||
getCityById: function (id) { | ||
return _findEntry(city_json_1.default, id); | ||
}, | ||
getStatesOfCountry: function (countryId) { | ||
var states = state_json_1.default.filter(function (value, index) { | ||
return value.country_id === countryId; | ||
getStatesOfCountry: function (countryCode) { | ||
var states = state_json_1.default.filter(function (value) { | ||
return value.countryCode === countryCode; | ||
}); | ||
return states.sort(compare); | ||
}, | ||
getCitiesOfState: function (stateId) { | ||
var cities = city_json_1.default.filter(function (value, index) { | ||
return value.state_id === stateId; | ||
getCitiesOfState: function (countryCode, stateCode) { | ||
var cities = city_json_1.default.filter(function (value) { | ||
return value.countryCode === countryCode && value.stateCode === stateCode; | ||
}); | ||
return cities.sort(compare); | ||
}, | ||
getCitiesOfCountry: function (countryCode) { | ||
var cities = city_json_1.default.filter(function (value) { | ||
return value.countryCode === countryCode; | ||
}); | ||
return cities.sort(compare); | ||
}, | ||
getAllCountries: function () { | ||
return country_json_1.default; | ||
} | ||
}, | ||
getAllStates: function () { | ||
return state_json_1.default; | ||
}, | ||
getAllCities: function () { | ||
return city_json_1.default; | ||
}, | ||
getCountryByCode: function (isoCode) { | ||
return _findEntryByCode(country_json_1.default, isoCode); | ||
}, | ||
getStateByCode: function (isoCode) { | ||
return _findEntryByCode(state_json_1.default, isoCode); | ||
}, | ||
}; | ||
var _findEntry = function (source, id) { | ||
if (id && source != null) { | ||
var idx = source.findIndex(function (c) { return c.id === id; }); | ||
return (idx !== -1) ? source[idx] : ""; | ||
} | ||
else | ||
return ""; | ||
}; | ||
function compare(a, b) { | ||
if (a.name < b.name) | ||
return -1; | ||
if (a.name > b.name) | ||
return 1; | ||
return 0; | ||
} |
@@ -0,16 +1,32 @@ | ||
interface Timezones { | ||
zoneName: string; | ||
gmtOffset: number; | ||
gmtOffsetName: string; | ||
abbreviation: string; | ||
tzName: string; | ||
} | ||
export interface ICountry { | ||
id: string; | ||
name: string; | ||
phonecode: string; | ||
sortname: string; | ||
isoCode: string; | ||
flag: string; | ||
currency: string; | ||
latitude: string; | ||
longitude: string; | ||
timezones?: Timezones[]; | ||
} | ||
export interface IState { | ||
id: string; | ||
name: string; | ||
country_id: string; | ||
isoCode: string; | ||
countryCode: string; | ||
latitude?: string | null; | ||
longitude?: string | null; | ||
} | ||
export interface ICity { | ||
id: string; | ||
name: string; | ||
state_id: string; | ||
countryCode: string; | ||
stateCode: string; | ||
latitude?: string | null; | ||
longitude?: string | null; | ||
} | ||
export {}; |
{ | ||
"name": "country-state-city", | ||
"version": "2.0.0-beta-1", | ||
"version": "2.0.0", | ||
"description": "Library for fetching Country, its States and Cities", | ||
@@ -9,3 +9,4 @@ "main": "dist/index.js", | ||
"test": "jest", | ||
"build": "tsc -p tsconfig.json" | ||
"build": "tsc -p tsconfig.json", | ||
"lint": "eslint index.ts __test__ --fix" | ||
}, | ||
@@ -40,9 +41,19 @@ "author": "harpreetkhalsagtbit@gmail.com", | ||
"devDependencies": { | ||
"@types/jest": "^24.0.12", | ||
"husky": "^1.3.1", | ||
"jest": "^23.6.0", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "^3.4.5" | ||
"@types/jest": "^26.0.19", | ||
"@typescript-eslint/eslint-plugin": "^4.12.0", | ||
"@typescript-eslint/parser": "^4.12.0", | ||
"eslint": "^7.17.0", | ||
"eslint-config-airbnb": "^18.2.1", | ||
"eslint-config-prettier": "^7.1.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-jsx-a11y": "^6.4.1", | ||
"eslint-plugin-prettier": "^3.3.1", | ||
"eslint-plugin-react": "^7.22.0", | ||
"husky": "^4.3.6", | ||
"jest": "^26.6.3", | ||
"prettier": "^2.2.1", | ||
"ts-jest": "^26.4.4", | ||
"typescript": "^4.1.3" | ||
}, | ||
"dependencies": {} | ||
} |
192
README.md
@@ -5,6 +5,6 @@ country-state-city | ||
Data taken from: | ||
Data Source:<br> | ||
https://github.com/dr5hn/countries-states-cities-database<br> | ||
For any data related issue, you can raise a Issue [here](https://github.com/dr5hn/countries-states-cities-database/issues/new). | ||
https://github.com/hiiamrohit/Countries-States-Cities-database | ||
# Install | ||
@@ -15,10 +15,16 @@ `npm i country-state-city` | ||
## Latest Release : `v1.0.0` (First Major Version Release - Not backword compatible) | ||
## Latest Release : `v2.0.0` (First Major Version Release - Not backward compatible) | ||
- ES6 Module usage | ||
`import csc from 'country-state-city'` | ||
```js | ||
import csc from 'country-state-city' | ||
// Import Interfaces` | ||
import { ICountry, IState, ICity } from 'country-state-city' | ||
``` | ||
- AMD Module usage | ||
`let csc = require('country-state-city').default` | ||
```js | ||
let csc = require('country-state-city').default | ||
``` | ||
@@ -30,15 +36,18 @@ | ||
`import csc from 'country-state-city'` | ||
```js | ||
import csc from 'country-state-city' | ||
``` | ||
- AMD Module usage | ||
`let csc = require('country-state-city')` | ||
```js | ||
let csc = require('country-state-city') | ||
``` | ||
# Docs | ||
getCountryById(id) | ||
getCountryByCode(code) | ||
--------------- | ||
It accepts a valid `CountryId` and returns *Country Details* | ||
It accepts a valid `CountryCode` eg: `'AS'` and returns *Country Details* | ||
@@ -49,88 +58,143 @@ type: **json | ICountry** | ||
{ | ||
"id": "4", | ||
"sortname": "AS", | ||
"name": "American Samoa", | ||
"phonecode": "1684" | ||
"isoCode": "IN", | ||
"name": "India", | ||
"phonecode": "91", | ||
"flag": "🇮🇳", | ||
"currency": "INR", | ||
"latitude": "20.00000000", | ||
"longitude": "77.00000000", | ||
"timezones": [ | ||
{ | ||
"zoneName": "Asia\/Kolkata", | ||
"gmtOffset": 19800, | ||
"gmtOffsetName": "UTC+05:30", | ||
"abbreviation": "IST", | ||
"tzName": "Indian Standard Time" | ||
} | ||
] | ||
} | ||
``` | ||
getStateById(id) | ||
getStatesOfCountry(countryCode) | ||
--------------- | ||
It accepts a valid `StateId` and returns *State Details* | ||
It accepts a valid `CountryCode` and returns *all States* as Array of JSON | ||
type: **json | IState** | ||
type: **array of json | IState** | ||
```js | ||
{ | ||
"id": 4119, | ||
"name": "Midlands", | ||
"country_id": "246" | ||
} | ||
[ | ||
{ | ||
"name": "Delhi", | ||
"isoCode": "DL", | ||
"countryCode": "IN", | ||
"latitude": "28.70405920", | ||
"longitude": "77.10249020" | ||
} | ||
] | ||
``` | ||
getCityById(id) | ||
getCitiesOfState(countryCode, stateCode) | ||
--------------- | ||
It accepts a valid `CityId` and returns *City Details* | ||
It accepts a valid `CountryCode`, `StateCode` and returns *all Cities* as Array of JSON | ||
type: **json | ICity** | ||
type: **array of json | ICity** | ||
```js | ||
{ | ||
"id": "3", | ||
"name": "Port Blair", | ||
"state_id": "1" | ||
} | ||
[ | ||
{ | ||
"name": "New Delhi", | ||
"countryCode": "IN", | ||
"stateCode": "DL", | ||
"latitude": "28.63576000", | ||
"longitude": "77.22445000" | ||
} | ||
] | ||
``` | ||
getStatesOfCountry(countryId) | ||
getCitiesOfCountry(countryCode) | ||
--------------- | ||
It accepts a valid `CountryId` and returns *all States* as Array of JSON | ||
It accepts a valid `CountryCode` and returns *all Cities* as Array of JSON | ||
type: **array of json | IState** | ||
type: **array of json | ICity** | ||
```js | ||
[ | ||
{ | ||
"id": 4119, | ||
"name": "Midlands", | ||
"country_id": "246" | ||
} | ||
{ | ||
"name": "New Delhi", | ||
"countryCode": "IN", | ||
"stateCode": "DL", | ||
"latitude": "28.63576000", | ||
"longitude": "77.22445000" | ||
} | ||
] | ||
``` | ||
getCitiesOfState(stateId) | ||
getAllCountries | ||
--------------- | ||
It returns **all Countries** | ||
It accepts a valid `CityId` and returns *all Cities* as Array of JSON | ||
type: **array of json | ICountry** | ||
type: **array of json | ICity** | ||
```js | ||
[ | ||
{ | ||
"isoCode": "IN", | ||
"name": "India", | ||
"phonecode": "91", | ||
"flag": "🇮🇳", | ||
"currency": "INR", | ||
"latitude": "20.00000000", | ||
"longitude": "77.00000000", | ||
"timezones": [ | ||
{ | ||
"zoneName": "Asia\/Kolkata", | ||
"gmtOffset": 19800, | ||
"gmtOffsetName": "UTC+05:30", | ||
"abbreviation": "IST", | ||
"tzName": "Indian Standard Time" | ||
} | ||
] | ||
} | ||
] | ||
``` | ||
getAllStates | ||
--------------- | ||
It returns **all States** | ||
type: **array of json | IState** | ||
```js | ||
[ | ||
{ | ||
"id": "3", | ||
"name": "Port Blair", | ||
"state_id": "1" | ||
} | ||
{ | ||
"name": "Delhi", | ||
"isoCode": "DL", | ||
"countryCode": "IN", | ||
"latitude": "28.70405920", | ||
"longitude": "77.10249020" | ||
} | ||
] | ||
``` | ||
getAllCountries | ||
getAllCities | ||
--------------- | ||
It returns **all Countries** | ||
It returns **all Cities** | ||
type: **array of json | ICountry** | ||
type: **array of json | ICity** | ||
```js | ||
[ | ||
{ | ||
"id": "4", | ||
"sortname": "AS", | ||
"name": "American Samoa", | ||
"phonecode": "1684" | ||
} | ||
{ | ||
"name": "Delhi", | ||
"isoCode": "DL", | ||
"countryCode": "IN", | ||
"latitude": "28.70405920", | ||
"longitude": "77.10249020" | ||
} | ||
] | ||
@@ -143,8 +207,16 @@ ``` | ||
[@baywet](https://github.com/baywet) - For mentoring Javascript to Typescript Conversion | ||
[@dr5hn](https://github.com/dr5hn) - For updated World Data Dictionary | ||
Change Logs | ||
--------------- | ||
**v2.0.0 (Backward Incompatible)** :: [Data Source (dr5hn)](https://github.com/dr5hn/countries-states-cities-database) | ||
**v1.0.0** | ||
1. Not backward compatible with previous versions. | ||
2. New and updated API functions. | ||
3. Removed proprietor Indexed Id's for uniquely identifying Country, State, City. Instead now using standard isoCodes. | ||
4. Data taken from more robust and accurate database. | ||
5. Countries with their respective flags, isoCode, currencies, latitude, longitude & timezones. | ||
6. States & cities with their latitude & longitude. | ||
**v1.0.0** :: [Data Source (hiiamrohit)](https://github.com/hiiamrohit/Countries-States-Cities-database) | ||
1. `export = {}` changed to `export default` in index.ts. | ||
@@ -158,3 +230,3 @@ 2. `Interface` type `re-exported` from `index.ts`. | ||
**v0.1.8** | ||
**v0.1.8** :: [Data Source (hiiamrohit)](https://github.com/hiiamrohit/Countries-States-Cities-database) | ||
1. Development code - Javascript to Typescript conversion: [#12](https://github.com/harpreetkhalsagtbit/country-state-city/pull/12) | ||
@@ -161,0 +233,0 @@ |
@@ -0,17 +1,33 @@ | ||
interface Timezones { | ||
zoneName: string; | ||
gmtOffset: number; | ||
gmtOffsetName: string; | ||
abbreviation: string; | ||
tzName: string; | ||
} | ||
export interface ICountry { | ||
id: string; | ||
name: string; | ||
phonecode: string; | ||
sortname: string; | ||
isoCode: string; | ||
flag: string; | ||
currency: string; | ||
latitude: string; | ||
longitude: string; | ||
timezones?: Timezones[]; | ||
} | ||
export interface IState { | ||
id: string; | ||
name: string; | ||
country_id: string; | ||
isoCode: string; | ||
countryCode: string; | ||
latitude?: string | null; | ||
longitude?: string | null; | ||
} | ||
export interface ICity { | ||
id: string; | ||
name: string; | ||
state_id: string; | ||
name: string; | ||
countryCode: string; | ||
stateCode: string; | ||
latitude?: string | null; | ||
longitude?: string | null; | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
51929593
19
0
232
15
81803
1