Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
freenerd committed Nov 14, 2016
1 parent 811727e commit a7f6f7e
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
67 changes: 66 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,31 @@ module.exports = function(version, language) {

return config.join('');
},
compile: function(step) {
precompilePriorityTable: function(leg) {
priorityTable = {};

function safeIncrease(obj, key) {
if (obj[key]) {
obj[key] = obj[key] + 1
} else {
obj[key] = 1
}
}

leg.steps.forEach(function(step) {
if (step.name) {
safeIncrease(priorityTable, step.name);
}
if (step.ref) {
step.ref.split(';').forEach(function(ref) {
safeIncrease(priorityTable, ref)
})
}
});

return priorityTable;
},
compile: function(step, lookupTable) {
if (!step.maneuver) throw new Error('No step maneuver provided');

var type = step.maneuver.type;
Expand Down Expand Up @@ -129,6 +153,47 @@ 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) {
if(!priorityTable[ref]) throw new Error('ref ' + ref + ' not in priorityTable');
collect.push({
type: 'ref',
value: ref,
score: priorityTable[ref]
});
});
}

// find the highest score
res = collect.reduce(function(a, b) {
if (a.score < b.score) return b;
}, { score: 0 });

// 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) {
Expand Down
65 changes: 65 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit a7f6f7e

Please sign in to comment.