Skip to content

Commit 9b0d75e

Browse files
committed
WIP
1 parent 811727e commit 9b0d75e

File tree

2 files changed

+141
-1
lines changed

2 files changed

+141
-1
lines changed

index.js

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,35 @@ module.exports = function(version, language) {
6060

6161
return config.join('');
6262
},
63-
compile: function(step) {
63+
precompilePriorityTable: function(route) {
64+
priorityTable = {};
65+
66+
function safeIncrease(obj, key) {
67+
if (obj[key]) {
68+
obj[key] = obj[key] + 1
69+
} else {
70+
obj[key] = 1
71+
}
72+
}
73+
74+
route.legs.forEach(function(leg) {
75+
leg.steps.forEach(function(step) {
76+
if (step.name) {
77+
safeIncrease(priorityTable, step.name);
78+
}
79+
if (step.ref) {
80+
step.ref.split(';').forEach(function(ref) {
81+
safeIncrease(priorityTable, ref.trim())
82+
})
83+
}
84+
});
85+
});
86+
87+
console.log(priorityTable);
88+
89+
return priorityTable;
90+
},
91+
compile: function(step, priorityTable) {
6492
if (!step.maneuver) throw new Error('No step maneuver provided');
6593

6694
var type = step.maneuver.type;
@@ -129,6 +157,53 @@ module.exports = function(version, language) {
129157
}
130158
name = name.replace(' (' + step.ref + ')', '');
131159

160+
// alter by priority
161+
if (priorityTable) {
162+
// get all scores
163+
collect = [];
164+
if (name) {
165+
if (!priorityTable[name]) throw new Error('name ' + name + ' not in priorityTable');
166+
collect.push({
167+
type: 'name',
168+
value: name,
169+
score: priorityTable[name]
170+
});
171+
}
172+
if (step.ref) {
173+
step.ref.split(';').forEach(function(_ref) {
174+
const ref = _ref.trim();
175+
if(!priorityTable[ref]) throw new Error('ref ' + ref + ' not in priorityTable');
176+
collect.push({
177+
type: 'ref',
178+
value: ref,
179+
score: priorityTable[ref]
180+
});
181+
});
182+
}
183+
184+
if (collect.length > 0) {
185+
// find the highest score
186+
res = collect.reduce(function(a, b) {
187+
return (a.score > b.score) ? a : b;
188+
}, { score: 0 });
189+
console.log(collect, res);
190+
191+
if (res) {
192+
// save for later
193+
if (res.type === 'name') {
194+
name = res.value;
195+
ref = null;
196+
} else if (res.type === 'ref') {
197+
name = null;
198+
ref = res.value;
199+
} else {
200+
throw new Error('unknown type ' + res.type + 'in ' + JSON.stringify(res));
201+
}
202+
}
203+
}
204+
}
205+
206+
// select
132207
if (name && ref && name !== ref) {
133208
wayName = name + ' (' + ref + ')';
134209
} else if (!name && ref) {

test/index_test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,68 @@ tape.test('v5 compile', function(t) {
116116
assert.end();
117117
});
118118
});
119+
120+
tape.only('v5 precompileLookupTable and compile', function(t) {
121+
var v5Instructions = instructions('v5', process.env.LANGUAGE || 'en');
122+
var lookupTable;
123+
var fixture = {
124+
steps: [
125+
{
126+
maneuver: {
127+
type: 'depart',
128+
modifier: 'right',
129+
bearing_after: 270
130+
},
131+
name: "Chain Bridge Road",
132+
ref: "VA 123"
133+
},
134+
{
135+
maneuver: {
136+
type: 'turn',
137+
modifier: 'left',
138+
bearing_after: 270
139+
},
140+
name: "George Washington Memorial Parkway",
141+
ref: "VA 123"
142+
},
143+
{
144+
maneuver: {
145+
type: 'arrive',
146+
modifier: 'right',
147+
bearing_after: 270
148+
},
149+
name: "Chain Bridge Road",
150+
ref: "VA 123"
151+
}
152+
]
153+
};
154+
155+
t.test('generates expected lookup table', function(assert) {
156+
lookupTable = v5Instructions.precompilePriorityTable(fixture);
157+
158+
assert.deepEqual(lookupTable, {
159+
"Chain Bridge Road": 2,
160+
"VA 123": 3,
161+
"George Washington Memorial Parkway": 1
162+
});
163+
164+
assert.end();
165+
});
166+
167+
t.test('has expected compile results', function(assert) {
168+
assert.deepEqual(fixture.steps.map((step) => {
169+
return v5Instructions.compile(step, lookupTable);
170+
}), [
171+
'Head west on VA 123',
172+
'Turn left onto VA 123',
173+
'You have arrived at your destination, on the right'
174+
]);
175+
176+
// Previously:
177+
// 'Head west on Chain Bridge Road (VA 123)',
178+
// 'Turn left onto George Washington Memorial Parkway (VA 123)',
179+
// 'You have arrived at your destination, on the right'
180+
181+
assert.end();
182+
});
183+
});

0 commit comments

Comments
 (0)