Add frequently utils

Store and returns frequently used emojis.
If not already in the frequently used, last used emoji will always be added to the array.
release
Etienne Lemay 2016-07-07 14:20:22 -04:00
parent 0befb30dc8
commit a922dbb8c5
2 changed files with 29 additions and 0 deletions

28
src/utils/frequently.js Normal file
View File

@ -0,0 +1,28 @@
import {store} from '.'
let frequently = store.get('frequently') || {}
function add(emoji) {
var { id } = emoji
frequently[id] || (frequently[id] = 0)
frequently[id] += 1
store.set('last', id)
store.set('frequently', frequently)
}
function get(quantity) {
var sorted = Object.keys(frequently).sort((a, b) => frequently[a] - frequently[b]).reverse(),
sliced = sorted.slice(0, quantity),
last = store.get('last')
if (last && sliced.indexOf(last) == -1) {
sliced.pop()
sliced.push(last)
}
return sliced
}
export default { add, get }

View File

@ -1 +1,2 @@
export {default as store} from './store'
export {default as frequently} from './frequently'