Skip to content

Commit 708583d

Browse files
committed
move tokenizer to top level api
1 parent 7485f85 commit 708583d

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

index.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,31 @@ module.exports = function(version, _options) {
180180

181181
// Replace tokens
182182
// NOOP if they don't exist
183-
instruction = instruction
184-
.replace('{way_name}', wayName)
185-
.replace('{destination}', (step.destinations || '').split(',')[0])
186-
.replace('{exit}', (step.exits || '').split(';')[0])
187-
.replace('{exit_number}', this.ordinalize(language, step.maneuver.exit || 1))
188-
.replace('{rotary_name}', step.rotary_name)
189-
.replace('{lane_instruction}', laneInstruction)
190-
.replace('{modifier}', instructions[language][version].constants.modifier[modifier])
191-
.replace('{direction}', this.directionFromDegree(language, step.maneuver.bearing_after))
192-
.replace('{nth}', nthWaypoint)
193-
.replace(/ {2}/g, ' '); // remove excess spaces
183+
var replaceTokens = {
184+
'way_name': wayName,
185+
'destination': (step.destinations || '').split(',')[0],
186+
'exit': (step.exits || '').split(';')[0],
187+
'exit_number': this.ordinalize(language, step.maneuver.exit || 1),
188+
'rotary_name': step.rotary_name,
189+
'lane_instruction': laneInstruction,
190+
'modifier': instructions[language][version].constants.modifier[modifier],
191+
'direction': this.directionFromDegree(language, step.maneuver.bearing_after),
192+
'nth': nthWaypoint
193+
};
194+
195+
return this.tokenize(instruction, replaceTokens, language);
196+
},
197+
tokenize: function(instruction, tokens, language) {
198+
var output = Object.keys(tokens).reduce(function(memo, token) {
199+
return memo.replace('{' + token + '}', tokens[token]);
200+
}, instruction)
201+
.replace(/ {2}/g, ' '); // remove excess spaces
194202

195203
if (instructions[language].meta.capitalizeFirstLetter) {
196-
instruction = this.capitalizeFirstLetter(instruction);
204+
return this.capitalizeFirstLetter(output);
197205
}
198206

199-
return instruction;
207+
return output;
200208
}
201209
};
202210
};

0 commit comments

Comments
 (0)