Skip to content

Commit

Permalink
Add: field arguments to state and country
Browse files Browse the repository at this point in the history
  • Loading branch information
anmol-fzr committed Mar 30, 2024
1 parent 5e21d9a commit 5866a84
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 18 deletions.
56 changes: 48 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,15 @@ type: **json | ICountry**
}
```

State.getStatesOfCountry(countryCode)
State.getStatesOfCountry(countryCode, fields)
---------------

It accepts a valid `CountryCode` and returns *all States* as Array of JSON
It accepts a valid
`CountryCode` and returns *all States* as Array of JSON, optionally accepts `fields` and returns *all States but with only specified fields* as Array of JSON

type: **array of json | IState**
type: **array of json | IState[]**

State.getStatesOfCountry("IN")

```js
[
Expand All @@ -176,12 +179,30 @@ type: **array of json | IState**
]
```

type: **array of json | Partial\<IState>[]**

State.getStatesOfCountry("IN",["name","isoCode"])

```js
[
{
"name": "Delhi",
"isoCode": "DL",
}
]
```




City.getCitiesOfState(countryCode, stateCode)
---------------

It accepts a valid `CountryCode`, `StateCode` and returns *all Cities* as Array of JSON

type: **array of json | ICity**
type: **array of json | ICity[]**

```js
[
Expand All @@ -201,7 +222,7 @@ City.getCitiesOfCountry(countryCode)

It accepts a valid `CountryCode` and returns *all Cities* as Array of JSON

type: **array of json | ICity**
type: **array of json | ICity[]**

```js
[
Expand All @@ -216,11 +237,13 @@ type: **array of json | ICity**
```

Country.getAllCountries
Country.getAllCountries()
---------------
It returns **all Countries**
It returns **all Countries** abd optionally accepts fields argument which is (keyof ICountry)[] type
returns *all Countries but with only specified fields*

type: **array of json | ICountry**

type: **array of json | ICountry[]**

```js
[
Expand All @@ -245,6 +268,23 @@ type: **array of json | ICountry**
]
```

Country.getAllCountries(["isoCode","name","phonecode"])
---------------
type: **array of json | ICountry**

```js
[
{
"isoCode": "IN",
"name": "India",
"phonecode": "91",
}
]
```




State.getAllStates
---------------
It returns **all States**
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/__test__/country.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ const executeAllTests = (Country: any) => {
const country: ICountry = Country.getCountryByCode(code);
expect(country).toEqual(code);
});

test('Check for Specific Fields For Country', () => {
const country: ICountry = Country.getAllCountries(["isoCode"])[0]
expect(country).toEqual({ isoCode: "AF" });
});

});
};

Expand Down
9 changes: 9 additions & 0 deletions src/__test__/state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,16 @@ const executeAllTests = (State: any) => {
'Zoundwéogo Province',
]);
});

test('Check for Specific Fields For Country', () => {
const state: IState= State.getStatesOfCountry("IN",["isoCode"])[0]
expect(state).toEqual({ isoCode: "AN" });
});


});


};

export default executeAllTests;
Expand Down
14 changes: 12 additions & 2 deletions src/country.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import countryList from './assets/country.json';
import { compare, findEntryByCode } from './utils';
import { ICountry } from './interface';
import { ICountry, CountryFields } from './interface';

// Get a country by isoCode.
function getCountryByCode(isoCode: string): ICountry | undefined {
Expand All @@ -10,7 +10,17 @@ function getCountryByCode(isoCode: string): ICountry | undefined {
}

// Get a list of all countries.
function getAllCountries(): ICountry[] {
function getAllCountries(fields?: CountryFields[]): ICountry[] | Partial<ICountry>[] {
if (fields && fields.length > 0) {
return countryList.filter(country => {
return fields.every(field => country.hasOwnProperty(field));
}).map(country =>
Object.fromEntries(
Object.entries(country)
.filter(([key]) => fields.includes(key as keyof ICountry))
)
);
}
return countryList;
}

Expand Down
3 changes: 3 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface ICountry {
getAllCountries?(): ICountry[];
getCountryByCode?(): ICountry;
}
export type CountryFields = keyof ICountry;

export interface IState {
name: string;
Expand All @@ -28,6 +29,8 @@ export interface IState {
getStateByCodeAndCountry?(): IState;
getStateByCode?(): IState;
}
export type StateFields = keyof IState;

export interface ICity {
name: string;
countryCode: string;
Expand Down
22 changes: 16 additions & 6 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import stateList from './assets/state.json';
import { findEntryByCode, findStateByCodeAndCountryCode, compare } from './utils';
import { IState } from './interface';
import { IState,StateFields } from './interface';

// Get a list of all states.
export function getAllStates(): IState[] {
return stateList;
}

// Get a list of states belonging to a specific country.
export function getStatesOfCountry(countryCode: string = ''): IState[] {
export function getStatesOfCountry(countryCode: string = '', fields?:StateFields[]): IState[] | Partial<IState>[] {
if (!countryCode) return [];
// get data from file or cache
const states = stateList.filter((value) => {
return value.countryCode === countryCode;
});
const states = stateList.filter((value) => value.countryCode === countryCode);

if (fields && fields.length > 0) {
return states.filter(state => {
return fields.every(field => state.hasOwnProperty(field));
}).map(country =>
Object.fromEntries(
Object.entries(country)
.filter(([key]) => fields.includes(key as keyof IState))
)
);
}

return states.sort(compare);
}

Expand Down Expand Up @@ -50,3 +59,4 @@ export default {
getStateByCode,
sortByIsoCode,
};

0 comments on commit 5866a84

Please sign in to comment.