Files: 9f05fffe1423a376666951a479b2f427ba0c4d68 / emoji / async / suggest.js
1243 bytesRaw
1 | const nest = require('depnest') |
2 | const { h } = require('mutant') |
3 | |
4 | exports.gives = nest('emoji.async.suggest') |
5 | |
6 | exports.needs = nest({ |
7 | 'emoji.sync.names': 'first', |
8 | 'emoji.sync.url': 'first' |
9 | }) |
10 | |
11 | exports.create = function (api) { |
12 | var suggestions = null |
13 | var subscribed = null |
14 | |
15 | return nest('emoji.async.suggest', suggestedEmoji) |
16 | |
17 | function suggestedEmoji () { |
18 | return function (word) { |
19 | // when no emoji typed, list some default ones |
20 | if (!word) { |
21 | return api.emoji.sync.names() |
22 | .sort(() => Math.random() > 0.5 ? +1 : -1) //shuffle |
23 | .slice(0, 10) |
24 | .map(toSuggestion) |
25 | } |
26 | |
27 | if (word[word.length - 1] === ':') { |
28 | word = word.slice(0, -1) |
29 | } |
30 | var word = word.toLowerCase() |
31 | |
32 | return api.emoji.sync.names() |
33 | .filter(name => ~name.indexOf(word)) |
34 | .sort((a, b) => { |
35 | // if word fragment occurs earlier in name, bump up |
36 | return a.indexOf(word) < b.indexOf(word) ? -1 : +1 |
37 | }) |
38 | .slice(0, 100) |
39 | .map(toSuggestion) |
40 | } |
41 | } |
42 | |
43 | function toSuggestion (emoji) { |
44 | return { |
45 | image: api.emoji.sync.url(emoji), |
46 | title: emoji, |
47 | subtitle: h('div.emoji', emoji), |
48 | value: ':' + emoji + ':' |
49 | } |
50 | } |
51 | } |
52 | |
53 |
Built with git-ssb-web