2014-05-07 17:29:47 +02:00
|
|
|
var lunr = require('lunr')
|
|
|
|
, localList = require('./local-list');
|
|
|
|
|
2014-05-08 18:13:39 +02:00
|
|
|
var Search = function() {
|
2014-05-07 17:29:47 +02:00
|
|
|
this.index = lunr(function () {
|
2014-05-08 18:13:39 +02:00
|
|
|
this.field('name', {boost: 10});
|
|
|
|
this.field('description', {boost: 4});
|
|
|
|
this.field('author', {boost: 6});
|
|
|
|
this.field('readme');
|
2014-05-07 17:29:47 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Search.prototype = {
|
|
|
|
query: function(q) {
|
2014-05-07 20:08:29 +02:00
|
|
|
return this.index.search(q);
|
2014-05-07 17:29:47 +02:00
|
|
|
},
|
2014-05-07 20:08:29 +02:00
|
|
|
add: function(package) {
|
2014-05-08 23:34:16 +02:00
|
|
|
this.index.add({
|
|
|
|
id: package.name,
|
|
|
|
name: package.name,
|
|
|
|
description: package.description,
|
2014-08-08 03:58:25 +02:00
|
|
|
author: package._npmUser ? package._npmUser.name : '???'
|
2014-05-07 17:29:47 +02:00
|
|
|
});
|
2014-05-07 20:08:29 +02:00
|
|
|
},
|
2014-05-08 23:48:15 +02:00
|
|
|
remove: function(name) {
|
|
|
|
this.index.remove({
|
|
|
|
id: name
|
|
|
|
});
|
|
|
|
},
|
2014-05-07 20:08:29 +02:00
|
|
|
reindex: function() {
|
|
|
|
var self = this;
|
|
|
|
this.storage.get_local(function(err, packages) {
|
|
|
|
var i = packages.length;
|
|
|
|
|
|
|
|
while(i--) {
|
|
|
|
self.add(packages[i]);
|
|
|
|
}
|
|
|
|
});
|
2014-05-08 18:13:39 +02:00
|
|
|
},
|
|
|
|
configureStorage: function(storage) {
|
|
|
|
this.storage = storage;
|
|
|
|
this.reindex();
|
2014-05-07 17:29:47 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-08 03:58:25 +02:00
|
|
|
module.exports = new Search();
|