diff --git a/.jshintrc b/.jshintrc index 159fb604..aa3f1574 100644 --- a/.jshintrc +++ b/.jshintrc @@ -2,7 +2,7 @@ "node": true, "curly": true, "eqeqeq": true, - "esversion": 6, + "esversion": 8, "freeze": true, "immed": true, "indent": 2, diff --git a/api/extract.js b/api/extract.js index 4e9f187b..add15f0f 100644 --- a/api/extract.js +++ b/api/extract.js @@ -1,6 +1,7 @@ const Database = require('better-sqlite3'); const query = { extract: require('../query/extract') }; const analyze = require('../lib/analyze'); +const asyncForEach = require('../lib/asyncForEach'); // export setup method function setup( addressDbPath, streetDbPath ){ @@ -12,7 +13,7 @@ function setup( addressDbPath, streetDbPath ){ db.exec(`ATTACH DATABASE '${streetDbPath}' as 'street'`); // query method - var q = function( coord, names, cb ){ + var q = async function( coord, names, cb ){ var point = { lat: parseFloat( coord.lat ), @@ -20,8 +21,8 @@ function setup( addressDbPath, streetDbPath ){ }; var normalized = []; - names.forEach( function( name ){ - normalized = normalized.concat( analyze.street( name ) ); + await asyncForEach(names, async (name) => { + normalized = normalized.concat( await analyze.street( name ) ); }); // error checking diff --git a/api/search.js b/api/search.js index dfbfb829..02f02b3a 100644 --- a/api/search.js +++ b/api/search.js @@ -19,7 +19,7 @@ function setup( addressDbPath, streetDbPath ){ db.exec('PRAGMA street.mmap_size=268435456;'); // query method - var q = function( coord, number, street, cb ){ + var q = async function( coord, number, street, cb ){ var point = { lat: parseFloat( coord.lat ), @@ -31,7 +31,7 @@ function setup( addressDbPath, streetDbPath ){ var normalized = { number: analyze.housenumber( number ), - street: analyze.street( street ) + street: await analyze.street( street ) }; // error checking diff --git a/cmd/server.js b/cmd/server.js index 5b6033f6..ca30f962 100644 --- a/cmd/server.js +++ b/cmd/server.js @@ -217,10 +217,10 @@ app.use('/demo', express.static('demo')); // app.use('/builds', express.static('/data/builds')); // app.use('/builds', directory('/data/builds', { hidden: false, icons: false, view: 'details' })); -app.listen( PORT, function() { +app.listen( PORT, async function() { // force loading of libpostal - analyze.street( 'test street' ); + await analyze.street( 'test street' ); console.log( 'server listening on port', PORT ); }); diff --git a/lib/analyze.js b/lib/analyze.js index cc13cf99..4eea5a49 100644 --- a/lib/analyze.js +++ b/lib/analyze.js @@ -31,11 +31,11 @@ function get_libpostal() { /** analyze input streetname string and return a list of expansions. **/ -function street( streetName ){ +async function street( streetName ){ const postal = get_libpostal(); // use libpostal to expand the address - var expansions = postal.expand.expand_address( streetName ); + let expansions = await postal.expand.expand_address( streetName ); // remove ordinals expansions = expansions.map(function( item ){ diff --git a/lib/asyncForEach.js b/lib/asyncForEach.js new file mode 100644 index 00000000..69c6cec7 --- /dev/null +++ b/lib/asyncForEach.js @@ -0,0 +1,8 @@ +// async friendly version of Array.forEach +async function asyncForEach(array, callback) { + for (let index = 0; index < array.length; index++) { + await callback(array[index], index, array); + } +} + +module.exports = asyncForEach; diff --git a/stream/address/lookup.js b/stream/address/lookup.js index 45400803..10d0fee1 100644 --- a/stream/address/lookup.js +++ b/stream/address/lookup.js @@ -17,7 +17,7 @@ if( hasFD3 ){ function streamFactory(db){ - return through.obj(function( batch, _, next ){ + return through.obj(async function( batch, _, next ){ // invalid batch if( !batch || !batch.length ){ @@ -30,7 +30,7 @@ function streamFactory(db){ // all street names in batch should be the same // perform libpostal normalization - var names = analyze.street( result.getStreet() ); + var names = await analyze.street( result.getStreet() ); // ensure at least one name was produced if( !names.length ){ diff --git a/stream/street/augment.js b/stream/street/augment.js index 84564222..31e7bdf2 100644 --- a/stream/street/augment.js +++ b/stream/street/augment.js @@ -1,5 +1,6 @@ const through = require('through2'); const analyze = require('../../lib/analyze'); +const asyncForEach = require('../../lib/asyncForEach'); // increase/decrease bbox bounds by this much in order to find houses which // might be slighly outside the bounds. @@ -15,12 +16,12 @@ const FUDGE_FACTOR = 0.005; **/ // the transform function is executed once per batch in the stream. -const transform = (street, _, next) => { +const transform = async (street, _, next) => { // normalize all names let names = []; - street.getNames().forEach(function (name) { - names = names.concat(analyze.street(name)); + await asyncForEach(street.getNames(), async (name) => { + names = names.concat(await analyze.street(name)); }); // if the source file contains no valid names for this polyline diff --git a/test/lib/analyze.js b/test/lib/analyze.js index 9ae15f6f..8590cbb0 100644 --- a/test/lib/analyze.js +++ b/test/lib/analyze.js @@ -4,18 +4,18 @@ var analyze = require('../../lib/analyze'); module.exports.analyze = {}; module.exports.analyze.street = function(test) { - test('street: synonym expansions', function(t) { - var perms = analyze.street('grolmanstraße'); + test('street: synonym expansions', async function(t) { + var perms = await analyze.street('grolmanstraße'); t.deepEqual(perms, ['grolmanstraße', 'grolman straße']); t.end(); }); - test('street: remove ordinals', function(t) { - var perms = analyze.street('West 26th st'); + test('street: remove ordinals', async function(t) { + var perms = await analyze.street('West 26th st'); t.deepEqual(perms, ['west 26 street', 'west 26 saint']); t.end(); }); - test('street: always returns array', function(t) { - var perms = analyze.street(''); + test('street: always returns array', async function(t) { + var perms = await analyze.street(''); t.deepEqual(perms, ['']); t.end(); });