@@ -23,6 +23,10 @@ interface MinifiedSearchRecord {
23
23
d : number ;
24
24
}
25
25
26
+ interface Bloodhound < T > {
27
+ local : T [ ] ;
28
+ }
29
+
26
30
function typeSearch ( el : HTMLInputElement ) {
27
31
const jqueryEl = $ ( el ) ;
28
32
const opts : Twitter . Typeahead . Options = {
@@ -62,6 +66,11 @@ function typeSearch(el: HTMLInputElement) {
62
66
if ( ai ) {
63
67
ai . trackEvent ( 'navigate' , { target : value } ) ;
64
68
}
69
+ // Navigate only if the selected string is a valid package, else return.
70
+ const result = source . local . some ( ( e : MinifiedSearchRecord ) => e . t === value ) ;
71
+ if ( ! result ) {
72
+ return ;
73
+ }
65
74
window . location . href = `https://www.npmjs.org/package/@types/${ value } ` ;
66
75
}
67
76
@@ -83,7 +92,8 @@ function typeSearch(el: HTMLInputElement) {
83
92
}
84
93
}
85
94
86
- function createDataSource ( ) {
95
+ function createDataSource ( ) : Bloodhound < MinifiedSearchRecord > {
96
+ let query = "" ;
87
97
const local = JSON . parse ( window . localStorage . getItem ( localStorageDataKey ) ) || undefined ;
88
98
89
99
const bh = new Bloodhound ( {
@@ -92,6 +102,7 @@ function typeSearch(el: HTMLInputElement) {
92
102
return [ entry . l , entry . p , entry . t ] . concat ( entry . g ) . concat ( entry . m ) ;
93
103
} ,
94
104
queryTokenizer : ( input : string ) => {
105
+ query = input ;
95
106
return [ input ] ;
96
107
} ,
97
108
identify : ( e : MinifiedSearchRecord ) => < any > e . t ,
@@ -101,7 +112,16 @@ function typeSearch(el: HTMLInputElement) {
101
112
} ,
102
113
sorter : ( x : MinifiedSearchRecord , y : MinifiedSearchRecord ) => {
103
114
// TODO: Include edit distance as additional weighting factor
104
- return y . d - x . d ;
115
+ // Direct matches should be ranked higher, else rank on basis of download count
116
+ if ( x . t === query || x . t === ( query + "js" ) || x . t === ( query + ".js" ) || x . t === ( query + "-js" ) ) {
117
+ return - 1 ;
118
+ }
119
+ else if ( y . t === query || y . t === ( query + "js" ) || y . t === ( query + ".js" ) || y . t === ( query + "-js" ) ) {
120
+ return 1 ;
121
+ }
122
+ else {
123
+ return y . d - x . d ;
124
+ }
105
125
} ,
106
126
local
107
127
} ) ;
0 commit comments