From 9b0d75e768d4edfd55cd11bc12331eaa5adc554a Mon Sep 17 00:00:00 2001 From: Freenerd Date: Mon, 14 Nov 2016 20:08:03 +0100 Subject: [PATCH] WIP --- index.js | 77 +++++++++++++++++++++++++++++++++++++++++++++- test/index_test.js | 65 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 3508b59fc..c5fa00742 100644 --- a/index.js +++ b/index.js @@ -60,7 +60,35 @@ module.exports = function(version, language) { return config.join(''); }, - compile: function(step) { + precompilePriorityTable: function(route) { + priorityTable = {}; + + function safeIncrease(obj, key) { + if (obj[key]) { + obj[key] = obj[key] + 1 + } else { + obj[key] = 1 + } + } + + route.legs.forEach(function(leg) { + leg.steps.forEach(function(step) { + if (step.name) { + safeIncrease(priorityTable, step.name); + } + if (step.ref) { + step.ref.split(';').forEach(function(ref) { + safeIncrease(priorityTable, ref.trim()) + }) + } + }); + }); + + console.log(priorityTable); + + return priorityTable; + }, + compile: function(step, priorityTable) { if (!step.maneuver) throw new Error('No step maneuver provided'); var type = step.maneuver.type; @@ -129,6 +157,53 @@ module.exports = function(version, language) { } name = name.replace(' (' + step.ref + ')', ''); + // alter by priority + if (priorityTable) { + // get all scores + collect = []; + if (name) { + if (!priorityTable[name]) throw new Error('name ' + name + ' not in priorityTable'); + collect.push({ + type: 'name', + value: name, + score: priorityTable[name] + }); + } + if (step.ref) { + step.ref.split(';').forEach(function(_ref) { + const ref = _ref.trim(); + if(!priorityTable[ref]) throw new Error('ref ' + ref + ' not in priorityTable'); + collect.push({ + type: 'ref', + value: ref, + score: priorityTable[ref] + }); + }); + } + + if (collect.length > 0) { + // find the highest score + res = collect.reduce(function(a, b) { + return (a.score > b.score) ? a : b; + }, { score: 0 }); + console.log(collect, res); + + if (res) { + // save for later + if (res.type === 'name') { + name = res.value; + ref = null; + } else if (res.type === 'ref') { + name = null; + ref = res.value; + } else { + throw new Error('unknown type ' + res.type + 'in ' + JSON.stringify(res)); + } + } + } + } + + // select if (name && ref && name !== ref) { wayName = name + ' (' + ref + ')'; } else if (!name && ref) { diff --git a/test/index_test.js b/test/index_test.js index 7295e1781..c9f9c5b6d 100644 --- a/test/index_test.js +++ b/test/index_test.js @@ -116,3 +116,68 @@ tape.test('v5 compile', function(t) { assert.end(); }); }); + +tape.only('v5 precompileLookupTable and compile', function(t) { + var v5Instructions = instructions('v5', process.env.LANGUAGE || 'en'); + var lookupTable; + var fixture = { + steps: [ + { + maneuver: { + type: 'depart', + modifier: 'right', + bearing_after: 270 + }, + name: "Chain Bridge Road", + ref: "VA 123" + }, + { + maneuver: { + type: 'turn', + modifier: 'left', + bearing_after: 270 + }, + name: "George Washington Memorial Parkway", + ref: "VA 123" + }, + { + maneuver: { + type: 'arrive', + modifier: 'right', + bearing_after: 270 + }, + name: "Chain Bridge Road", + ref: "VA 123" + } + ] + }; + + t.test('generates expected lookup table', function(assert) { + lookupTable = v5Instructions.precompilePriorityTable(fixture); + + assert.deepEqual(lookupTable, { + "Chain Bridge Road": 2, + "VA 123": 3, + "George Washington Memorial Parkway": 1 + }); + + assert.end(); + }); + + t.test('has expected compile results', function(assert) { + assert.deepEqual(fixture.steps.map((step) => { + return v5Instructions.compile(step, lookupTable); + }), [ + 'Head west on VA 123', + 'Turn left onto VA 123', + 'You have arrived at your destination, on the right' + ]); + + // Previously: + // 'Head west on Chain Bridge Road (VA 123)', + // 'Turn left onto George Washington Memorial Parkway (VA 123)', + // 'You have arrived at your destination, on the right' + + assert.end(); + }); +});