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

Add handling for name and ref #32

Merged
merged 1 commit into from
Oct 19, 2016
Merged
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Change Log
All notable changes to this project will be documented in this file. For change log formatting, see http://keepachangelog.com/

# Unreleased
## Unreleased

- Better u-turn instructions
- Fix rotary names instruction `exit onto`
- Only mention end-of-road on uturn intructions
- Respect name and ref for way_name

## 0.0.2 2016-10-18

Expand Down
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,24 @@ module.exports = function(_version) {
// NOOP, since no special logic for that type
}

// Decide way_name with special handling for name and ref
var wayName;
var name = (step.name || '').replace(new RegExp(' (' + step.ref + ')$'), ''); // Remove hack from Mapbox Directions mixing ref into name
var ref = (step.ref || '').split(';')[0];
if (name && ref && name !== ref) {
wayName = name + ' (' + ref + ')';
} else if (!name && ref) {
wayName = ref;
} else {
wayName = name;
}

// Decide which instruction string to use
// Destination takes precedence over name
var instruction;
if (step.destinations && instructionObject.destination) {
instruction = instructionObject.destination;
} else if (step.name && instructionObject.name) {
} else if (wayName && instructionObject.name) {
instruction = instructionObject.name;
} else {
instruction = instructionObject.default;
Expand All @@ -126,14 +138,14 @@ module.exports = function(_version) {
// NOOP if they don't exist
var nthWaypoint = ''; // TODO, add correct waypoint counting
instruction = instruction
.replace('{nth}', nthWaypoint)
.replace('{way_name}', wayName)
.replace('{destination}', (step.destinations || '').split(',')[0])
.replace('{exit_number}', this.ordinalize(step.maneuver.exit || 1))
.replace('{rotary_name}', step.rotary_name)
.replace('{lane_instruction}', laneInstruction)
.replace('{modifier}', instructions[version].constants.modifier[modifier])
.replace('{direction}', this.directionFromDegree(step.maneuver.bearing_after))
.replace('{way_name}', step.name)
.replace('{nth}', nthWaypoint)
.replace(/ {2}/g, ' '); // remove excess spaces

return instruction;
Expand Down
52 changes: 51 additions & 1 deletion scripts/generate_fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ var path = require('path');

var instructions = require('../index.js');
var v5Instructions = instructions('v5');
var constants = require('../test/constants');
var type = process.argv[2];

function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}

function underscorify(input) {
return input.replace(/ /g, '_');
}

function write(step, p) {
fs.writeFileSync(p, JSON.stringify(step, null, 4));
fs.writeFileSync(p, JSON.stringify(step, null, 4) + '\n');
}

function writeVariations(basePath, baseStep, onlyDefault) {
Expand Down Expand Up @@ -50,6 +55,51 @@ function writeVariations(basePath, baseStep, onlyDefault) {

function execute() {
switch (type) {
case 'turn':
var basePath = path.join(__dirname, '..', 'test', 'fixtures', 'v5', 'turn');

// do name_ref combinations
var baseStep = {
maneuver: {
type: 'turn',
modifier: 'left'
},
name: '',
ref: 'Ref1;Ref2'
};
writeVariations(path.join(basePath, 'left_ref'), baseStep, true);
baseStep = {
maneuver: {
type: 'turn',
modifier: 'left'
},
name: 'Way Name',
ref: 'Ref1;Ref2'
};
writeVariations(path.join(basePath, 'left_ref_name'), baseStep, true);
baseStep = {
maneuver: {
type: 'turn',
modifier: 'left'
},
name: 'Way Name',
ref: 'Ref1;Ref2',
destinations: 'Destination 1,Destination 2'
};
writeVariations(path.join(basePath, 'left_destination_ref_name'), baseStep, true);

// do variation per modifier
constants.modifiers.forEach((modifier) => {
baseStep = {
maneuver: {
type: 'turn',
modifier: modifier
},
name: ''
};
writeVariations(path.join(basePath, underscorify(modifier)), baseStep);
});
break;
case 'rotary':
// default
var basePath = path.join(__dirname, '..', 'test', 'fixtures', 'v5', 'rotary', 'default');
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/v5/turn/left_destination.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"step": {
"maneuver": {
"type": "turn",
"modifier": "left"
},
"name": "Way Name",
"destinations": "Destination 1,Destination 2"
},
"instruction": "Turn left towards Destination 1"
}
12 changes: 12 additions & 0 deletions test/fixtures/v5/turn/left_destination_ref_name_default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"step": {
"maneuver": {
"type": "turn",
"modifier": "left"
},
"name": "Way Name",
"ref": "Ref1;Ref2",
"destinations": "Destination 1,Destination 2"
},
"instruction": "Turn left towards Destination 1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "turn",
"modifier": "left"
},
"name": "Street Name"
"name": "Way Name"
},
"instruction": "Turn left onto Street Name"
"instruction": "Turn left onto Way Name"
}
11 changes: 11 additions & 0 deletions test/fixtures/v5/turn/left_ref_default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"step": {
"maneuver": {
"type": "turn",
"modifier": "left"
},
"name": "",
"ref": "Ref1;Ref2"
},
"instruction": "Turn left onto Ref1"
Copy link
Member Author

Choose a reason for hiding this comment

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

@bsudekum @1ec5 @willwhite @karenzshea highlighting here that I've changed the behaviour to only include the first ref, instead of all. This is a debatable decision, but I value conciseness highly here, thus one ref should be sufficient. Choosing the best ref should be smarter though, e.g. via #30.

}
11 changes: 11 additions & 0 deletions test/fixtures/v5/turn/left_ref_name_default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"step": {
"maneuver": {
"type": "turn",
"modifier": "left"
},
"name": "Way Name",
"ref": "Ref1;Ref2"
},
"instruction": "Turn left onto Way Name (Ref1)"
}
11 changes: 11 additions & 0 deletions test/fixtures/v5/turn/right_destination.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"step": {
"maneuver": {
"type": "turn",
"modifier": "right"
},
"name": "Way Name",
"destinations": "Destination 1,Destination 2"
},
"instruction": "Turn right towards Destination 1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "turn",
"modifier": "right"
},
"name": "Street Name"
"name": "Way Name"
},
"instruction": "Turn right onto Street Name"
"instruction": "Turn right onto Way Name"
}
11 changes: 11 additions & 0 deletions test/fixtures/v5/turn/sharp_left_destination.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"step": {
"maneuver": {
"type": "turn",
"modifier": "sharp left"
},
"name": "Way Name",
"destinations": "Destination 1,Destination 2"
},
"instruction": "Make a sharp left towards Destination 1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "turn",
"modifier": "sharp left"
},
"name": "Street Name"
"name": "Way Name"
},
"instruction": "Make a sharp left onto Street Name"
"instruction": "Make a sharp left onto Way Name"
}
11 changes: 11 additions & 0 deletions test/fixtures/v5/turn/sharp_right_destination.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"step": {
"maneuver": {
"type": "turn",
"modifier": "sharp right"
},
"name": "Way Name",
"destinations": "Destination 1,Destination 2"
},
"instruction": "Make a sharp right towards Destination 1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "turn",
"modifier": "sharp right"
},
"name": "Street Name"
"name": "Way Name"
},
"instruction": "Make a sharp right onto Street Name"
"instruction": "Make a sharp right onto Way Name"
}
11 changes: 11 additions & 0 deletions test/fixtures/v5/turn/slight_left_destination.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"step": {
"maneuver": {
"type": "turn",
"modifier": "slight left"
},
"name": "Way Name",
"destinations": "Destination 1,Destination 2"
},
"instruction": "Make a slight left towards Destination 1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "turn",
"modifier": "slight left"
},
"name": "Street Name"
"name": "Way Name"
},
"instruction": "Make a slight left onto Street Name"
"instruction": "Make a slight left onto Way Name"
}
11 changes: 11 additions & 0 deletions test/fixtures/v5/turn/slight_right_destination.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"step": {
"maneuver": {
"type": "turn",
"modifier": "slight right"
},
"name": "Way Name",
"destinations": "Destination 1,Destination 2"
},
"instruction": "Make a slight right towards Destination 1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "turn",
"modifier": "slight right"
},
"name": "Street Name"
"name": "Way Name"
},
"instruction": "Make a slight right onto Street Name"
"instruction": "Make a slight right onto Way Name"
}
11 changes: 11 additions & 0 deletions test/fixtures/v5/turn/straight_destination.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"step": {
"maneuver": {
"type": "turn",
"modifier": "straight"
},
"name": "Way Name",
"destinations": "Destination 1,Destination 2"
},
"instruction": "Go straight towards Destination 1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "turn",
"modifier": "straight"
},
"name": "Street Name"
"name": "Way Name"
},
"instruction": "Go straight onto Street Name"
"instruction": "Go straight onto Way Name"
}
11 changes: 11 additions & 0 deletions test/fixtures/v5/turn/uturn_destination.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"step": {
"maneuver": {
"type": "turn",
"modifier": "uturn"
},
"name": "Way Name",
"destinations": "Destination 1,Destination 2"
},
"instruction": "Make a U-turn towards Destination 1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "turn",
"modifier": "uturn"
},
"name": "Street Name"
"name": "Way Name"
},
"instruction": "Make a U-turn onto Street Name"
"instruction": "Make a U-turn onto Way Name"
}
33 changes: 32 additions & 1 deletion test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ tape.test('v5 compile', function(t) {
}

function checkModifiers(type) {
constants.modifiers.forEach(function(modifier) {
assert.ok(
fs.existsSync(path.join(basePath, underscorify(type), `${underscorify(modifier)}_default.json`)),
`${type}/${modifier}_default`);
assert.ok(
fs.existsSync(path.join(basePath, underscorify(type), `${underscorify(modifier)}_destination.json`)),
`${type}/${modifier}_destination`);
assert.ok(
fs.existsSync(path.join(basePath, underscorify(type), `${underscorify(modifier)}_name.json`)),
`${type}/${modifier}_name`);
});
}

function checkModifiersNoName(type) {
// TODO: Remove this function and replace it complately by checkModifiers
constants.modifiers.forEach(function(modifier) {
// check normal fixture
var p = path.join(basePath, underscorify(type), underscorify(modifier) + '.json');
Expand All @@ -118,6 +133,22 @@ tape.test('v5 compile', function(t) {

constants.types.forEach(function(type) {
switch(type) {
case 'turn':
// name_ref combinations
assert.ok(
fs.existsSync(path.join(basePath, 'turn', `left_ref_default.json`)),
`${type}/left_ref_default`);
assert.ok(
fs.existsSync(path.join(basePath, 'turn', `left_ref_default.json`)),
`${type}/left_ref_name`);
assert.ok(
fs.existsSync(path.join(basePath, 'turn', `left_ref_default.json`)),
`${type}/left_destination_ref_name`);

// regular combinations
checkModifiers(type);

break;
case 'rotary':
[ 'default', 'exit_1', 'name', 'name_exit' ].forEach((s) => {
assert.ok(
Expand Down Expand Up @@ -152,7 +183,7 @@ tape.test('v5 compile', function(t) {
});
break;
default:
checkModifiers(type);
checkModifiersNoName(type);
break
};
});
Expand Down