Skip to content

Commit 3e0bc82

Browse files
authored
Merge pull request #167 from Project-OSRM/get-wayname
expose getWayName from compile for other use cases
2 parents 3e701e9 + 871ca4a commit 3e0bc82

File tree

3 files changed

+87
-34
lines changed

3 files changed

+87
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. For change
44
## master
55

66
- Added grammatical cases support for Russian way names [#102](https://github.com/Project-OSRM/osrm-text-instructions/pull/102)
7+
- Adds `osrmti.getWayName` to the public api. You MUST NOT change the result before passing it into `osrmti.tokenize`. [#167](https://github.com/Project-OSRM/osrm-text-instructions/pull/167)
78

89
## 0.7.1 2017-09-26
910

index.js

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,45 @@ module.exports = function(version, _options) {
6969

7070
return config.join('');
7171
},
72+
getWayName: function(language, step, options) {
73+
var classes = options ? options.classes || [] : [];
74+
if (typeof step !== 'object') throw new Error('step must be an Object');
75+
if (!Array.isArray(classes)) throw new Error('classes must be an Array or undefined');
76+
77+
var wayName;
78+
var name = step.name || '';
79+
var ref = (step.ref || '').split(';')[0];
80+
81+
// Remove hacks from Mapbox Directions mixing ref into name
82+
if (name === step.ref) {
83+
// if both are the same we assume that there used to be an empty name, with the ref being filled in for it
84+
// we only need to retain the ref then
85+
name = '';
86+
}
87+
name = name.replace(' (' + step.ref + ')', '');
88+
89+
// In attempt to avoid using the highway name of a way,
90+
// check and see if the step has a class which should signal
91+
// the ref should be used instead of the name.
92+
var wayMotorway = classes.indexOf('motorway') !== -1;
93+
94+
if (name && ref && name !== ref && !wayMotorway) {
95+
var phrase = instructions[language][version].phrase['name and ref'] ||
96+
instructions.en[version].phrase['name and ref'];
97+
wayName = this.tokenize(language, phrase, {
98+
name: name,
99+
ref: ref
100+
});
101+
} else if (name && ref && wayMotorway && (/\d/).test(ref)) {
102+
wayName = ref;
103+
} else if (!name && ref) {
104+
wayName = ref;
105+
} else {
106+
wayName = name;
107+
}
108+
109+
return wayName;
110+
},
72111
compile: function(language, step, options) {
73112
if (!language) throw new Error('No language code provided');
74113
if (languages.supportedCodes.indexOf(language) === -1) throw new Error('language code ' + language + ' not loaded');
@@ -127,40 +166,7 @@ module.exports = function(version, _options) {
127166
}
128167

129168
// Decide way_name with special handling for name and ref
130-
var wayName;
131-
var name = step.name || '';
132-
var ref = (step.ref || '').split(';')[0];
133-
134-
// Remove hacks from Mapbox Directions mixing ref into name
135-
if (name === step.ref) {
136-
// if both are the same we assume that there used to be an empty name, with the ref being filled in for it
137-
// we only need to retain the ref then
138-
name = '';
139-
}
140-
name = name.replace(' (' + step.ref + ')', '');
141-
142-
// In attempt to avoid using the highway name of a way,
143-
// check and see if the step has a class which should signal
144-
// the ref should be used instead of the name.
145-
var wayMotorway = false;
146-
if (options && options.classes) {
147-
wayMotorway = options.classes.indexOf('motorway') !== -1;
148-
}
149-
150-
if (name && ref && name !== ref && !wayMotorway) {
151-
var phrase = instructions[language][version].phrase['name and ref'] ||
152-
instructions.en[version].phrase['name and ref'];
153-
wayName = this.tokenize(language, phrase, {
154-
name: name,
155-
ref: ref
156-
});
157-
} else if (name && ref && wayMotorway && (/\d/).test(ref)) {
158-
wayName = ref;
159-
} else if (!name && ref) {
160-
wayName = ref;
161-
} else {
162-
wayName = name;
163-
}
169+
var wayName = this.getWayName(language, step, options);
164170

165171
// Decide which instruction string to use
166172
// Destination takes precedence over name

test/index_test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,52 @@ tape.test('v5 directionFromDegree', function(assert) {
8383
assert.end();
8484
});
8585

86+
tape.test('v5 wayName', function(assert) {
87+
var v5Compiler = compiler('v5');
88+
89+
function makeStep(ref, name) {
90+
return {
91+
ref: ref,
92+
name: name
93+
};
94+
}
95+
96+
[
97+
[makeStep('', ''), ''],
98+
[makeStep('ref', 'name'), 'Name (ref)'],
99+
[makeStep('ref', 'name (ref)'), 'Name (ref)'],
100+
[makeStep('ref; other', 'name (ref; other)'), 'Name (ref)'],
101+
[makeStep('ref1; ref2', 'name'), 'Name (ref1)'],
102+
[makeStep('ref1; ref2', 'name (ref1; ref2)'), 'Name (ref1)']
103+
].forEach((c, i) => {
104+
assert.equal(v5Compiler.getWayName('en', c[0]), c[1], `correct way name for non motorway test ${i}`);
105+
});
106+
107+
[
108+
[makeStep('', ''), ''],
109+
[makeStep('ref', 'name'), 'name'],
110+
[makeStep('ref1', 'name'), 'ref1'],
111+
[makeStep('ref', 'name (ref)'), 'name'],
112+
[makeStep('ref1', 'name (ref1)'), 'ref1'],
113+
[makeStep('ref1; ref2', 'name'), 'ref1'],
114+
[makeStep('ref1; ref2', 'name (ref1; ref2)'), 'ref1']
115+
].forEach((c, i) => {
116+
assert.equal(v5Compiler.getWayName('en', c[0], {classes: ['motorway']}), c[1], `correct way name for motorway test ${i}`);
117+
});
118+
119+
assert.throws(
120+
() => { v5Compiler.getWayName(); },
121+
'throws when no step is passed'
122+
);
123+
124+
assert.throws(
125+
() => { v5Compiler.getWayName({}, 1); },
126+
'throws when classes is invalid'
127+
);
128+
129+
assert.end();
130+
});
131+
86132
tape.test('v5 laneDiagram', function(assert) {
87133
var v5Compiler = compiler('v5');
88134

0 commit comments

Comments
 (0)