Browse Source

Merge pull request #1134 from pelias/master

Merge master into staging
pull/1138/head
Julian Simioni 6 years ago committed by GitHub
parent
commit
c7ee5bf965
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      .travis.yml
  2. 33
      controller/structured_libpostal.js
  3. 15
      package.json
  4. 2
      public/apiDoc.md
  5. 49
      test/unit/controller/structured_libpostal.js

5
.travis.yml

@ -5,15 +5,14 @@ notifications:
node_js: node_js:
- 6 - 6
- 8 - 8
- 10
matrix: matrix:
fast_finish: true fast_finish: true
script: npm run travis script: npm run travis
before_install: before_install:
- npm i -g npm - npm i -g npm
before_script:
- npm prune
after_success: after_success:
- npm run semantic-release - npx semantic-release
branches: branches:
except: except:
- /^v\d+\.\d+\.\d+$/ - /^v\d+\.\d+\.\d+$/

33
controller/structured_libpostal.js

@ -3,19 +3,20 @@ const Debug = require('../helper/debug');
const debugLog = new Debug('controller:libpostal'); const debugLog = new Debug('controller:libpostal');
const logger = require('pelias-logger').get('api'); const logger = require('pelias-logger').get('api');
// if there's a house_number in the libpostal response, return it // Find field in libpostal response
// otherwise return the postcode field (which may be undefined) function findField(response, field, replacementField) {
function findHouseNumberField(response) { const libpostalField = response.find(f => f.label === field);
const house_number_field = response.find(f => f.label === 'house_number');
if (libpostalField) {
if (house_number_field) { return libpostalField;
return house_number_field; } else if(replacementField) {
return response.find(f => f.label === replacementField);
} else {
return;
} }
return response.find(f => f.label === 'postcode');
} }
function setup(libpostalService, should_execute) { function setup(libpostalService, should_execute) {
function controller( req, res, next ){ function controller( req, res, next ){
// bail early if req/res don't pass conditions for execution // bail early if req/res don't pass conditions for execution
@ -35,7 +36,9 @@ function setup(libpostalService, should_execute) {
// libpostal parses some inputs, like `3370 cobbe ave`, as a postcode+street // libpostal parses some inputs, like `3370 cobbe ave`, as a postcode+street
// so because we're treating the entire field as a street address, it's safe // so because we're treating the entire field as a street address, it's safe
// to assume that an identified postcode is actually a house number. // to assume that an identified postcode is actually a house number.
const house_number_field = findHouseNumberField(response); // if there's a house_number in the libpostal response, return it
// otherwise return the postcode field (which may be undefined)
const house_number_field = findField(response, 'house_number', 'postcode');
// if we're fairly certain that libpostal identified a house number // if we're fairly certain that libpostal identified a house number
// (from either the house_number or postcode field), place it into the // (from either the house_number or postcode field), place it into the
@ -48,6 +51,14 @@ function setup(libpostalService, should_execute) {
// remove the first instance of the number and trim whitespace // remove the first instance of the number and trim whitespace
req.clean.parsed_text.street = _.trim(_.replace(req.clean.parsed_text.address, req.clean.parsed_text.number, '')); req.clean.parsed_text.street = _.trim(_.replace(req.clean.parsed_text.address, req.clean.parsed_text.number, ''));
// If libpostal have parsed unit then add it for search
const unit_field = findField(response, 'unit');
if(unit_field) {
req.clean.parsed_text.unit = unit_field.value;
// Removing unit from street and trim
req.clean.parsed_text.street = _.trim(_.replace(req.clean.parsed_text.street, req.clean.parsed_text.unit, ''));
}
} else { } else {
// otherwise no house number was identifiable, so treat the entire input // otherwise no house number was identifiable, so treat the entire input
// as a street // as a street

15
package.json

@ -1,6 +1,6 @@
{ {
"name": "pelias-api", "name": "pelias-api",
"version": "0.0.0-semantic-release", "version": "0.0.0-development",
"author": "pelias", "author": "pelias",
"description": "Pelias API", "description": "Pelias API",
"homepage": "https://github.com/pelias/api", "homepage": "https://github.com/pelias/api",
@ -14,12 +14,10 @@
"lint": "jshint .", "lint": "jshint .",
"start": "./bin/start", "start": "./bin/start",
"test": "npm run unit", "test": "npm run unit",
"travis": "npm run check-dependencies && npm test", "travis": "npm test",
"unit": "./bin/units", "unit": "./bin/units",
"validate": "npm ls", "validate": "npm ls",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"config": "node -e \"console.log(JSON.stringify(require( 'pelias-config' ).generate(require('./schema')), null, 2))\"", "config": "node -e \"console.log(JSON.stringify(require( 'pelias-config' ).generate(require('./schema')), null, 2))\"",
"check-dependencies": "node_modules/.bin/npm-check --production --ignore pelias-interpolation",
"prune": "npm prune" "prune": "npm prune"
}, },
"repository": { "repository": {
@ -72,7 +70,6 @@
"difflet": "^1.0.1", "difflet": "^1.0.1",
"istanbul": "^0.4.2", "istanbul": "^0.4.2",
"jshint": "^2.5.6", "jshint": "^2.5.6",
"npm-check": "git://github.com/orangejulius/npm-check.git#disable-update-check",
"nsp": "^3.0.0", "nsp": "^3.0.0",
"pelias-mock-logger": "1.2.0", "pelias-mock-logger": "1.2.0",
"precommit-hook": "^3.0.0", "precommit-hook": "^3.0.0",
@ -88,7 +85,9 @@
"lint", "lint",
"prune", "prune",
"validate", "validate",
"test", "test"
"check-dependencies" ],
] "release": {
"branch": "production"
}
} }

2
public/apiDoc.md

@ -1 +1 @@
## [View our documentation on GitHub](https://github.com/pelias/pelias-doc/blob/master/index.md) ## [View our documentation on GitHub](https://github.com/pelias/documentation/)

49
test/unit/controller/structured_libpostal.js

@ -291,6 +291,55 @@ module.exports.tests.success_conditions = (test, common) => {
}); });
test('service returning house_number and unit should set req.clean.parsed_text.unit', t => {
const service = (req, callback) => {
const response = [
{
label: 'road',
value: 'the street'
},
{
label: 'house_number',
value: '22'
},
{
label: 'unit',
value: '1 th'
}
];
callback(null, response);
};
const controller = libpostal(service, () => true);
const req = {
clean: {
parsed_text: {
address: 'the street 22 1 th'
}
},
errors: []
};
controller(req, undefined, () => {
t.deepEquals(req, {
clean: {
parsed_text: {
street: 'the street',
number: '22',
unit: '1 th'
}
},
errors: []
}, 'req should have been modified');
t.end();
});
});
// test('service returning valid response should convert and append', t => { // test('service returning valid response should convert and append', t => {
// const service = (req, callback) => { // const service = (req, callback) => {
// const response = [ // const response = [

Loading…
Cancel
Save