Skip to content

Commit 3b5b7db

Browse files
committed
Removed duplicates from search results
1 parent f9d7917 commit 3b5b7db

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

script.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ function initSearchBar() {
109109

110110
document.querySelector('header > input').addEventListener('input', function(event) {
111111
var results = [],
112+
pre_results = {},
112113
first = null,
113114
cursor_position = this.selectionStart,
114115
r = new RegExp('[^\w]' + this.value);
@@ -119,35 +120,42 @@ function initSearchBar() {
119120
for (var i = 0, l = SEARCH.length; i < l; i++) {
120121
var item = SEARCH[i];
121122

122-
if (item[0].indexOf(this.value) === 0) {
123-
results.push(item);
123+
if (item[0].indexOf(this.value) === 0 && !pre_results[key]) {
124+
pre_results[item[0]] = item;
124125
}
125126
}
126127

127128
for (var key in BOOKMARKS) {
128129
if (key.indexOf(this.value) === 0) {
129-
var start_with = key.match(/[^/]+\/\/(www\.)?/)[1];
130-
131-
results.push([
132-
key.replace(start_with, ''),
133-
0,
134-
start_with
135-
]);
130+
var start_with = key.match(/[^/]+\/\/(www\.)?/)[1],
131+
url = key.replace(start_with, '');
132+
133+
if (!pre_results[key]) {
134+
pre_results[url] = [
135+
url,
136+
0,
137+
start_with
138+
];
139+
}
136140
}
137141
}
138142

139143
for (var i = 0; i < TOP_SITES_length; i++) {
140144
var key = TOP_SITES[i];
141145

142-
if (key.indexOf(this.value) === 0) {
143-
results.push([
146+
if (key.indexOf(this.value) === 0 && !pre_results[key]) {
147+
pre_results[key] = [
144148
key,
145149
0,
146150
'https://'
147-
]);
151+
];
148152
}
149153
}
150154

155+
for (var key in pre_results) {
156+
results.push(pre_results[key]);
157+
}
158+
151159
results = sort(results, 1);
152160

153161
results = results.slice(0, 6);

0 commit comments

Comments
 (0)