mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-01-20 22:52:46 -05:00
Migrate Search to class
This commit is contained in:
parent
b5acc054bf
commit
e6561e10d4
1 changed files with 58 additions and 46 deletions
|
@ -1,36 +1,47 @@
|
|||
var lunr = require('lunr')
|
||||
"use strict";
|
||||
|
||||
function Search() {
|
||||
var self = Object.create(Search.prototype)
|
||||
self.index = lunr(function() {
|
||||
const lunr = require('lunr')
|
||||
|
||||
class Search {
|
||||
constructor() {
|
||||
this.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) {
|
||||
query(q) {
|
||||
return q === '*'
|
||||
? this.storage.config.localList.get().map( function( package ){ return { ref: package, score: 1 }; } )
|
||||
: this.index.search(q);
|
||||
}
|
||||
? this.storage.config.localList.get().map( function( pkg ) {
|
||||
return { ref: pkg, score: 1 };
|
||||
}) : this.index.search(q);
|
||||
}
|
||||
|
||||
Search.prototype.add = function(package) {
|
||||
add(pkg) {
|
||||
this.index.add({
|
||||
id: package.name,
|
||||
name: package.name,
|
||||
description: package.description,
|
||||
author: package._npmUser ? package._npmUser.name : '???',
|
||||
id: pkg.name,
|
||||
name: pkg.name,
|
||||
description: pkg.description,
|
||||
author: pkg._npmUser ? pkg._npmUser.name : '???',
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
Search.prototype.remove = function(name) {
|
||||
add(pkg) {
|
||||
this.index.add({
|
||||
id: pkg.name,
|
||||
name: pkg.name,
|
||||
description: pkg.description,
|
||||
author: pkg._npmUser ? pkg._npmUser.name : '???',
|
||||
})
|
||||
}
|
||||
|
||||
remove(name) {
|
||||
this.index.remove({ id: name })
|
||||
}
|
||||
}
|
||||
|
||||
Search.prototype.reindex = function() {
|
||||
reindex() {
|
||||
var self = this
|
||||
this.storage.get_local(function(err, packages) {
|
||||
if (err) throw err // that function shouldn't produce any
|
||||
|
@ -39,11 +50,12 @@ Search.prototype.reindex = function() {
|
|||
self.add(packages[i])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Search.prototype.configureStorage = function(storage) {
|
||||
configureStorage(storage) {
|
||||
this.storage = storage
|
||||
this.reindex()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Search()
|
||||
module.exports = new Search();
|
||||
|
|
Loading…
Add table
Reference in a new issue