emoji-mart-lazyload/src/utils/frequently.js

82 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-07-20 18:45:28 +00:00
import store from './store'
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',
]
let frequently, initialized
let defaults = {}
function init() {
initialized = true
frequently = store.get('frequently')
}
function add(emoji) {
if (!initialized) init()
var { id } = emoji
frequently || (frequently = defaults)
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) {
if (!initialized) init()
2017-04-24 02:27:21 +00:00
if (!frequently) {
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++) {
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')
if (last && sliced.indexOf(last) == -1) {
sliced.pop()
sliced.push(last)
}
return sliced
}
export default { add, get }