From 5866a840f6b251f284b093160e1111391112322d Mon Sep 17 00:00:00 2001 From: Anmol Date: Sat, 30 Mar 2024 21:26:06 +0530 Subject: [PATCH] Add: field arguments to state and country --- README.md | 56 ++++++++++++++++++++++++++++++------ package-lock.json | 4 +-- src/__test__/country.test.ts | 6 ++++ src/__test__/state.test.ts | 9 ++++++ src/country.ts | 14 +++++++-- src/interface.ts | 3 ++ src/state.ts | 22 ++++++++++---- 7 files changed, 96 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a1d7ac6d..ddc3cb91 100644 --- a/README.md +++ b/README.md @@ -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 [ @@ -176,12 +179,30 @@ type: **array of json | IState** ] ``` + +type: **array of json | Partial\[]** + +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 [ @@ -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 [ @@ -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 [ @@ -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** diff --git a/package-lock.json b/package-lock.json index 6d3eb3a2..62c01c1b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "country-state-city", - "version": "3.1.2", + "version": "3.2.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "country-state-city", - "version": "3.1.2", + "version": "3.2.1", "license": "GPL-3.0", "devDependencies": { "@types/jest": "^26.0.24", diff --git a/src/__test__/country.test.ts b/src/__test__/country.test.ts index b413f9a7..5dbfdc6a 100644 --- a/src/__test__/country.test.ts +++ b/src/__test__/country.test.ts @@ -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" }); + }); + }); }; diff --git a/src/__test__/state.test.ts b/src/__test__/state.test.ts index 2a0ab4b9..8357c9c3 100644 --- a/src/__test__/state.test.ts +++ b/src/__test__/state.test.ts @@ -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; diff --git a/src/country.ts b/src/country.ts index 2c1a2dc8..b6fcb33c 100644 --- a/src/country.ts +++ b/src/country.ts @@ -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 { @@ -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[] { + 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; } diff --git a/src/interface.ts b/src/interface.ts index 2681a364..d55586fd 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -17,6 +17,7 @@ export interface ICountry { getAllCountries?(): ICountry[]; getCountryByCode?(): ICountry; } +export type CountryFields = keyof ICountry; export interface IState { name: string; @@ -28,6 +29,8 @@ export interface IState { getStateByCodeAndCountry?(): IState; getStateByCode?(): IState; } +export type StateFields = keyof IState; + export interface ICity { name: string; countryCode: string; diff --git a/src/state.ts b/src/state.ts index 8a97a581..c00033b6 100644 --- a/src/state.ts +++ b/src/state.ts @@ -1,6 +1,6 @@ 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[] { @@ -8,12 +8,21 @@ export function getAllStates(): IState[] { } // 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[] { 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); } @@ -50,3 +59,4 @@ export default { getStateByCode, sortByIsoCode, }; +