2014-05-07 17:29:47 +02:00
|
|
|
var lunr = require('lunr')
|
|
|
|
|
2014-11-12 12:14:37 +01:00
|
|
|
function Search() {
|
|
|
|
var self = Object.create(Search.prototype)
|
|
|
|
self.index = lunr(function() {
|
|
|
|
this.field('name' , { boost: 10 })
|
|
|
|
this.field('description' , { boost: 4 })
|
|
|
|
this.field('author' , { boost: 6 })
|
|
|
|
this.field('readme')
|
|
|
|
})
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
Search.prototype.query = function(q) {
|
2017-04-12 02:17:24 +02:00
|
|
|
return q === '*'
|
2017-04-09 16:05:31 +02:00
|
|
|
? this.storage.config.localList.get().map( function( package ){ return { ref: package, score: 1 }; } )
|
|
|
|
: this.index.search(q);
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Search.prototype.add = function(package) {
|
|
|
|
this.index.add({
|
|
|
|
id: package.name,
|
|
|
|
name: package.name,
|
|
|
|
description: package.description,
|
|
|
|
author: package._npmUser ? package._npmUser.name : '???',
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
Search.prototype.remove = function(name) {
|
|
|
|
this.index.remove({ id: name })
|
|
|
|
}
|
|
|
|
|
|
|
|
Search.prototype.reindex = function() {
|
|
|
|
var self = this
|
|
|
|
this.storage.get_local(function(err, packages) {
|
|
|
|
if (err) throw err // that function shouldn't produce any
|
|
|
|
var i = packages.length
|
|
|
|
while (i--) {
|
|
|
|
self.add(packages[i])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
Search.prototype.configureStorage = function(storage) {
|
|
|
|
this.storage = storage
|
|
|
|
this.reindex()
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Search()
|