├── .travis.yml ├── .gitignore ├── package.json ├── test.js ├── index.js ├── README.md ├── LICENSE └── country-capital.json /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | cache: 3 | directories: 4 | - node_modules 5 | node_js: 6 | - "8" 7 | before_script: 8 | - npm install -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Node build artifacts 2 | node_modules 3 | npm-debug.log 4 | 5 | # Local development 6 | *.env 7 | *.dev 8 | .DS_Store 9 | 10 | # Docker 11 | Dockerfile 12 | docker-compose.yml 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "capitals", 3 | "version": "1.0.1", 4 | "description": "Get countries capitals", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node ./test.js" 8 | }, 9 | "author": "Hosein2398 ", 10 | "license": "MIT", 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/hosein2398/Capitals" 14 | }, 15 | "devDependencies": { 16 | "tape": "^4.8.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var test = require('tape'); 2 | var capitals = require('./index'); 3 | test('should return string test', function (t) { 4 | t.plan(2); 5 | t.equal(typeof capitals.capitalOf('japan'), 'string'); 6 | t.equal(typeof capitals.isCapitalOf('budapest'), 'string'); 7 | }); 8 | 9 | test('should return true names', function (t) { 10 | t.plan(4); 11 | 12 | t.equal(capitals.capitalOf('germany'), 'Berlin'); 13 | t.equal(capitals.capitalOf('Peru'), 'Lima'); 14 | 15 | t.equal(capitals.isCapitalOf('Bangkok'), 'Thailand'); 16 | t.equal(capitals.isCapitalOf('jakarta'), 'Indonesia'); 17 | 18 | }); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var fs = require("fs"); 2 | var contents = fs.readFileSync("country-capital.json"); 3 | var jsonContent = JSON.parse(contents); 4 | 5 | function capitalOf(countryName) { 6 | for (key in jsonContent) { 7 | if (countryName.toLowerCase() === jsonContent[key].country.toLowerCase()) { 8 | return jsonContent[key].city; 9 | } 10 | } 11 | } 12 | 13 | 14 | function isCapitalOf(capitalName) { 15 | for (key in jsonContent) { 16 | if (jsonContent[key].city) { 17 | if (capitalName.toLowerCase() === jsonContent[key].city.toLowerCase()) { 18 | return jsonContent[key].country; 19 | } 20 | } 21 | } 22 | } 23 | 24 | module.exports = { 25 | capitalOf : capitalOf, 26 | isCapitalOf : isCapitalOf 27 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Capitals [![Build Status](https://travis-ci.org/hosein2398/Capitals.svg?branch=master)](https://travis-ci.org/hosein2398/Capitals) 2 | Get countries' capitals name and reverse. 3 | 4 | ## Installation 5 | ``` 6 | npm i --save capitals 7 | ``` 8 | 9 | ## Usage 10 | Get a capital name : 11 | ```JS 12 | var capitals = require('capitals'); 13 | 14 | var germany = capitals.capitalOf('Germany'); 15 | console.log(germany); // Berlin 16 | ``` 17 | 18 | Get a country name: 19 | ```JS 20 | var capitals = require('capitals'); 21 | 22 | var jakarta = capitals.isCapitalOf('Jakarta'); 23 | console.log(jakarta); // Indonesia 24 | ``` 25 | 26 | ## Api 27 | 28 | **capitalOf** (parameter : `string`) 29 | Description : returns name a city. 30 | Returns : `string` 31 | 32 | **isCapitalOf** (parameter : `string`) 33 | Description : returns name a country. 34 | Returns `string` 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (C) 2012-2016 Mariusz Nowak (www.medikoo.com) 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /country-capital.json: -------------------------------------------------------------------------------- 1 | [ 2 | {"country" : "Afghanistan","city" : "Kabul"}, 3 | {"country" : "Albania","city" : "Tirana"}, 4 | {"country" : "Algeria","city" : "Alger"}, 5 | {"country" : "American Samoa","city" : "Fagatogo"}, 6 | {"country" : "Andorra","city" : "Andorra la Vella"}, 7 | {"country" : "Angola","city" : "Luanda"}, 8 | {"country" : "Anguilla","city" : "The Valley"}, 9 | {"country" : "Antarctica","city" : null}, 10 | {"country" : "Antigua and Barbuda","city" : "Saint John's"}, 11 | {"country" : "Argentina","city" : "Buenos Aires"}, 12 | {"country" : "Armenia","city" : "Yerevan"}, 13 | {"country" : "Aruba","city" : "Oranjestad"}, 14 | {"country" : "Australia","city" : "Canberra"}, 15 | {"country" : "Austria","city" : "Wien"}, 16 | {"country" : "Azerbaijan","city" : "Baku"}, 17 | {"country" : "Bahamas","city" : "Nassau"}, 18 | {"country" : "Bahrain","city" : "al-Manama"}, 19 | {"country" : "Bangladesh","city" : "Dhaka"}, 20 | {"country" : "Barbados","city" : "Bridgetown"}, 21 | {"country" : "Belarus","city" : "Minsk"}, 22 | {"country" : "Belgium","city" : "Bruxelles [Brussel]"}, 23 | {"country" : "Belize","city" : "Belmopan"}, 24 | {"country" : "Benin","city" : "Porto-Novo"}, 25 | {"country" : "Bermuda","city" : "Hamilton"}, 26 | {"country" : "Bhutan","city" : "Thimphu"}, 27 | {"country" : "Bolivia","city" : "La Paz"}, 28 | {"country" : "Bosnia and Herzegovina","city" : "Sarajevo"}, 29 | {"country" : "Botswana","city" : "Gaborone"}, 30 | {"country" : "Bouvet Island","city" : null}, 31 | {"country" : "Brazil","city" : "Brasília"}, 32 | {"country" : "British Indian Ocean Territory","city" : null}, 33 | {"country" : "Brunei","city" : "Bandar Seri Begawan"}, 34 | {"country" : "Bulgaria","city" : "Sofia"}, 35 | {"country" : "Burkina Faso","city" : "Ouagadougou"}, 36 | {"country" : "Burundi","city" : "Bujumbura"}, 37 | {"country" : "Cambodia","city" : "Phnom Penh"}, 38 | {"country" : "Cameroon","city" : "Yaound"}, 39 | {"country" : "Canada","city" : "Ottawa"}, 40 | {"country" : "Cape Verde","city" : "Praia"}, 41 | {"country" : "Cayman Islands","city" : "George Town"}, 42 | {"country" : "Central African Republic","city" : "Bangui"}, 43 | {"country" : "Chad","city" : "N'Djam"}, 44 | {"country" : "Chile","city" : "Santiago de Chile"}, 45 | {"country" : "China","city" : "Peking"}, 46 | {"country" : "Christmas Island","city" : "Flying Fish Cove"}, 47 | {"country" : "Cocos (Keeling) Islands","city" : "West Island"}, 48 | {"country" : "Colombia","city" : "Santaf"}, 49 | {"country" : "Comoros","city" : "Moroni"}, 50 | {"country" : "Congo","city" : "Brazzaville"}, 51 | {"country" : "Congo, The Democratic Republic of the","city" : "Kinshasa"}, 52 | {"country" : "Cook Islands","city" : "Avarua"}, 53 | {"country" : "Costa Rica","city" : "San Jos"}, 54 | {"country" : "Ivory Coast","city" : "Yamoussoukro"}, 55 | {"country" : "Croatia","city" : "Zagreb"}, 56 | {"country" : "Cuba","city" : "La Habana"}, 57 | {"country" : "Cyprus","city" : "Nicosia"}, 58 | {"country" : "Czech Republic","city" : "Praha"}, 59 | {"country" : "Denmark","city" : "Copenhagen"}, 60 | {"country" : "Djibouti","city" : "Djibouti"}, 61 | {"country" : "Dominica","city" : "Roseau"}, 62 | {"country" : "Dominican Republic","city" : "Santo Domingo de Guzm"}, 63 | {"country" : "East Timor","city" : "Dili"}, 64 | {"country" : "Ecuador","city" : "Quito"}, 65 | {"country" : "Egypt","city" : "Cairo"}, 66 | {"country" : "England","city" : "London"}, 67 | {"country" : "El Salvador","city" : "San Salvador"}, 68 | {"country" : "Equatorial Guinea","city" : "Malabo"}, 69 | {"country" : "Eritrea","city" : "Asmara"}, 70 | {"country" : "Estonia","city" : "Tallinn"}, 71 | {"country" : "Ethiopia","city" : "Addis Abeba"}, 72 | {"country" : "Falkland Islands","city" : "Stanley"}, 73 | {"country" : "Faroe Islands","city" : "Tórshavn"}, 74 | {"country" : "Fiji Islands","city" : "Suva"}, 75 | {"country" : "Finland","city" : "Helsinki [Helsingfors]"}, 76 | {"country" : "France","city" : "Paris"}, 77 | {"country" : "French Guiana","city" : "Cayenne"}, 78 | {"country" : "French Polynesia","city" : "Papeete"}, 79 | {"country" : "French Southern territories","city" : null}, 80 | {"country" : "Gabon","city" : "Libreville"}, 81 | {"country" : "Gambia","city" : "Banjul"}, 82 | {"country" : "Georgia","city" : "Tbilisi"}, 83 | {"country" : "Germany","city" : "Berlin"}, 84 | {"country" : "Ghana","city" : "Accra"}, 85 | {"country" : "Gibraltar","city" : "Gibraltar"}, 86 | {"country" : "Greece","city" : "Athenai"}, 87 | {"country" : "Greenland","city" : "Nuuk"}, 88 | {"country" : "Grenada","city" : "Saint George's"}, 89 | {"country" : "Guadeloupe","city" : "Basse-Terre"}, 90 | {"country" : "Guam","city" : "Aga"}, 91 | {"country" : "Guatemala","city" : "Ciudad de Guatemala"}, 92 | {"country" : "Guinea","city" : "Conakry"}, 93 | {"country" : "Guinea-Bissau","city" : "Bissau"}, 94 | {"country" : "Guyana","city" : "Georgetown"}, 95 | {"country" : "Haiti","city" : "Port-au-Prince"}, 96 | {"country" : "Heard Island and McDonald Islands","city" : null}, 97 | {"country" : "Holy See (Vatican City State)","city" : "Citt"}, 98 | {"country" : "Honduras","city" : "Tegucigalpa"}, 99 | {"country" : "Hong Kong","city" : "Victoria"}, 100 | {"country" : "Hungary","city" : "Budapest"}, 101 | {"country" : "Iceland","city" : "Reykjav"}, 102 | {"country" : "India","city" : "New Delhi"}, 103 | {"country" : "Indonesia","city" : "Jakarta"}, 104 | {"country" : "Iran","city" : "Tehran"}, 105 | {"country" : "Iraq","city" : "Baghdad"}, 106 | {"country" : "Ireland","city" : "Dublin"}, 107 | {"country" : "Israel","city" : "Jerusalem"}, 108 | {"country" : "Italy","city" : "Roma"}, 109 | {"country" : "Jamaica","city" : "Kingston"}, 110 | {"country" : "Japan","city" : "Tokyo"}, 111 | {"country" : "Jordan","city" : "Amman"}, 112 | {"country" : "Kazakstan","city" : "Astana"}, 113 | {"country" : "Kenya","city" : "Nairobi"}, 114 | {"country" : "Kiribati","city" : "Bairiki"}, 115 | {"country" : "Kuwait","city" : "Kuwait"}, 116 | {"country" : "Kyrgyzstan","city" : "Bishkek"}, 117 | {"country" : "Laos","city" : "Vientiane"}, 118 | {"country" : "Latvia","city" : "Riga"}, 119 | {"country" : "Lebanon","city" : "Beirut"}, 120 | {"country" : "Lesotho","city" : "Maseru"}, 121 | {"country" : "Liberia","city" : "Monrovia"}, 122 | {"country" : "Libyan Arab Jamahiriya","city" : "Tripoli"}, 123 | {"country" : "Liechtenstein","city" : "Vaduz"}, 124 | {"country" : "Lithuania","city" : "Vilnius"}, 125 | {"country" : "Luxembourg","city" : "Luxembourg [Luxemburg/L"}, 126 | {"country" : "Macao","city" : "Macao"}, 127 | {"country" : "Macedonia","city" : "Skopje"}, 128 | {"country" : "Madagascar","city" : "Antananarivo"}, 129 | {"country" : "Malawi","city" : "Lilongwe"}, 130 | {"country" : "Malaysia","city" : "Kuala Lumpur"}, 131 | {"country" : "Maldives","city" : "Male"}, 132 | {"country" : "Mali","city" : "Bamako"}, 133 | {"country" : "Malta","city" : "Valletta"}, 134 | {"country" : "Marshall Islands","city" : "Dalap-Uliga-Darrit"}, 135 | {"country" : "Martinique","city" : "Fort-de-France"}, 136 | {"country" : "Mauritania","city" : "Nouakchott"}, 137 | {"country" : "Mauritius","city" : "Port-Louis"}, 138 | {"country" : "Mayotte","city" : "Mamoutzou"}, 139 | {"country" : "Mexico","city" : "Ciudad de M"}, 140 | {"country" : "Micronesia, Federated States of","city" : "Palikir"}, 141 | {"country" : "Moldova","city" : "Chisinau"}, 142 | {"country" : "Monaco","city" : "Monaco-Ville"}, 143 | {"country" : "Mongolia","city" : "Ulan Bator"}, 144 | {"country" : "Montserrat","city" : "Plymouth"}, 145 | {"country" : "Morocco","city" : "Rabat"}, 146 | {"country" : "Mozambique","city" : "Maputo"}, 147 | {"country" : "Myanmar","city" : "Rangoon (Yangon)"}, 148 | {"country" : "Namibia","city" : "Windhoek"}, 149 | {"country" : "Nauru","city" : "Yaren"}, 150 | {"country" : "Nepal","city" : "Kathmandu"}, 151 | {"country" : "Netherlands","city" : "Amsterdam"}, 152 | {"country" : "Netherlands Antilles","city" : "Willemstad"}, 153 | {"country" : "New Caledonia","city" : "Noum"}, 154 | {"country" : "New Zealand","city" : "Wellington"}, 155 | {"country" : "Nicaragua","city" : "Managua"}, 156 | {"country" : "Niger","city" : "Niamey"}, 157 | {"country" : "Nigeria","city" : "Abuja"}, 158 | {"country" : "Niue","city" : "Alofi"}, 159 | {"country" : "Norfolk Island","city" : "Kingston"}, 160 | {"country" : "North Korea","city" : "Pyongyang"}, 161 | {"country" : "Northern Ireland","city" : "Belfast"}, 162 | {"country" : "Northern Mariana Islands","city" : "Garapan"}, 163 | {"country" : "Norway","city" : "Oslo"}, 164 | {"country" : "Oman","city" : "Masqat"}, 165 | {"country" : "Pakistan","city" : "Islamabad"}, 166 | {"country" : "Palau","city" : "Koror"}, 167 | {"country" : "Palestine","city" : "Gaza"}, 168 | {"country" : "Panama","city" : "Ciudad de Panam"}, 169 | {"country" : "Papua New Guinea","city" : "Port Moresby"}, 170 | {"country" : "Paraguay","city" : "Asunci"}, 171 | {"country" : "Peru","city" : "Lima"}, 172 | {"country" : "Philippines","city" : "Manila"}, 173 | {"country" : "Pitcairn","city" : "Adamstown"}, 174 | {"country" : "Poland","city" : "Warszawa"}, 175 | {"country" : "Portugal","city" : "Lisboa"}, 176 | {"country" : "Puerto Rico","city" : "San Juan"}, 177 | {"country" : "Qatar","city" : "Doha"}, 178 | {"country" : "Reunion","city" : "Saint-Denis"}, 179 | {"country" : "Romania","city" : "Bucuresti"}, 180 | {"country" : "Russian Federation","city" : "Moscow"}, 181 | {"country" : "Rwanda","city" : "Kigali"}, 182 | {"country" : "Saint Helena","city" : "Jamestown"}, 183 | {"country" : "Saint Kitts and Nevis","city" : "Basseterre"}, 184 | {"country" : "Saint Lucia","city" : "Castries"}, 185 | {"country" : "Saint Pierre and Miquelon","city" : "Saint-Pierre"}, 186 | {"country" : "Saint Vincent and the Grenadines","city" : "Kingstown"}, 187 | {"country" : "Samoa","city" : "Apia"}, 188 | {"country" : "San Marino","city" : "San Marino"}, 189 | {"country" : "Sao Tome and Principe","city" : "S"}, 190 | {"country" : "Saudi Arabia","city" : "Riyadh"}, 191 | {"country" : "Scotland","city" : "Edinburgh"}, 192 | {"country" : "Senegal","city" : "Dakar"}, 193 | {"country" : "Seychelles","city" : "Victoria"}, 194 | {"country" : "Sierra Leone","city" : "Freetown"}, 195 | {"country" : "Singapore","city" : "Singapore"}, 196 | {"country" : "Slovakia","city" : "Bratislava"}, 197 | {"country" : "Slovenia","city" : "Ljubljana"}, 198 | {"country" : "Solomon Islands","city" : "Honiara"}, 199 | {"country" : "Somalia","city" : "Mogadishu"}, 200 | {"country" : "South Africa","city" : "Pretoria"}, 201 | {"country" : "South Georgia and the South Sandwich Islands","city" : null}, 202 | {"country" : "South Korea","city" : "Seoul"}, 203 | {"country" : "South Sudan","city" : "Juba"}, 204 | {"country" : "Spain","city" : "Madrid"}, 205 | {"country" : "Sri Lanka","city" : "Colombo"}, 206 | {"country" : "Sudan","city" : "Khartum"}, 207 | {"country" : "Suriname","city" : "Paramaribo"}, 208 | {"country" : "Svalbard and Jan Mayen","city" : "Longyearbyen"}, 209 | {"country" : "Swaziland","city" : "Mbabane"}, 210 | {"country" : "Sweden","city" : "Stockholm"}, 211 | {"country" : "Switzerland","city" : "Bern"}, 212 | {"country" : "Syria","city" : "Damascus"}, 213 | {"country" : "Taiwan","city" : "Taipei"}, 214 | {"country" : "Tajikistan","city" : "Dushanbe"}, 215 | {"country" : "Tanzania","city" : "Dodoma"}, 216 | {"country" : "Thailand","city" : "Bangkok"}, 217 | {"country" : "Togo","city" : "Lom"}, 218 | {"country" : "Tokelau","city" : "Fakaofo"}, 219 | {"country" : "Tonga","city" : "Nuku'alofa"}, 220 | {"country" : "Trinidad and Tobago","city" : "Port-of-Spain"}, 221 | {"country" : "Tunisia","city" : "Tunis"}, 222 | {"country" : "Turkey","city" : "Ankara"}, 223 | {"country" : "Turkmenistan","city" : "Ashgabat"}, 224 | {"country" : "Turks and Caicos Islands","city" : "Cockburn Town"}, 225 | {"country" : "Tuvalu","city" : "Funafuti"}, 226 | {"country" : "Uganda","city" : "Kampala"}, 227 | {"country" : "Ukraine","city" : "Kyiv"}, 228 | {"country" : "United Arab Emirates","city" : "Abu Dhabi"}, 229 | {"country" : "United Kingdom","city" : "London"}, 230 | {"country" : "United States","city" : "Washington"}, 231 | {"country" : "United States Minor Outlying Islands","city" : null}, 232 | {"country" : "Uruguay","city" : "Montevideo"}, 233 | {"country" : "Uzbekistan","city" : "Toskent"}, 234 | {"country" : "Vanuatu","city" : "Port-Vila"}, 235 | {"country" : "Venezuela","city" : "Caracas"}, 236 | {"country" : "Vietnam","city" : "Hanoi"}, 237 | {"country" : "Virgin Islands, British","city" : "Road Town"}, 238 | {"country" : "Virgin Islands, U.S.","city" : "Charlotte Amalie"}, 239 | {"country" : "Wales","city" : "Cardiff"}, 240 | {"country" : "Wallis and Futuna","city" : "Mata-Utu"}, 241 | {"country" : "Western Sahara","city" : "El-Aai"}, 242 | {"country" : "Yemen","city" : "Sanaa"}, 243 | {"country" : "Yugoslavia","city" : "Beograd"}, 244 | {"country" : "Zambia","city" : "Lusaka"}, 245 | {"country" : "Zimbabwe","city" : "Harare"} 246 | ] 247 | --------------------------------------------------------------------------------