Skip to content

Commit e607947

Browse files
committed
perform basic weighting of results
1 parent e954ad7 commit e607947

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

lib/resources/script.js

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,66 @@ function shiftWindow() {
5959
}
6060

6161
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+
};
6374

6475
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
6977

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+
}
7282

7383
// iterate through the pool of strings and for any string that
7484
// 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));
78109
}
79110
});
80111

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);
82122
};
83123

84124
function initTypeahead() {
@@ -119,7 +159,7 @@ function initSearch() {
119159
var jsonReq = new XMLHttpRequest();
120160
jsonReq.open('GET', 'index.json', true);
121161
jsonReq.addEventListener('load', function() {
122-
index = JSON.parse(jsonReq.responseText);
162+
searchIndex = JSON.parse(jsonReq.responseText);
123163
initTypeahead();
124164
});
125165
jsonReq.send();

0 commit comments

Comments
 (0)