Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules
*.tgz
/package
.DS_Store
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.vscode
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.wordWrap": "on"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Tui2Tone

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
[![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)

# countries-with-cities-select
Retrieve all countries with cities

## Table of Contents
+ [Get countries](#get-countries)
+ [Get countries with details](#get-countries-with-details)
+ [Get cities](#get-cities)

## Features
+ Get names, codes and cities of all countries
+ Filter cities by country name, country code, continent name

## Installing

```
npm install countries-with-cities-select

OR

yarn add countries-with-cities-select

```
Once the package is installed you use the require/import approach

```javascript
const countriesWithCities = require('countries-with-cities-select');

OR

import countriesWithCities from 'countries-with-cities-select'

```

Or with TypeScript
```typescript
import * as countriesWithCities from 'countries-with-cities-select'
```
create a `.d.ts` file and add
```ts
declare module 'countries-with-cities-select'
```

## #Get countries
This will list all countries.
```javascript

const countriesWithCities = require('countries-with-cities-select');
// To get all countries
countriesWithCities.getCountries();

```

## #Get countries with details
This will retrieve information about countries with code, continent etc. Filter by country name, country code or continent
```javascript

const countriesWithCities = require('countries-with-cities-select');

countriesWithCities.getCountriesWithDetails();

// To get the countries with code, name or continent name
countriesWithCities.getCountriesWithDetails("kenya");
// use exact code to filter country with code
countriesWithCities.getCountriesWithDetails("KE");
countriesWithCities.getCountriesWithDetails("africa");

```

## #Get cities
This will retrieve information about cities. Filter with country name, country code or continent
Empty filter will return no cities

```javascript
const countriesWithCities = require('countries-with-cities-select');

// To get all cities in a country
countriesWithCities.getCities('kenya');

// To get all cities in a continent
countriesWithCities.getCities('africa');

// To get all cities in a country with code
countriesWithCities.getCities('ke');

// To getcity with name
countriesWithCities.getCities('nairobi');


```

## License

[MIT](LICENSE)

[![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=for-the-badge)](#)

[![Open Source Love](https://badges.frapsoft.com/os/v2/open-source-200x33.png?v=103)](#)

Happy coding, Star before Fork 😊💪💯
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "countries-with-cities-select",
"version": "1.0.0",
"description": "All countries with cities select dropdown",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -15,7 +15,7 @@
"countires",
"dropdown",
"select",
"serach",
"search",
"cities",
"towns"
],
Expand Down
1 change: 1 addition & 0 deletions src/country-cities.json

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const countriesData = require("./country-cities.json")

function getCountries() {
return Object.keys(countriesData)
}

// filter with continent, country code, country name
function getCountriesWithDetails(filter = null) {
const countries = Object.entries(countriesData).map(([country, cities]) => {
return {
countryName: country,
continentCode: cities[0].continentCode,
continentName: cities[0].continentName,
countryIsoCode: cities[0].countryIsoCode,
}
})

if (!filter) {
return countries
}

// country code priority
const countryWithCode = countries.filter(
(c) => c.countryIsoCode === filter
)
if (countryWithCode.length) return countryWithCode

return countries.filter(
(c) =>
c.countryName.toLowerCase().includes(filter.toLowerCase()) ||
c.continentName.toLowerCase().includes(filter.toLowerCase()) ||
c.countryIsoCode.toLowerCase().includes(filter.toLowerCase())
)
}

// filter with city name, country name, country code, continent name
function getCities(filter) {
// NOTE: don't see need of getting all cities, no probable usecase
if (!filter) return []

const search = filter.toLowerCase()

const citiesArr = Object.values(countriesData)

const cities = citiesArr.flat()
// country code priority
const cityWithCode = cities.filter((city) => city.countryIsoCode === filter)
if (cityWithCode.length) return cityWithCode

return cities.filter(
(city) =>
city.city.toLowerCase().includes(search) ||
city.countryIsoCode.toLowerCase().includes(search) ||
city.countryName.toLowerCase().includes(search) ||
city.continentName.toLowerCase().includes(search)
)
}

module.exports = {
getCountries,
getCountriesWithDetails,
getCities,
}