-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageProvider.js
70 lines (63 loc) · 1.88 KB
/
imageProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(function() {
var lunr = require('lunr');
require('lunr-languages/lunr.stemmer.support.js')(lunr);
require('lunr-languages/lunr.fr.js')(lunr);
/*
* Define index data for LUNR
* field: Searchable field
* ref: The value returned in a search to reference it to the found data
*/
var indexes = {
en: lunr(function () {
this.ref('imageId')
this.field('tags_en')
}),
fr: lunr(function () {
this.use(lunr.fr);
this.ref('imageId')
this.field('tags_fr')
})
}
/*
* Array of objects containing our searchable/retrievable data
*/
var images = [
{
imageId: 0,
tags_en: ['star', 'astronomy', 'constellation', 'space'],
tags_fr: ['étoile', 'astronomie', 'cosmos'],
extraField: 'should not search (or break)!'
},
{
imageId: 1,
tags_en: ['nature', 'trees', 'landscape'],
tags_fr: ['la nature', 'arbre', 'paysage']
}
];
/*
* Initializes any search/image related stuff, such as indexing our images
*/
var initialize = function() {
for(var i = 0; i < images.length; i++) {
indexes.en.add(images[i]);
indexes.fr.add(images[i]);
}
};
/*
* Largely unnecessary wrapper that just makes things more semantically straightforward in result parsing code
*/
var getImageData = function(id) {
return images[id];
};
/*
* Simple wrapper to create a consistent interface
*/
var search = function(language, searchTerm) {
return indexes[language].search(searchTerm);
}
module.exports = {
initialize: initialize,
search: search,
getImageData: getImageData
};
}());