Skip to content

Commit

Permalink
Merge pull request #10 from adarshaacharya/develop
Browse files Browse the repository at this point in the history
feat: minor version release
  • Loading branch information
adarshaacharya authored Feb 9, 2021
2 parents 3587d8e + ea6a095 commit 8fc9462
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 29 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <https://adarshaacharya.com.np/>
- 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/).<br />
This project is [MIT](https://github.com/adarshaacharya/states-nepal/blob/master/LICENSE) licensed.
12 changes: 11 additions & 1 deletion docs/usage/districts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
12 changes: 11 additions & 1 deletion docs/usage/provinces.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
38 changes: 35 additions & 3 deletions src/entities/district.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fetcher } from '../fetchers'
import { numericEnglish } from '../utils'
import { IMunicipality, Municipality } from './municipality'

type Language = 'en' | 'np'

Expand All @@ -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
*/
Expand All @@ -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. `)
}
Expand Down Expand Up @@ -93,14 +103,36 @@ 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
*
* @param key
* @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))
}
}
38 changes: 32 additions & 6 deletions src/entities/province.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
}

/**
Expand Down
16 changes: 11 additions & 5 deletions tests/entities/district.spec.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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',
Expand All @@ -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)
}
}
Expand Down
9 changes: 8 additions & 1 deletion tests/entities/province.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8fc9462

Please sign in to comment.