0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-20 22:52:46 -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'); const lunr = require('lunr');
/**
* Handle the search Indexer.
*/
class Search { class Search {
/**
* Constructor.
*/
constructor() { constructor() {
this.index = lunr(function() { this.index = lunr(function() {
this.field('name', {boost: 10}); 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) { query(q) {
return q === '*' return q === '*'
? this.storage.config.localList.get().map( function( pkg ) { ? this.storage.config.localList.get().map( function( pkg ) {
@ -19,6 +32,10 @@ class Search {
}) : this.index.search(q); }) : this.index.search(q);
} }
/**
* Add a new element to index
* @param {*} pkg the package
*/
add(pkg) { add(pkg) {
this.index.add({ this.index.add({
id: pkg.name, id: pkg.name,
@ -28,10 +45,17 @@ class Search {
}); });
} }
/**
* Remove an element from the index.
* @param {*} name the id element
*/
remove(name) { remove(name) {
this.index.remove({id: name}); this.index.remove({id: name});
} }
/**
* Force a reindex.
*/
reindex() { reindex() {
let self = this; let self = this;
this.storage.get_local(function(err, packages) { this.storage.get_local(function(err, packages) {
@ -43,6 +67,10 @@ class Search {
}); });
} }
/**
* Set up the {Storage}
* @param {*} storage An storage reference.
*/
configureStorage(storage) { configureStorage(storage) {
this.storage = storage; this.storage = storage;
this.reindex(); this.reindex();