mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.1 KiB
35 lines
1.1 KiB
const text_analyzer = require('pelias-text-analyzer'); |
|
const _ = require('lodash'); |
|
const iso3166 = require('iso3166-1'); |
|
const Debug = require('../helper/debug'); |
|
const debugLog = new Debug('controller:libpostal'); |
|
|
|
function setup(should_execute) { |
|
function controller( req, res, next ){ |
|
// bail early if req/res don't pass conditions for execution |
|
if (!should_execute(req, res)) { |
|
return next(); |
|
} |
|
const initialTime = debugLog.beginTimer(req); |
|
// parse text with query parser |
|
const parsed_text = text_analyzer.parse(req.clean.text); |
|
|
|
if (parsed_text !== undefined) { |
|
// if a known ISO2 country was parsed, convert it to ISO3 |
|
if (_.has(parsed_text, 'country') && iso3166.is2(_.toUpper(parsed_text.country))) { |
|
parsed_text.country = iso3166.to3(_.toUpper(parsed_text.country)); |
|
} |
|
|
|
req.clean.parser = 'libpostal'; |
|
req.clean.parsed_text = parsed_text; |
|
debugLog.push(req, {parsed_text: req.clean.parsed_text}); |
|
} |
|
debugLog.stopTimer(req, initialTime); |
|
return next(); |
|
|
|
} |
|
|
|
return controller; |
|
} |
|
|
|
module.exports = setup;
|
|
|