Skip to content

Commit

Permalink
use newer es6 let
Browse files Browse the repository at this point in the history
  • Loading branch information
bhousel committed Oct 22, 2018
1 parent 04107cf commit ecdbff7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 59 deletions.
22 changes: 11 additions & 11 deletions build_allNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if (process.argv.length < 3) {
const checkKeys = ['amenity', 'shop', 'leisure', 'man_made', 'tourism'];
const THRESHOLD = process.argv[3] || 50;

var counts = {};
let counts = {};
build();


Expand All @@ -36,25 +36,25 @@ function build() {
// Start clean
shell.rm('-f', ['dist/allNames.json']);

var handler = new osmium.Handler();
let handler = new osmium.Handler();
handler.options({ tagged_nodes_only: true });
handler.on('node', countTags);
handler.on('way', countTags);
handler.on('relation', countTags);

var reader = new osmium.Reader(process.argv[2]);
let reader = new osmium.Reader(process.argv[2]);
osmium.apply(reader, handler);

// filter
var filtered = {};
for (var key in counts) {
let filtered = {};
for (let key in counts) {
if (counts[key] > THRESHOLD) {
filtered[key] = counts[key];
}
}

// sort
var sorted = {};
let sorted = {};
Object.keys(filtered).sort().forEach(function(k) {
sorted[k] = filtered[k];
});
Expand All @@ -65,15 +65,15 @@ function build() {


function countTags(entity) {
var name = entity.tags('name'); // fast name check
let name = entity.tags('name'); // fast name check
if (!name) return;
var tags = entity.tags();
let tags = entity.tags();

for (var i = 0; i < checkKeys.length; i++) {
var key = checkKeys[i];
for (let i = 0; i < checkKeys.length; i++) {
let key = checkKeys[i];
if (!tags[key]) continue;

var fullName = key + '/' + tags[key] + '|' + name;
let fullName = key + '/' + tags[key] + '|' + name;
counts[fullName] = (counts[fullName] || 0) + 1;
}
}
Expand Down
78 changes: 39 additions & 39 deletions build_filterNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const stringify = require('json-stringify-pretty-compact');

const allNames = require('./dist/allNames.json');
const filters = require('./config/filters.json');
var canonical = require('./config/canonical.json');
let canonical = require('./config/canonical.json');

// perform JSON-schema validation
const Validator = require('jsonschema').Validator;
Expand All @@ -18,9 +18,9 @@ validateSchema('config/canonical.json', canonical, canonicalSchema);


// all names start out in discard..
var discard = Object.assign({}, allNames);
var keep = {};
var rIndex = {};
let discard = Object.assign({}, allNames);
let keep = {};
let rIndex = {};

filterNames();
mergeConfig();
Expand All @@ -29,8 +29,8 @@ mergeConfig();

// Perform JSON Schema validation
function validateSchema(fileName, object, schema) {
var v = new Validator();
var validationErrors = v.validate(object, schema).errors;
let v = new Validator();
let validationErrors = v.validate(object, schema).errors;
if (validationErrors.length) {
console.error(colors.red('\nError - Schema validation:'));
console.error(' ' + colors.yellow(fileName + ': '));
Expand Down Expand Up @@ -67,9 +67,9 @@ function filterNames() {

// filter by keepTags (move from discard -> keep)
filters.keepTags.forEach(s => {
var re = new RegExp(s, 'i');
for (var key in discard) {
var tag = key.split('|', 2)[0];
let re = new RegExp(s, 'i');
for (let key in discard) {
let tag = key.split('|', 2)[0];
if (re.test(tag)) {
keep[key] = discard[key];
delete discard[key];
Expand All @@ -79,8 +79,8 @@ function filterNames() {

// filter by discardKeys (move from keep -> discard)
filters.discardKeys.forEach(s => {
var re = new RegExp(s, 'i');
for (var key in keep) {
let re = new RegExp(s, 'i');
for (let key in keep) {
if (re.test(key)) {
discard[key] = keep[key];
delete keep[key];
Expand All @@ -90,9 +90,9 @@ function filterNames() {

// filter by discardNames (move from keep -> discard)
filters.discardNames.forEach(s => {
var re = new RegExp(s, 'i');
for (var key in keep) {
var name = key.split('|', 2)[1];
let re = new RegExp(s, 'i');
for (let key in keep) {
let name = key.split('|', 2)[1];
if (re.test(name)) {
discard[key] = keep[key];
delete keep[key];
Expand Down Expand Up @@ -121,12 +121,12 @@ function mergeConfig() {
Object.keys(keep).forEach(k => {
if (rIndex[k]) return;

var obj = canonical[k];
var parts = k.split('|', 2);
var tag = parts[0].split('/', 2);
var key = tag[0];
var value = tag[1];
var name = parts[1];
let obj = canonical[k];
let parts = k.split('|', 2);
let tag = parts[0].split('/', 2);
let key = tag[0];
let value = tag[1];
let name = parts[1];

if (!obj) {
obj = { count: 0, tags: {} };
Expand Down Expand Up @@ -171,7 +171,7 @@ function mergeConfig() {
// (This is useful for file diffing)
//
function sort(obj) {
var sorted = {};
let sorted = {};
Object.keys(obj).sort().forEach(k => {
sorted[k] = Array.isArray(obj[k]) ? obj[k].sort() : obj[k];
});
Expand All @@ -183,12 +183,12 @@ function sort(obj) {
// Returns a reverse index to map match keys back to their original keys
//
function buildReverseIndex() {
var warnCollisions = [];
let warnCollisions = [];

for (var key in canonical) {
for (let key in canonical) {
if (canonical[key].match) {
for (var i = canonical[key].match.length - 1; i >= 0; i--) {
var match = canonical[key].match[i];
for (let i = canonical[key].match.length - 1; i >= 0; i--) {
let match = canonical[key].match[i];
if (rIndex[match]) {
warnCollisions.push([rIndex[match], match]);
warnCollisions.push([key, match]);
Expand All @@ -212,14 +212,14 @@ function buildReverseIndex() {
// Checks all the entries in `canonical.json` for several kinds of issues
//
function checkCanonical() {
var warnUncommon = [];
var warnMatched = [];
var warnDuplicate = [];
var warnFormatWikidata = [];
var warnFormatWikipedia = [];
var warnMissingWikidata = [];
var warnMissingWikipedia = [];
var seen = {};
let warnUncommon = [];
let warnMatched = [];
let warnDuplicate = [];
let warnFormatWikidata = [];
let warnFormatWikipedia = [];
let warnMissingWikidata = [];
let warnMissingWikipedia = [];
let seen = {};

Object.keys(canonical).forEach(k => {
// Warn if the item is uncommon (i.e. not found in keepNames)
Expand All @@ -238,11 +238,11 @@ function checkCanonical() {
}

// Warn if the name appears to be a duplicate
var stem = stemmer(k.split('|', 2)[1]);
var other = seen[stem];
let stem = stemmer(k.split('|', 2)[1]);
let other = seen[stem];
if (other) {
// suppress warning?
var suppress = false;
let suppress = false;
if (canonical[other].nomatch && canonical[other].nomatch.indexOf(k) !== -1) {
suppress = true;
} else if (canonical[k].nomatch && canonical[k].nomatch.indexOf(other) !== -1) {
Expand All @@ -256,13 +256,13 @@ function checkCanonical() {


// Warn if `brand:wikidata` or `brand:wikipedia` tags are missing or look wrong..
var wd = canonical[k].tags['brand:wikidata'];
let wd = canonical[k].tags['brand:wikidata'];
if (!wd) {
warnMissingWikidata.push(k);
} else if (!/^Q\d+$/.test(wd)) {
warnFormatWikidata.push([k, wd]);
}
var wp = canonical[k].tags['brand:wikipedia'];
let wp = canonical[k].tags['brand:wikipedia'];
if (!wp) {
warnMissingWikipedia.push(k);
} else if (!/^[a-z_]{2,}:[^_]*$/.test(wp)) {
Expand Down Expand Up @@ -346,7 +346,7 @@ function checkCanonical() {
// Removes noise from the name so that we can compare
// similar names for catching duplicates.
function stemmer(name) {
var noise = [
let noise = [
/ban(k|c)(a|o)?/ig,
/банк/ig,
/coop/ig,
Expand Down
14 changes: 7 additions & 7 deletions build_nameSuggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function buildJSON() {


function buildXML() {
var presets = xmlbuilder
let presets = xmlbuilder
.create('presets', { version: '1.0', encoding: 'UTF-8' })
.att('xmlns', 'http://josm.openstreetmap.de/tagging-preset-1.0')
.att('author', 'Name Suggestion Index')
Expand All @@ -66,32 +66,32 @@ function buildXML() {
.att('link', 'https://github.com/' + package.repository)
.att('version', package.version);

var topgroup = presets
let topgroup = presets
.ele('group')
.att('name', 'Name Suggestion Index');

// Create JOSM presets using the key and value structure from the json
// to organize the presets into JOSM preset groups.
for (let key in out) {
var keygroup = topgroup.ele('group').att('name', key);
let keygroup = topgroup.ele('group').att('name', key);

for (let value in out[key]) {
var valuegroup = keygroup.ele('group').att('name', value);
let valuegroup = keygroup.ele('group').att('name', value);

for (let name in out[key][value]) {
var item = valuegroup
let item = valuegroup
.ele('item')
.att('name', name)
.att('type', 'node,closedway,multipolygon');

var tags = out[key][value][name].tags;
let tags = out[key][value][name].tags;
for (let k in tags) {
item.ele('key').att('key', k).att('value', tags[k]);
}
}
}
}

var xmlstring = presets.end({ pretty: true })
let xmlstring = presets.end({ pretty: true })
fs.writeFileSync('dist/name-suggestions.presets.xml', xmlstring);
}
2 changes: 0 additions & 2 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const Validator = require('jsonschema').Validator;

const filters = require('./config/filters.json');
const canonical = require('./config/canonical.json');


const filtersSchema = require('./schema/filters.json');
const canonicalSchema = require('./schema/canonical.json');

Expand Down

0 comments on commit ecdbff7

Please sign in to comment.