@@ -59,26 +59,66 @@ function shiftWindow() {
59
59
}
60
60
61
61
function initSearch ( ) {
62
- var index ; // the JSON data
62
+ var searchIndex ; // the JSON data
63
+
64
+ var weights = {
65
+ 'library' : 2 ,
66
+ 'class' : 2 ,
67
+ 'typedef' : 3 ,
68
+ 'method' : 4 ,
69
+ 'accessor' : 4 ,
70
+ 'operator' : 4 ,
71
+ 'property' : 4 ,
72
+ 'constructor' : 4
73
+ } ;
63
74
64
75
function findMatches ( q , cb ) {
65
- var matches , substringRegex ;
66
-
67
- // an array that will be populated with substring matches
68
- matches = [ ] ;
76
+ var allMatches = [ ] ; // ist of matches
69
77
70
- // regex used to determine if a string contains the substring `q`
71
- substrRegex = new RegExp ( q , 'i' ) ;
78
+ function score ( element , num ) {
79
+ var weightFactor = weights [ element . type ] || 4 ;
80
+ return { e : element , score : ( num / weightFactor ) >> 0 } ;
81
+ }
72
82
73
83
// iterate through the pool of strings and for any string that
74
84
// contains the substring `q`, add it to the `matches` array
75
- $ . each ( index , function ( i , element ) {
76
- if ( substrRegex . test ( element . name ) ) {
77
- matches . push ( element ) ;
85
+ $ . each ( searchIndex , function ( i , element ) {
86
+ // TODO: prefer matches in the current library
87
+ // TODO: help prefer a named constructor
88
+
89
+ var lowerName = element . name . toLowerCase ( ) ;
90
+ var lowerQ = q . toLowerCase ( ) ;
91
+
92
+ if ( element . name === q ) {
93
+ // exact match, maximum score
94
+ allMatches . push ( score ( element , 1000 ) ) ;
95
+ } else if ( lowerName === lowerQ ) {
96
+ // case-insensitive exact match
97
+ allMatches . push ( score ( element , 900 ) ) ;
98
+ } else if ( element . name . indexOf ( q ) === 0 ) {
99
+ // starts with
100
+ allMatches . push ( score ( element , 750 ) ) ;
101
+ } else if ( lowerName . indexOf ( lowerQ ) === 0 ) {
102
+ // case-insensitive starts with
103
+ allMatches . push ( score ( element , 650 ) ) ;
104
+ } else if ( element . name . indexOf ( q ) >= 0 ) {
105
+ // contains
106
+ allMatches . push ( score ( element , 500 ) ) ;
107
+ } else if ( lowerName . indexOf ( lowerQ ) >= 0 ) {
108
+ allMatches . push ( score ( element , 400 ) ) ;
78
109
}
79
110
} ) ;
80
111
81
- cb ( matches ) ;
112
+ allMatches . sort ( function ( a , b ) {
113
+ return b . score - a . score ;
114
+ } ) ;
115
+
116
+ var sortedMatches = [ ] ;
117
+ for ( var i = 0 ; i < allMatches . length ; i ++ ) {
118
+ sortedMatches . push ( allMatches [ i ] . e ) ;
119
+ }
120
+
121
+ cb ( sortedMatches ) ;
82
122
} ;
83
123
84
124
function initTypeahead ( ) {
@@ -119,7 +159,7 @@ function initSearch() {
119
159
var jsonReq = new XMLHttpRequest ( ) ;
120
160
jsonReq . open ( 'GET' , 'index.json' , true ) ;
121
161
jsonReq . addEventListener ( 'load' , function ( ) {
122
- index = JSON . parse ( jsonReq . responseText ) ;
162
+ searchIndex = JSON . parse ( jsonReq . responseText ) ;
123
163
initTypeahead ( ) ;
124
164
} ) ;
125
165
jsonReq . send ( ) ;
0 commit comments