-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
75 lines (64 loc) · 1.49 KB
/
library.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function dateToString(date) {
return date.toLocaleString('en-SG', {
month: 'short',
day: 'numeric',
weekday: 'short',
hour: 'numeric',
minute: 'numeric'
})
}
function orderBy(selector, ascending = true) {
return (a, b) => selector(ascending ? a : b).localeCompare(selector(ascending ? b : a))
}
function orderByDesc(selector) {
return orderBy(selector, false)
}
function ryverLibrary(info) {
return {findEntity, findEntities}
function findEntities(...jids) {
return jids.map(findEntity)
}
function findEntity(jid) {
const predicate = entity => entity.jid === jid
const user = info.users.find(predicate)
if (user) {
return {...user, entityType: 'users'}
}
const team = info.teams.find(predicate)
if (team) {
return {...team, entityType: 'teams'}
}
const forum = info.forums.find(predicate)
if (forum) {
return {...forum, entityType: 'forums'}
}
return null
}
}
function chromePromises() {
return {
cookies: {
get: promisify(chrome.cookies.get)
},
idle: {
queryState: promisify(chrome.idle.queryState)
},
storage: {
local: {
get: promisify(chrome.storage.local.get.bind(chrome.storage.local))
}
}
}
}
function promisify(fn) {
return (...args) => new Promise((resolve, reject) => {
fn(...args, value => {
const error = chrome.runtime.lastError
if (error) {
reject(error)
} else {
resolve(value)
}
})
})
}