diff --git a/CHANGELOG.md b/CHANGELOG.md index 613f117..1b45cb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # 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. +- Add `getProvincesWithDistrictsWithMunicipalities()` to province entity. 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. 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' diff --git a/docs/usage/provinces.md b/docs/usage/provinces.md index 6ceb210..60c1d46 100644 --- a/docs/usage/provinces.md +++ b/docs/usage/provinces.md @@ -85,7 +85,17 @@ 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' diff --git a/package.json b/package.json index ad78ece..3ab00d0 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,14 @@ { "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", - "nodejs", - "typescript" + "nepali", + "district-nepal", + "municipality-nepal", + "province-nepal" ], "main": "./lib/cjs/src/index.js", "module": "./lib/esm/src/index.js", 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..301095b 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,14 +92,40 @@ export class Province { */ public getProvincesWithDistricts() { - const district = new District(APP_LANG) + const district = new District(this.lang) const provinces = this.provinces - const result = provinces.map(item => ({ - ...item, - districts: district.getDistrictsByProvince(item.id), + return provinces.map(provinceItem => ({ + ...provinceItem, + districts: district.getDistrictsByProvince(provinceItem.id), + })) + } + + /** + * 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) + + 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), + })), + })), })) - return result } /** diff --git a/tests/entities/district.spec.ts b/tests/entities/district.spec.ts index 5900886..614d239 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 get 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..03da0ae 100644 --- a/tests/entities/province.spec.ts +++ b/tests/entities/province.spec.ts @@ -36,12 +36,19 @@ 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) }) }) + 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()