From 93425132dbfe77c47a889fe15f5fdf5d11815ecb Mon Sep 17 00:00:00 2001 From: Aadarsha Date: Mon, 8 Feb 2021 17:07:38 +0545 Subject: [PATCH 01/10] refactor: remove all emojis --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d29ed9d..f24d75b 100644 --- a/README.md +++ b/README.md @@ -86,30 +86,30 @@ See the how to implement the package on your Javascript applications on followin - [Municipalities](./docs/usage/municipalities.md) - [Categories](./docs/usage/categories.md) -## 🕐 Release Notes +## Release Notes Read [Change Log](CHANGELOG.md) for complete logs. -## 🤝 Contributing +## Contributing I would love to have some of your contributions to this project. You can checkout [Contributing Guide](CONTRIBUTING.md) for Contribution guidelines. -## 💙 Contributors +## Contributors This package is inspired from the Php composer package [local-states-nepal](https://github.com/sagautam5/local-states-nepal). Massive thanks to [Sagar Gautam](https://github.com/sagautam5) for providing the dataset. -## 👏🏻 Show your support +## Show your support Give a ⭐️ if you like the project! :tada: -## 👤 Author +## Author - Website: -- Twitter: [@adarshatweets](https://twitter.com/aadarshatweets) +- Twitter: [@aadarshatweets](https://twitter.com/aadarshatweets) - Github: [@adarshaacharya](https://github.com/adarshaacharya) - LinkedIn: [@adarshaacharya](https://linkedin.com/in/adarshaacharya) -## 📝 License +## License Copyright © 2020 [Aadarsha Acharya](http://adarshaacharya.com.np/).
This project is [MIT](https://github.com/adarshaacharya/states-nepal/blob/master/LICENSE) licensed. From 36878cbd0800a991992101efaa4545d8beb99743 Mon Sep 17 00:00:00 2001 From: Aadarsha Date: Tue, 9 Feb 2021 16:51:18 +0545 Subject: [PATCH 02/10] feat: add getdistricts with municipalities --- src/entities/district.ts | 38 ++++++++++++++++++++++++++++++--- src/entities/province.ts | 3 +-- tests/entities/district.spec.ts | 16 +++++++++----- tests/entities/province.spec.ts | 1 - 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/entities/district.ts b/src/entities/district.ts index 9e695d5..321987a 100644 --- a/src/entities/district.ts +++ b/src/entities/district.ts @@ -1,5 +1,6 @@ import { fetcher } from '../fetchers' import { numericEnglish } from '../utils' +import { IMunicipality, Municipality } from './municipality' type Language = 'en' | 'np' @@ -10,8 +11,17 @@ export interface IDistrict { area_sq_km: string website: string headquarter: string + municipalities?: IMunicipality[] } +export type Key = + | 'id' + | 'province_id' + | 'name' + | 'area_sq_km' + | 'website' + | 'headquarter' + /** * Class District */ @@ -20,14 +30,14 @@ export class District { private lang /** - * Category constructor. + * District constructor. * @param string lang * @throws exception */ constructor(lang: Language = 'en') { try { this.lang = lang - this.districts = fetcher('districts',this.lang) + this.districts = fetcher('districts', this.lang) } catch (err) { throw new Error(`Districts of given language doesn't exists. `) } @@ -93,6 +103,28 @@ export class District { return this.districts[numericArea.indexOf(Math.min(...numericArea))] } + /** + * Get district with municipalities + * + * @return array of districts with municipalities + */ + public getDistrictsWithMunicipalities() { + const municipality = new Municipality(this.lang) + const districts = this.allDistricts() + + const districtsWithMunicipalities = districts.map(districtItem => ({ + ...districtItem, + municipalities: municipality + .getMunicipalitiesByDistrict(districtItem.id) + ?.map(municipalityItem => ({ + ...municipalityItem, + wards: municipality.wards(municipalityItem.id), + })), + })) + + return districtsWithMunicipalities + } + /** * Search Districts * @@ -100,7 +132,7 @@ export class District { * @param value * @return array of districts that match with given key */ - public search(key: keyof IDistrict, value: string | number) { + public search(key: Key, value: string | number) { return this.districts.filter(el => (el[key] ? el[key] === value : null)) } } diff --git a/src/entities/province.ts b/src/entities/province.ts index ebd9f7b..f13442b 100644 --- a/src/entities/province.ts +++ b/src/entities/province.ts @@ -95,11 +95,10 @@ export class Province { const district = new District(APP_LANG) const provinces = this.provinces - const result = provinces.map(item => ({ + return provinces.map(item => ({ ...item, districts: district.getDistrictsByProvince(item.id), })) - return result } /** diff --git a/tests/entities/district.spec.ts b/tests/entities/district.spec.ts index 5900886..ce47f1a 100644 --- a/tests/entities/district.spec.ts +++ b/tests/entities/district.spec.ts @@ -1,4 +1,4 @@ -import { District, IDistrict } from '../../src/entities/district' +import { District, Key } from '../../src/entities/district' import { range } from '../../src/utils' const APP_LANG = 'np' @@ -33,6 +33,15 @@ describe('Test District entities', () => { expect(smallest).toMatchObject({ id: 23, province_id: 3 }) }) + it('should districts with municipalities', () => { + const districtsWithMunicipalities = _district.getDistrictsWithMunicipalities() + + districtsWithMunicipalities.map(item => { + expect(item.municipalities).toBeDefined() + expect(item.municipalities?.length).toBeGreaterThanOrEqual(1) + }) + }) + it('should search districts that match with given key', () => { const keywords = [ 'id', @@ -46,10 +55,7 @@ describe('Test District entities', () => { for (const key of keywords) { for (const value of districts) { - const items = _district.search( - key as keyof IDistrict, - value[key as keyof IDistrict] - ) + const items = _district.search(key as Key, value[key as Key]) expect(items.length).toBeGreaterThanOrEqual(1) } } diff --git a/tests/entities/province.spec.ts b/tests/entities/province.spec.ts index 50d2dae..62873ea 100644 --- a/tests/entities/province.spec.ts +++ b/tests/entities/province.spec.ts @@ -36,7 +36,6 @@ describe('Test province entities', () => { it('should test if districts are correctly loaded with provinces', () => { const provincesWithDistricts = _province.getProvincesWithDistricts() - // console.log(provincesWithDistricts.length) provincesWithDistricts.map(item => { expect(item.districts.length).toBeGreaterThanOrEqual(1) }) From eacd6943c70d8354e8c13040da93ec7ab7e4a1a8 Mon Sep 17 00:00:00 2001 From: Aadarsha Date: Tue, 9 Feb 2021 17:13:20 +0545 Subject: [PATCH 03/10] docs: update district docs --- docs/usage/districts.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/usage/districts.md b/docs/usage/districts.md index 278ff07..9ff2d0e 100644 --- a/docs/usage/districts.md +++ b/docs/usage/districts.md @@ -85,7 +85,17 @@ const district = new District() district.smallest() ``` -**6. Search districts by key and value with exact match option** +**6. Get array of districts with municipalities (having wards no.)** + +```js +import { District } from 'states-nepal' + +const district = new District() + +district.getDistrictsWithMunicipalities() +``` + +**7. Search districts by key and value with exact match option** ```js import { District } from 'states-nepal' From 658eae0a68c4374d249e418037c2a05f2aeef39e Mon Sep 17 00:00:00 2001 From: Aadarsha Date: Tue, 9 Feb 2021 17:44:39 +0545 Subject: [PATCH 04/10] chore: update changelog --- CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 613f117..7c92103 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Release Notes -## 1.0.0 +## 0.1.0 Initial Release + +## 0.2.0 + +This is the minor release with some fixes in docs and addition of new functions. + +- Add `getDistrictsWithMunicipalities()` to district entity. \ No newline at end of file From 088cc6152b41d6d84b5fb9bb5c552a263dbcbba8 Mon Sep 17 00:00:00 2001 From: Aadarsha Date: Tue, 9 Feb 2021 19:54:50 +0545 Subject: [PATCH 05/10] feat: add getprovinces with districts and municipalities --- CHANGELOG.md | 2 +- src/entities/province.ts | 32 +++++++++++++++++++++++++++----- tests/entities/district.spec.ts | 2 +- tests/entities/province.spec.ts | 8 ++++++++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c92103..d29295a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,4 +8,4 @@ Initial Release This is the minor release with some fixes in docs and addition of new functions. -- Add `getDistrictsWithMunicipalities()` to district entity. \ No newline at end of file +- Add `getDistrictsWithMunicipalities()` to district entity. diff --git a/src/entities/province.ts b/src/entities/province.ts index f13442b..67e86d7 100644 --- a/src/entities/province.ts +++ b/src/entities/province.ts @@ -1,8 +1,8 @@ import { fetcher } from '../fetchers' import { numericEnglish } from '../utils' import { District, IDistrict } from './district' +import { Municipality } from './municipality' type Language = 'en' | 'np' -const APP_LANG = 'np' export type Key = 'id' | 'name' | 'area_sq_km' | 'website' | 'headquarter' export interface IProvince { @@ -92,12 +92,34 @@ export class Province { */ public getProvincesWithDistricts() { - const district = new District(APP_LANG) + const district = new District(this.lang) const provinces = this.provinces - return provinces.map(item => ({ - ...item, - districts: district.getDistrictsByProvince(item.id), + return provinces.map(provinceItem => ({ + ...provinceItem, + districts: district.getDistrictsByProvince(provinceItem.id), + })) + } + + public getProvincesWithDistrictsWithMunicipalities() { + const district = new District(this.lang) + const municipality = new Municipality(this.lang) + + const provinces = this.provinces + + return provinces.map(provinceItem => ({ + ...provinceItem, + districts: district + .getDistrictsByProvince(provinceItem.id) + .map(districtItem => ({ + ...districtItem, + municipalities: municipality + .getMunicipalitiesByDistrict(districtItem.id) + ?.map(municipalityItem => ({ + ...municipalityItem, + wards: municipality.wards(municipalityItem.id), + })), + })), })) } diff --git a/tests/entities/district.spec.ts b/tests/entities/district.spec.ts index ce47f1a..614d239 100644 --- a/tests/entities/district.spec.ts +++ b/tests/entities/district.spec.ts @@ -33,7 +33,7 @@ describe('Test District entities', () => { expect(smallest).toMatchObject({ id: 23, province_id: 3 }) }) - it('should districts with municipalities', () => { + it('should get districts with municipalities', () => { const districtsWithMunicipalities = _district.getDistrictsWithMunicipalities() districtsWithMunicipalities.map(item => { diff --git a/tests/entities/province.spec.ts b/tests/entities/province.spec.ts index 62873ea..03da0ae 100644 --- a/tests/entities/province.spec.ts +++ b/tests/entities/province.spec.ts @@ -41,6 +41,14 @@ describe('Test province entities', () => { }) }) + it('should get provinces with districts with municipalities', () => { + const result = _province.getProvincesWithDistrictsWithMunicipalities() + + result.map(item => { + expect(item.districts.length).toBeGreaterThanOrEqual(1) + }) + }) + it('should search provinces that match with given key', () => { const keywords = ['id', 'name', 'area_sq_km', 'website', 'headquarter'] const allProvinces = _province.allProvinces() From 1b968655069f0c0da5576d71d684af97abdc98c5 Mon Sep 17 00:00:00 2001 From: Aadarsha Date: Tue, 9 Feb 2021 20:04:19 +0545 Subject: [PATCH 06/10] chore: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d29295a..1b45cb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,3 +9,4 @@ Initial Release This is the minor release with some fixes in docs and addition of new functions. - Add `getDistrictsWithMunicipalities()` to district entity. +- Add `getProvincesWithDistrictsWithMunicipalities()` to province entity. From 889837b3f843e29fd92c9f1d8048020af35ad0bf Mon Sep 17 00:00:00 2001 From: Aadarsha Date: Tue, 9 Feb 2021 20:12:57 +0545 Subject: [PATCH 07/10] docs: update province docs --- docs/usage/provinces.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/usage/provinces.md b/docs/usage/provinces.md index 6ceb210..3f4b837 100644 --- a/docs/usage/provinces.md +++ b/docs/usage/provinces.md @@ -85,7 +85,18 @@ const province = new Province() province.getProvincesWithDistricts() ``` -**6. Search provinces by key and value with exact match option** +**6. Get all provinces with its districts with municipalities(having wards no.)** + +```js +import { Province } from 'states-nepal' + +const province = new Province() + +province.getProvincesWithDistrictsWithMunicipalities() +``` + + +**7. Search provinces by key and value with exact match option** ```js import { Province } from 'states-nepal' From d6c17be560cace7b7735ea6f16f18358f004ee41 Mon Sep 17 00:00:00 2001 From: Aadarsha Date: Tue, 9 Feb 2021 20:14:10 +0545 Subject: [PATCH 08/10] chore: update province comment --- docs/usage/provinces.md | 1 - src/entities/province.ts | 5 +++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/usage/provinces.md b/docs/usage/provinces.md index 3f4b837..60c1d46 100644 --- a/docs/usage/provinces.md +++ b/docs/usage/provinces.md @@ -95,7 +95,6 @@ const province = new Province() province.getProvincesWithDistrictsWithMunicipalities() ``` - **7. Search provinces by key and value with exact match option** ```js diff --git a/src/entities/province.ts b/src/entities/province.ts index 67e86d7..301095b 100644 --- a/src/entities/province.ts +++ b/src/entities/province.ts @@ -101,6 +101,11 @@ export class Province { })) } + /** + * Get provinces with districts with its municipalities + * + * @return array of provinces + */ public getProvincesWithDistrictsWithMunicipalities() { const district = new District(this.lang) const municipality = new Municipality(this.lang) From c5c4e9104c6c6aa4c24cf9ec4dee1807ae7f8e85 Mon Sep 17 00:00:00 2001 From: Aadarsha Date: Tue, 9 Feb 2021 20:15:22 +0545 Subject: [PATCH 09/10] chore: update package description --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index ad78ece..d5b9bba 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "states-nepal", - "version": "0.1.0", - "description": "Get structured dataset for administrative division in Nepal.", + "version": "0.2.0", + "description": "Get structured dataset about administrative division in Nepal.", "keywords": [ "states-nepal", "nepal", From ea6a095a9eefeb739933ac1cfb90934d7d9a60c6 Mon Sep 17 00:00:00 2001 From: Aadarsha Date: Tue, 9 Feb 2021 20:32:11 +0545 Subject: [PATCH 10/10] chore: update package description --- package.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index d5b9bba..3ab00d0 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,10 @@ "keywords": [ "states-nepal", "nepal", - "nodejs", - "typescript" + "nepali", + "district-nepal", + "municipality-nepal", + "province-nepal" ], "main": "./lib/cjs/src/index.js", "module": "./lib/esm/src/index.js",