Skip to content

Commit

Permalink
Merge pull request #65 from harpreetkhalsagtbit/release-v3.0.0
Browse files Browse the repository at this point in the history
Release v3.0.0

Refactoring of complete package to make this package - Tree shakable
  • Loading branch information
harpreetkhalsagtbit authored Jun 19, 2021
2 parents c966378 + fb8c8cb commit a6ed495
Show file tree
Hide file tree
Showing 19 changed files with 1,153 additions and 274 deletions.
59 changes: 59 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Change Logs
---------------
**v3.0.0** ::
1. Refactoring of complete package to make this package - Tree shakable [Fix:60](https://github.com/harpreetkhalsagtbit/country-state-city/issues/60).
2. Change in usage:

- ES6 Module usage

```js
// Latest version - v3.0.0
import { Country, State, City } from 'country-state-city';
```

- AMD Module usage

```js
// Latest version - v3.0.0
let Country = require('country-state-city').Country;
let State = require('country-state-city').State;
console.log(Country.getAllCountries())
console.log(State.getAllStates())
```

**v2.2.0** ::

1. State, City Database update by [(dr5hn)](https://github.com/dr5hn/countries-states-cities-database)

**v2.1.0** ::

1. Fix [#53](https://github.com/harpreetkhalsagtbit/country-state-city/issues/53): returns wrong state as state codes can ne duplicate for different countries.
2. **Deprecate warning**: `getStateByCode`
3. New function - `getStateByCodeAndCountry`

**v2.0.0 (Backward Incompatible)** :: [Data Source (dr5hn)](https://github.com/dr5hn/countries-states-cities-database)

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.
2. `Interface` type `re-exported` from `index.ts`.
3. `Compatible` with `ES6` module syntax.
4. `Compatible` with `AMD` module - using `require('../index').default`.
5. Add tests for Interface Re-Exports.
6. Test cases for both AMD modules and ES6 modules usage.
7. Common Test Cases are being shared between AMD and ES6 modules test files.
**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)
**v0.1.0**
1. Fix: [#2](https://github.com/harpreetkhalsagtbit/country-state-city/issues/2)
2. Fix: [#3](https://github.com/harpreetkhalsagtbit/country-state-city/issues/3)
3. Added some missing states and cities for Canada and US
62 changes: 18 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,44 @@ For any data related issue, you can raise a Issue [here](https://github.com/dr5h

# Usage

## Release : `v2.0.0` (Major Version Release - Not backward compatible)
## Release : `v3.0.0` (Major Version Release - Not backward compatible)
- ES6 Module usage

```js
import csc from 'country-state-city'
// Latest version - v3.0.0 with Tree Shaking to reduce bundle size
import { Country, State, City } from 'country-state-city';
console.log(Country.getAllCountries())
console.log(State.getAllStates())

// Import Interfaces`
import { ICountry, IState, ICity } from 'country-state-city'
import { ICountry, IState, ICity } from 'country-state-city'

```
- AMD Module usage

```js
let csc = require('country-state-city').default
// Latest version - v3.0.0
let Country = require('country-state-city').Country;
let State = require('country-state-city').State;
console.log(Country.getAllCountries())
console.log(State.getAllStates())
```


## For versions `v0.1.8 and below`
## For versions `below v2.2.0 and above v0.1.8`

- ES6 Module usage

```js
import csc from 'country-state-city'
import csc from 'country-state-city';
```

- AMD Module usage

```js
let csc = require('country-state-city')
let csc = require('country-state-city').default;
```

# Docs
Expand Down Expand Up @@ -220,40 +230,4 @@ Special Thanks

[@dr5hn](https://github.com/dr5hn) - For updated World Data Dictionary

Change Logs
---------------
**v2.2.0** ::

1. State, City Database update by [(dr5hn)](https://github.com/dr5hn/countries-states-cities-database)

**v2.1.0** ::

1. Fix [#53](https://github.com/harpreetkhalsagtbit/country-state-city/issues/53): returns wrong state as state codes can ne duplicate for different countries.
2. **Deprecate warning**: `getStateByCode`
3. New function - `getStateByCodeAndCountry`

**v2.0.0 (Backward Incompatible)** :: [Data Source (dr5hn)](https://github.com/dr5hn/countries-states-cities-database)

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.
2. `Interface` type `re-exported` from `index.ts`.
3. `Compatible` with `ES6` module syntax.
4. `Compatible` with `AMD` module - using `require('../index').default`.
5. Add tests for Interface Re-Exports.
6. Test cases for both AMD modules and ES6 modules usage.
7. Common Test Cases are being shared between AMD and ES6 modules test files.
**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)
**v0.1.0**
1. Fix: [#2](https://github.com/harpreetkhalsagtbit/country-state-city/issues/2)
2. Fix: [#3](https://github.com/harpreetkhalsagtbit/country-state-city/issues/3)
3. Added some missing states and cities for Canada and US
[taylorthurlow](https://github.com/taylorthurlow) - For pointing into right direction - Module Tree Shaking
5 changes: 0 additions & 5 deletions __test__/amdModule.test.js

This file was deleted.

5 changes: 5 additions & 0 deletions __test__/amdModule.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import executeAllTests from './index.test';

const { Country, State, City } = require('../index');

executeAllTests(Country, State, City);
26 changes: 14 additions & 12 deletions __test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import csc, { ICountry, ICity, IState } from '../index';
/* eslint-disable no-shadow */
import { Country, State, City } from '../index';
import { ICountry, ICity, IState } from '../lib/interface';

const executeAllTests = function (csc: any) {
const executeAllTests = function (Country: any, State: any, City: any) {
test('Check for Country By Code ', () => {
const code = 'CA';
const country = csc.getCountryByCode(code);
const country: ICountry = Country.getCountryByCode(code);
expect(country).toEqual({
name: 'Canada',
phonecode: '1',
Expand Down Expand Up @@ -215,8 +217,8 @@ const executeAllTests = function (csc: any) {

test('Check for Canada States', () => {
const code = 'CA';
const country = csc.getCountryByCode(code);
const states = csc.getStatesOfCountry(country.isoCode);
const country: any = Country.getCountryByCode(code);
const states = State.getStatesOfCountry(country.isoCode);
const names = states.map((state: IState) => {
return state.name;
});
Expand All @@ -239,8 +241,8 @@ const executeAllTests = function (csc: any) {

test('Check All States for United States Of America', () => {
const code = 'US';
const country = csc.getCountryByCode(code);
const states = csc.getStatesOfCountry(country.isoCode);
const country: any = Country.getCountryByCode(code);
const states = State.getStatesOfCountry(country.isoCode);
const names = states.map((state: IState) => {
return state.name;
});
Expand Down Expand Up @@ -316,8 +318,8 @@ const executeAllTests = function (csc: any) {

test('Check States for India', () => {
const code = 'IN';
const country = csc.getCountryByCode(code);
const states = csc.getStatesOfCountry(country.isoCode);
const country: any = Country.getCountryByCode(code);
const states = State.getStatesOfCountry(country.isoCode);
const names = states.map((state: IState) => {
return state.name;
});
Expand Down Expand Up @@ -365,7 +367,7 @@ const executeAllTests = function (csc: any) {
test('Check Cities for Delhi', () => {
const countryCode = 'IN';
const stateCode = 'DL';
const cities = csc.getCitiesOfState(countryCode, stateCode);
const cities: any = City.getCitiesOfState(countryCode, stateCode);
const names = cities.map((city: ICity) => {
return city.name;
});
Expand Down Expand Up @@ -395,9 +397,9 @@ const executeAllTests = function (csc: any) {
test('Get State by State ISOCode and Country Code', () => {
const countryCode = 'PK';
const stateCode = 'KP';
const state = csc.getStateByCodeAndCountry(stateCode, countryCode);
const state: any = State.getStateByCodeAndCountry(stateCode, countryCode);
expect(state.name).toEqual('Khyber Pakhtunkhwa');
});
};
export default executeAllTests;
executeAllTests(csc);
executeAllTests(Country, State, City);
122 changes: 75 additions & 47 deletions __test__/interface-export.test.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,97 @@
// importing interfaces from main index.ts file
import { ICountry, ICity, IState } from '../index';
import { ICountry } from '../lib/interface';

// writing tests for Interfaces
// https://stackoverflow.com/questions/14425568/interface-type-check-with-typescript

function isValidCountryObjectStructure(object: any): object is ICountry {
return 'name' in object && 'phonecode' in object && 'isoCode' in object && 'flag' in object;
return 'name' in object && 'phonecode' in object && 'isoCode' in object && 'flag' in object;
}

test('Check for Interface export when Type Structure is Same', () => {
const country: ICountry = {
name: 'India',
phonecode: '+91',
isoCode: 'IN',
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"}]
};
let isCountry = isValidCountryObjectStructure(country)
expect(isCountry).toEqual(true);
const country = {
name: 'India',
phonecode: '+91',
isoCode: 'IN',
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',
},
]
};
const isCountry = isValidCountryObjectStructure(country);
expect(isCountry).toEqual(true);
});

test('Check for Interface export when Type Structure is Not Same', () => {
const country = {
phonecode: '+91', // missing name field
isoCode: 'IN',
flag: '🇮🇳'
};
let isCountry = isValidCountryObjectStructure(country)
expect(isCountry).toEqual(false);
const country = {
phonecode: '+91', // missing name field
isoCode: 'IN',
flag: '🇮🇳',
};
const isCountry = isValidCountryObjectStructure(country);
expect(isCountry).toEqual(false);
});

function isValidCountryObjectAndValueType(object: any): object is ICountry {
return typeof typeof object.name == "string" && typeof object.phonecode == "string" && typeof object.isoCode == "string" && typeof object.flag == "string";
return (
typeof typeof object.name === 'string' &&
typeof object.phonecode === 'string' &&
typeof object.isoCode === 'string' &&
typeof object.flag === 'string'
);
}

test('Check for Interface export when Type Structure is Same and Value is of same type as well', () => {
const country: ICountry = {
name: 'India',
phonecode: '+91',
isoCode: 'IN',
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"}]
};
let isCountry = isValidCountryObjectAndValueType(country)
expect(isCountry).toEqual(true);
const country: ICountry = {
name: 'India',
phonecode: '+91',
isoCode: 'IN',
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',
},
],
};
const isCountry = isValidCountryObjectAndValueType(country);
expect(isCountry).toEqual(true);
});

test('Check for Interface export when Type Structure is Same and Value is of same type as well', () => {
const country = {
name: 'India',
phonecode: 91, // wrong type
isoCode: 'IN',
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"}]
};
let isCountry = isValidCountryObjectAndValueType(country)
expect(isCountry).toEqual(false);
const country = {
name: 'India',
phonecode: 91, // wrong type
isoCode: 'IN',
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',
},
],
};
const isCountry = isValidCountryObjectAndValueType(country);
expect(isCountry).toEqual(false);
});

File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit a6ed495

Please sign in to comment.