-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
52 lines (40 loc) 路 1.15 KB
/
index.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
const emojilib = require('emojilib');
const lunr = require('lunr');
const DEFAULT_OPTIONS = {
fuzzy: 0,
nameBoost: 3,
formatOutput: el => el.ref,
};
let Emojideas = function (options) {
this.options = Object.assign({}, DEFAULT_OPTIONS, options);
this._buildIndex(this.options);
this._fuzzySuffix = this.options.fuzzy ? `~${ this.options.fuzzy }` : '';
};
Emojideas.prototype.suggest = function (input) {
return this._index.search(this._processInput(input.replace('_', ' '))).map(this.options.formatOutput);
};
Emojideas.prototype._processInput = function (input) {
return `${ input }${ this._fuzzySuffix }`;
};
Emojideas.prototype._buildIndex = function (options) {
let lib = {};
for (let key in emojilib) {
let [name, ...keywords] = emojilib[key];
lib[name] = {
char: key,
name: name.replace('_', ' '),
keywords: keywords.join(' '),
};
}
this._index = lunr(function () {
this.ref('char');
if (options.nameBoost) {
this.field('name', { boost: options.nameBoost });
}
this.field('keywords');
for (let key in lib) {
this.add(lib[key]);
}
});
};
module.exports = Emojideas;