Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement dropdown of regions for large countries.
- Loading branch information
Showing
12 changed files
with
2,330 additions
and
2,282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
env: { | ||
browser: true | ||
}, | ||
extends: [ | ||
'eslint-config-digitalbazaar/vue' | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ coverage | |
node_modules | ||
reports | ||
.cache | ||
bin/config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,28 @@ | ||
# bedrock-vue-address-form | ||
|
||
A Vue.js address form component for Bedrock Web apps | ||
|
||
### View the component in the test harness | ||
``` | ||
npm i | ||
cd test/ | ||
npm i | ||
npm start | ||
``` | ||
|
||
### Re-Build countries.js | ||
|
||
1. Create an account on http://www.geonames.org/ | ||
2. Navigate to the `bin/` directory | ||
3. Update `username` in `config.json` with your geoname's username | ||
4. Use the created JSON file to update `countries.js` | ||
``` | ||
cd bin/ | ||
node index.js | ||
``` | ||
|
||
### Additional Information | ||
|
||
Geonames Country Data | ||
* Source: http://www.geonames.org/ | ||
* License: https://creativecommons.org/licenses/by/4.0/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
env: { | ||
node: true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"username": "demo", | ||
"include": ["iso", "children", "name"], | ||
"filename": "countries.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
const fetch = require('node-fetch'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const config = require('./config.json'); | ||
|
||
const COUNTRIES = 'http://download.geonames.org/export/dump/countryInfo.txt'; | ||
|
||
function requestChildren(id, username = 'demo') { | ||
const base = 'http://api.geonames.org/childrenJSON'; | ||
const request = encodeURI(`${base}?geonameId=${id}&username=${username}`); | ||
return fetch(request).then(r => r.json()); | ||
} | ||
|
||
class Country { | ||
constructor(data) { | ||
this.line = data; | ||
this.iso = data[0], | ||
this.iso3 = data[1], | ||
this.iso_numeric = data[2], | ||
this.fips = data[3], | ||
this.name = data[4], | ||
this.capital = data[5], | ||
this.area = Number(data[6]), | ||
this.population = data[7], | ||
this.continent = data[8], | ||
this.tld = data[9], | ||
this.currency_code = data[10], | ||
this.currency_name = data[11], | ||
this.phone = data[12], | ||
this.postal_code_format = data[13], | ||
this.postal_code_regex = data[14], | ||
this.languages = data[15], | ||
this.geoname_id = data[16], | ||
this.neighbours = data[17], | ||
this.children = []; | ||
this.getChildren = this.getChildren.bind(this); | ||
} | ||
getChildren() { | ||
if(this.area >= 1000000) { | ||
return requestChildren(this.geoname_id, config.username) | ||
.then(children => { | ||
if(children && children.geonames) { | ||
this.children = children.geonames.map(gn => gn.name); | ||
} | ||
return this; | ||
}); | ||
} | ||
return this; | ||
} | ||
} | ||
|
||
function parseCountries(text) { | ||
const lines = text.split('\n'); | ||
const notCommentedOut = lines.filter(l => !l.startsWith('#')); | ||
const cells = notCommentedOut.map(l => l.split('\t')); | ||
const countries = cells.map(c => new Country(c)); | ||
return Promise.all(countries.map(c => c.getChildren())); | ||
} | ||
|
||
function saveCountries(data) { | ||
const countries = data.map(country => { | ||
for(const key in country) { | ||
if(!config.include.includes(key)) { | ||
delete country[key]; | ||
} | ||
} | ||
country.label = country.name || ''; | ||
country.value = country.iso || ''; | ||
delete country.name; | ||
delete country.iso; | ||
return country; | ||
}) | ||
.filter(c => c.label.length) | ||
.sort((a, b) => a.label.localeCompare(b.label)); | ||
const filepath = path.join(__dirname, `${config.filename}`); | ||
fs.writeFile( | ||
filepath, JSON.stringify(countries, null, 2), 'utf-8', console.error); | ||
} | ||
|
||
function getCountries() { | ||
return fetch(COUNTRIES) | ||
.then(r => r.text()) | ||
.then(parseCountries) | ||
.then(saveCountries); | ||
} | ||
|
||
getCountries().then(c => console.log('countries', c)); |
Oops, something went wrong.