@@ -60,7 +60,35 @@ module.exports = function(version, language) {
60
60
61
61
return config . join ( '' ) ;
62
62
} ,
63
- compile : function ( step ) {
63
+ precompilePriorityTable : function ( route ) {
64
+ priorityTable = { } ;
65
+
66
+ function safeIncrease ( obj , key ) {
67
+ if ( obj [ key ] ) {
68
+ obj [ key ] = obj [ key ] + 1
69
+ } else {
70
+ obj [ key ] = 1
71
+ }
72
+ }
73
+
74
+ route . legs . forEach ( function ( leg ) {
75
+ leg . steps . forEach ( function ( step ) {
76
+ if ( step . name ) {
77
+ safeIncrease ( priorityTable , step . name ) ;
78
+ }
79
+ if ( step . ref ) {
80
+ step . ref . split ( ';' ) . forEach ( function ( ref ) {
81
+ safeIncrease ( priorityTable , ref . trim ( ) )
82
+ } )
83
+ }
84
+ } ) ;
85
+ } ) ;
86
+
87
+ console . log ( priorityTable ) ;
88
+
89
+ return priorityTable ;
90
+ } ,
91
+ compile : function ( step , priorityTable ) {
64
92
if ( ! step . maneuver ) throw new Error ( 'No step maneuver provided' ) ;
65
93
66
94
var type = step . maneuver . type ;
@@ -129,6 +157,53 @@ module.exports = function(version, language) {
129
157
}
130
158
name = name . replace ( ' (' + step . ref + ')' , '' ) ;
131
159
160
+ // alter by priority
161
+ if ( priorityTable ) {
162
+ // get all scores
163
+ collect = [ ] ;
164
+ if ( name ) {
165
+ if ( ! priorityTable [ name ] ) throw new Error ( 'name ' + name + ' not in priorityTable' ) ;
166
+ collect . push ( {
167
+ type : 'name' ,
168
+ value : name ,
169
+ score : priorityTable [ name ]
170
+ } ) ;
171
+ }
172
+ if ( step . ref ) {
173
+ step . ref . split ( ';' ) . forEach ( function ( _ref ) {
174
+ const ref = _ref . trim ( ) ;
175
+ if ( ! priorityTable [ ref ] ) throw new Error ( 'ref ' + ref + ' not in priorityTable' ) ;
176
+ collect . push ( {
177
+ type : 'ref' ,
178
+ value : ref ,
179
+ score : priorityTable [ ref ]
180
+ } ) ;
181
+ } ) ;
182
+ }
183
+
184
+ if ( collect . length > 0 ) {
185
+ // find the highest score
186
+ res = collect . reduce ( function ( a , b ) {
187
+ return ( a . score > b . score ) ? a : b ;
188
+ } , { score : 0 } ) ;
189
+ console . log ( collect , res ) ;
190
+
191
+ if ( res ) {
192
+ // save for later
193
+ if ( res . type === 'name' ) {
194
+ name = res . value ;
195
+ ref = null ;
196
+ } else if ( res . type === 'ref' ) {
197
+ name = null ;
198
+ ref = res . value ;
199
+ } else {
200
+ throw new Error ( 'unknown type ' + res . type + 'in ' + JSON . stringify ( res ) ) ;
201
+ }
202
+ }
203
+ }
204
+ }
205
+
206
+ // select
132
207
if ( name && ref && name !== ref ) {
133
208
wayName = name + ' (' + ref + ')' ;
134
209
} else if ( ! name && ref ) {
0 commit comments