Skip to content

Commit ad2c8bb

Browse files
authored
Merge pull request #1 from JavaScript-Bits/dev
v1.0.0
2 parents b628d3a + cd7de61 commit ad2c8bb

File tree

9 files changed

+197
-2
lines changed

9 files changed

+197
-2
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/node_modules
2+
*.tgz
3+
/package
4+
.DS_Store

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.vscode

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.wordWrap": "on"
3+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Tui2Tone
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[![npm version](https://badge.fury.io/js/countries-with-cities-select.svg)](https://github.com/JavaScript-Bits/countries-with-cities-select) [![npm version](https://badgen.net/npm/dt/countries-with-cities-select)](https://github.com/JavaScript-Bits/countries-with-cities-select) [![npm version](https://badgen.net/npm/license/lodash)](https://github.com/JavaScript-Bits/countries-with-cities-select)
2+
3+
# countries-with-cities-select
4+
Retrieve all countries with cities
5+
6+
## Table of Contents
7+
+ [Get countries](#get-countries)
8+
+ [Get countries with details](#get-countries-with-details)
9+
+ [Get cities](#get-cities)
10+
11+
## Features
12+
+ Get names, codes and cities of all countries
13+
+ Filter cities by country name, country code, continent name
14+
15+
## Installing
16+
17+
```
18+
npm install countries-with-cities-select
19+
20+
OR
21+
22+
yarn add countries-with-cities-select
23+
24+
```
25+
Once the package is installed you use the require/import approach
26+
27+
```javascript
28+
const countriesWithCities = require('countries-with-cities-select');
29+
30+
OR
31+
32+
import countriesWithCities from 'countries-with-cities-select'
33+
34+
```
35+
36+
Or with TypeScript
37+
```typescript
38+
import * as countriesWithCities from 'countries-with-cities-select'
39+
```
40+
create a `.d.ts` file and add
41+
```ts
42+
declare module 'countries-with-cities-select'
43+
```
44+
45+
## #Get countries
46+
This will list all countries.
47+
```javascript
48+
49+
const countriesWithCities = require('countries-with-cities-select');
50+
// To get all countries
51+
countriesWithCities.getCountries();
52+
53+
```
54+
55+
## #Get countries with details
56+
This will retrieve information about countries with code, continent etc. Filter by country name, country code or continent
57+
```javascript
58+
59+
const countriesWithCities = require('countries-with-cities-select');
60+
61+
countriesWithCities.getCountriesWithDetails();
62+
63+
// To get the countries with code, name or continent name
64+
countriesWithCities.getCountriesWithDetails("kenya");
65+
// use exact code to filter country with code
66+
countriesWithCities.getCountriesWithDetails("KE");
67+
countriesWithCities.getCountriesWithDetails("africa");
68+
69+
```
70+
71+
## #Get cities
72+
This will retrieve information about cities. Filter with country name, country code or continent
73+
Empty filter will return no cities
74+
75+
```javascript
76+
const countriesWithCities = require('countries-with-cities-select');
77+
78+
// To get all cities in a country
79+
countriesWithCities.getCities('kenya');
80+
81+
// To get all cities in a continent
82+
countriesWithCities.getCities('africa');
83+
84+
// To get all cities in a country with code
85+
countriesWithCities.getCities('ke');
86+
87+
// To getcity with name
88+
countriesWithCities.getCities('nairobi');
89+
90+
91+
```
92+
93+
## License
94+
95+
[MIT](LICENSE)
96+
97+
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=for-the-badge)](#)
98+
99+
[![Open Source Love](https://badges.frapsoft.com/os/v2/open-source-200x33.png?v=103)](#)
100+
101+
Happy coding, Star before Fork 😊💪💯

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "countries-with-cities-select",
33
"version": "1.0.0",
44
"description": "All countries with cities select dropdown",
5-
"main": "index.js",
5+
"main": "src/index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
@@ -15,7 +15,7 @@
1515
"countires",
1616
"dropdown",
1717
"select",
18-
"serach",
18+
"search",
1919
"cities",
2020
"towns"
2121
],

src/country-cities.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

src/index.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const countriesData = require("./country-cities.json")
2+
3+
function getCountries() {
4+
return Object.keys(countriesData)
5+
}
6+
7+
// filter with continent, country code, country name
8+
function getCountriesWithDetails(filter = null) {
9+
const countries = Object.entries(countriesData).map(([country, cities]) => {
10+
return {
11+
countryName: country,
12+
continentCode: cities[0].continentCode,
13+
continentName: cities[0].continentName,
14+
countryIsoCode: cities[0].countryIsoCode,
15+
}
16+
})
17+
18+
if (!filter) {
19+
return countries
20+
}
21+
22+
// country code priority
23+
const countryWithCode = countries.filter(
24+
(c) => c.countryIsoCode === filter
25+
)
26+
if (countryWithCode.length) return countryWithCode
27+
28+
return countries.filter(
29+
(c) =>
30+
c.countryName.toLowerCase().includes(filter.toLowerCase()) ||
31+
c.continentName.toLowerCase().includes(filter.toLowerCase()) ||
32+
c.countryIsoCode.toLowerCase().includes(filter.toLowerCase())
33+
)
34+
}
35+
36+
// filter with city name, country name, country code, continent name
37+
function getCities(filter) {
38+
// NOTE: don't see need of getting all cities, no probable usecase
39+
if (!filter) return []
40+
41+
const search = filter.toLowerCase()
42+
43+
const citiesArr = Object.values(countriesData)
44+
45+
const cities = citiesArr.flat()
46+
// country code priority
47+
const cityWithCode = cities.filter((city) => city.countryIsoCode === filter)
48+
if (cityWithCode.length) return cityWithCode
49+
50+
return cities.filter(
51+
(city) =>
52+
city.city.toLowerCase().includes(search) ||
53+
city.countryIsoCode.toLowerCase().includes(search) ||
54+
city.countryName.toLowerCase().includes(search) ||
55+
city.continentName.toLowerCase().includes(search)
56+
)
57+
}
58+
59+
module.exports = {
60+
getCountries,
61+
getCountriesWithDetails,
62+
getCities,
63+
}

0 commit comments

Comments
 (0)