0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00

Add jsdoc search.js

This commit is contained in:
Juan Picado 2017-04-26 23:48:55 +02:00
parent 472e8e94b0
commit 7dde31546e
No known key found for this signature in database
GPG key ID: 18AC54485952D158

View file

@ -2,7 +2,14 @@
const lunr = require('lunr');
/**
* Handle the search Indexer.
*/
class Search {
/**
* Constructor.
*/
constructor() {
this.index = lunr(function() {
this.field('name', {boost: 10});
@ -12,6 +19,12 @@ class Search {
});
}
/**
* Performs a query to the indexer.
* If the keyword is a * it returns all local elements
* otherwise performs a search
* @param {*} q the keyword
*/
query(q) {
return q === '*'
? this.storage.config.localList.get().map( function( pkg ) {
@ -19,6 +32,10 @@ class Search {
}) : this.index.search(q);
}
/**
* Add a new element to index
* @param {*} pkg the package
*/
add(pkg) {
this.index.add({
id: pkg.name,
@ -28,10 +45,17 @@ class Search {
});
}
/**
* Remove an element from the index.
* @param {*} name the id element
*/
remove(name) {
this.index.remove({id: name});
}
/**
* Force a reindex.
*/
reindex() {
let self = this;
this.storage.get_local(function(err, packages) {
@ -43,6 +67,10 @@ class Search {
});
}
/**
* Set up the {Storage}
* @param {*} storage An storage reference.
*/
configureStorage(storage) {
this.storage = storage;
this.reindex();