2014-05-07 17:29:47 +02:00
|
|
|
var lunr = require('lunr')
|
|
|
|
, localList = require('./local-list');
|
|
|
|
|
2014-05-07 20:08:29 +02:00
|
|
|
var Search = function(storage) {
|
|
|
|
this.storage = storage;
|
|
|
|
|
2014-05-07 17:29:47 +02:00
|
|
|
this.index = lunr(function () {
|
|
|
|
this.field('name', {boost: 10});
|
|
|
|
this.field('description');
|
|
|
|
this.field('author');
|
|
|
|
});
|
|
|
|
|
2014-05-07 20:08:29 +02:00
|
|
|
this.reindex();
|
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-07 17:29:47 +02:00
|
|
|
this.index.add({
|
2014-05-07 20:08:29 +02:00
|
|
|
id: package.name,
|
|
|
|
name: package.name,
|
2014-05-07 17:29:47 +02:00
|
|
|
description: package.description,
|
|
|
|
author: package._npmUser.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-07 17:29:47 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-07 20:08:29 +02:00
|
|
|
module.exports = Search;
|