Skip to content

Commit a7f6f7e

Browse files
committed
WIP
1 parent 811727e commit a7f6f7e

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

index.js

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

6161
return config.join('');
6262
},
63-
compile: function(step) {
63+
precompilePriorityTable: function(leg) {
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+
leg.steps.forEach(function(step) {
75+
if (step.name) {
76+
safeIncrease(priorityTable, step.name);
77+
}
78+
if (step.ref) {
79+
step.ref.split(';').forEach(function(ref) {
80+
safeIncrease(priorityTable, ref)
81+
})
82+
}
83+
});
84+
85+
return priorityTable;
86+
},
87+
compile: function(step, lookupTable) {
6488
if (!step.maneuver) throw new Error('No step maneuver provided');
6589

6690
var type = step.maneuver.type;
@@ -129,6 +153,47 @@ module.exports = function(version, language) {
129153
}
130154
name = name.replace(' (' + step.ref + ')', '');
131155

156+
// alter by priority
157+
if (priorityTable) {
158+
// get all scores
159+
collect = [];
160+
if (name) {
161+
if (!priorityTable[name]) throw new Error('name ' + name + ' not in priorityTable');
162+
collect.push({
163+
type: 'name',
164+
value: name,
165+
score: priorityTable[name]
166+
});
167+
}
168+
if (step.ref) {
169+
step.ref.split(';').forEach(function(ref) {
170+
if(!priorityTable[ref]) throw new Error('ref ' + ref + ' not in priorityTable');
171+
collect.push({
172+
type: 'ref',
173+
value: ref,
174+
score: priorityTable[ref]
175+
});
176+
});
177+
}
178+
179+
// find the highest score
180+
res = collect.reduce(function(a, b) {
181+
if (a.score < b.score) return b;
182+
}, { score: 0 });
183+
184+
// save for later
185+
if (res.type === 'name') {
186+
name = res.value;
187+
ref = null;
188+
} else if (res.type === 'ref') {
189+
name = null;
190+
ref = res.value;
191+
} else {
192+
throw new Error('unknown type ' + res.type + 'in ' + JSON.stringify(res));
193+
}
194+
}
195+
196+
// select
132197
if (name && ref && name !== ref) {
133198
wayName = name + ' (' + ref + ')';
134199
} 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)