@@ -180,23 +180,31 @@ module.exports = function(version, _options) {
180
180
181
181
// Replace tokens
182
182
// 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
194
202
195
203
if ( instructions [ language ] . meta . capitalizeFirstLetter ) {
196
- instruction = this . capitalizeFirstLetter ( instruction ) ;
204
+ return this . capitalizeFirstLetter ( output ) ;
197
205
}
198
206
199
- return instruction ;
207
+ return output ;
200
208
}
201
209
} ;
202
210
} ;
0 commit comments