Skip to content

Commit

Permalink
feat(async): async-ify analyze.street()
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink committed Mar 20, 2020
1 parent a648854 commit 4fef160
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"node": true,
"curly": true,
"eqeqeq": true,
"esversion": 6,
"esversion": 8,
"freeze": true,
"immed": true,
"indent": 2,
Expand Down
7 changes: 4 additions & 3 deletions api/extract.js
Original file line number Diff line number Diff line change
@@ -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 ){
Expand All @@ -12,16 +13,16 @@ 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 ),
lon: parseFloat( coord.lon )
};

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
Expand Down
4 changes: 2 additions & 2 deletions api/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
});
4 changes: 2 additions & 2 deletions lib/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ){
Expand Down
8 changes: 8 additions & 0 deletions lib/asyncForEach.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions stream/address/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ){
Expand All @@ -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 ){
Expand Down
7 changes: 4 additions & 3 deletions stream/street/augment.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions test/lib/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down

0 comments on commit 4fef160

Please sign in to comment.