From 7c7fb53b55bd1144ccc8da029f4e5e9de25c8689 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sun, 12 Nov 2017 20:19:33 +0100 Subject: [PATCH] query locations --- index.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 73615013..7d23af94 100644 --- a/index.js +++ b/index.js @@ -91,7 +91,37 @@ const createClient = (profile) => { }) } - return {departures, journeys} + const locations = (query, opt = {}) => { + if ('string' !== typeof query) throw new Error('query must be a string.') + opt = Object.assign({ + fuzzy: true, // find only exact matches? + results: 10, // how many search results? + stations: true, + addresses: true, + poi: true // points of interest + }, opt) + + const f = profile.formatLocationFilter(opt.stations, opt.addresses, opt.poi) + return request(profile, { + cfg: {polyEnc: 'GPA'}, + meth: 'LocMatch', + req: {input: { + loc: { + type: f, + name: opt.fuzzy ? query + '?' : query + }, + maxLoc: opt.results, + field: 'S' // todo: what is this? + }} + }) + .then((d) => { + if (!d.match || !Array.isArray(d.match.locL)) return [] + const parse = profile.parseLocation + return d.match.locL.map(loc => parse(profile, loc)) + }) + } + + return {departures, journeys, locations} } module.exports = createClient