Passed
Push — master ( e7326f...65e227 )
by Sergii
01:22
created

template/static/scripts/fulltext-search.js   A

Complexity

Total Complexity 8
Complexity/F 1.33

Size

Lines of Code 36
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 0
c 1
b 0
f 1
nc 1
dl 0
loc 36
rs 10
noi 1
wmc 8
mnd 1
bc 8
fnc 6
bpm 1.3333
cpm 1.3333
1
window.Searcher = (function() {
2
    function Searcher() {
3
        this._index = lunr(function () {
4
            this.field('title', {boost: 10})
5
            this.field('body')
6
            this.ref('id')
7
          }) ;
8
9
        this._indexContent = undefined;
10
    }
11
12
    Searcher.prototype.init = function() {
13
        var self = this;
14
15
        $("script[type='text/x-docstrap-searchdb']").each(function(idx, item)  {
16
            self._indexContent = JSON.parse(item.innerHTML);
17
18
            for (var entryId in self._indexContent) {
19
                self._index.add(self._indexContent[entryId]);
20
            }
21
        });
22
    };
23
24
    Searcher.prototype.search = function(searchTerm) {
25
        var results = [],
26
                searchResults = this._index.search(searchTerm);
27
28
        for (var idx = 0; idx < searchResults.length; idx++) {
29
            results.push(this._indexContent[searchResults[idx].ref])
30
        }
31
32
        return results;
33
    };
34
35
    return new Searcher();
36
})();