Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use priority for most common/used name ref #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove console


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) {
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();
});
});