2016-07-20 18:45:28 +00:00
|
|
|
import store from './store'
|
2016-07-07 18:20:22 +00:00
|
|
|
|
2017-04-24 02:27:21 +00:00
|
|
|
const DEFAULTS = [
|
|
|
|
'+1',
|
|
|
|
'grinning',
|
|
|
|
'kissing_heart',
|
|
|
|
'heart_eyes',
|
|
|
|
'laughing',
|
|
|
|
'stuck_out_tongue_winking_eye',
|
|
|
|
'sweat_smile',
|
|
|
|
'joy',
|
|
|
|
'scream',
|
|
|
|
'disappointed',
|
|
|
|
'unamused',
|
|
|
|
'weary',
|
|
|
|
'sob',
|
|
|
|
'sunglasses',
|
|
|
|
'heart',
|
|
|
|
'poop',
|
|
|
|
]
|
2017-04-19 13:30:56 +00:00
|
|
|
|
2017-11-03 00:57:29 +00:00
|
|
|
let frequently, initialized
|
2017-05-01 09:18:40 +00:00
|
|
|
let defaults = {}
|
2016-07-07 18:20:22 +00:00
|
|
|
|
2017-11-03 00:57:29 +00:00
|
|
|
function init() {
|
|
|
|
initialized = true
|
|
|
|
frequently = store.get('frequently')
|
|
|
|
}
|
|
|
|
|
2016-07-07 18:20:22 +00:00
|
|
|
function add(emoji) {
|
2017-11-03 00:57:29 +00:00
|
|
|
if (!initialized) init()
|
2016-07-07 18:20:22 +00:00
|
|
|
var { id } = emoji
|
|
|
|
|
2017-05-01 09:18:40 +00:00
|
|
|
frequently || (frequently = defaults)
|
2016-07-07 18:20:22 +00:00
|
|
|
frequently[id] || (frequently[id] = 0)
|
|
|
|
frequently[id] += 1
|
|
|
|
|
|
|
|
store.set('last', id)
|
|
|
|
store.set('frequently', frequently)
|
|
|
|
}
|
|
|
|
|
2017-04-24 02:27:21 +00:00
|
|
|
function get(perLine) {
|
2017-11-03 00:57:29 +00:00
|
|
|
if (!initialized) init()
|
2017-04-24 02:27:21 +00:00
|
|
|
if (!frequently) {
|
2017-05-01 09:18:40 +00:00
|
|
|
defaults = {}
|
2017-04-24 02:27:21 +00:00
|
|
|
|
2017-09-17 08:54:22 +00:00
|
|
|
const result = []
|
|
|
|
|
|
|
|
for (let i = 0; i < perLine; i++) {
|
2017-05-01 09:18:40 +00:00
|
|
|
defaults[DEFAULTS[i]] = perLine - i
|
2017-09-17 08:54:22 +00:00
|
|
|
result.push(DEFAULTS[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
const quantity = perLine * 4
|
|
|
|
const frequentlyKeys = []
|
|
|
|
|
|
|
|
for (let key in frequently) {
|
|
|
|
if (frequently.hasOwnProperty(key)) {
|
2017-10-07 04:02:02 +00:00
|
|
|
frequentlyKeys.push(key)
|
2017-09-17 08:54:22 +00:00
|
|
|
}
|
2017-04-24 02:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 04:02:02 +00:00
|
|
|
const sorted = frequentlyKeys
|
|
|
|
.sort((a, b) => frequently[a] - frequently[b])
|
|
|
|
.reverse()
|
2017-09-17 08:54:22 +00:00
|
|
|
const sliced = sorted.slice(0, quantity)
|
|
|
|
|
|
|
|
const last = store.get('last')
|
2016-07-07 18:20:22 +00:00
|
|
|
|
|
|
|
if (last && sliced.indexOf(last) == -1) {
|
|
|
|
sliced.pop()
|
|
|
|
sliced.push(last)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sliced
|
|
|
|
}
|
|
|
|
|
|
|
|
export default { add, get }
|