forked from RajeshkannanRamakrishnan/desktop-avatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay-helpers.js
161 lines (145 loc) · 3.04 KB
/
display-helpers.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict'
const marked = require('marked')
const emoji = require('emojilib')
/* way/
* try our best to give show a good name from the data
* we have about the user
*/
function userName(ui) {
if(!ui) return "(no user)"
if(ui.firstName && ui.lastName) {
return ui.firstName + " " + ui.lastName
}
if(ui.firstName) return ui.firstName
if(ui.lastName) return ui.lastName
return "(no name)"
}
/* way/
* return user timezone (default to GMT)
*/
function timeZone(ui) {
return ui.timeZone || "GMT"
}
/* way/
* return a random smiley emoji
*/
function smiley() {
return anEmoji("smile")
}
/* way/
* randomly pick an emoji that matches the given keyword
*/
function anEmoji(keyword) {
let options = []
for(let k in emoji.lib) {
let c = emoji.lib[k]
if(c.keywords.indexOf(keyword)!=-1) {
options.push(`:${k}:`)
}
}
return oneOf(options)
}
/* way/
* greet the user based on the time of day or sometimes
* just by wishing them generically
*/
function greeting(ui) {
if(ui) ui = ` ${userName(ui)}`
else ui = ""
const greetings = [
"Hi there",
"Hello",
"Welcome",
"Good to see you",
"Hi",
]
if(Math.random() > 0.7) {
return oneOf(greetings) + ui
}
let hh = (new Date()).getHours()
if(hh >= 6 && hh < 12) return "Good Morning" + ui
if(hh >= 12 && hh < 16) return "Good Afternoon" + ui
return "Good Evening" + ui
}
/* way/
* return the parsed version of the markdown text
*/
function md(txt) {
return marked(txt)
}
/* understand/
* emoji shortcodes are like :smile:, :fire:, :+1:
*/
const rxMoj = /:[-+0-9a-z_]*:/g
/* outcome/
* Find emoji shortcodes and replace them with the HTML
* equivalents
*/
function emojify(txt) {
return txt.replace(rxMoj, sc => {
return moj(sc.substring(1, sc.length-1))
})
}
/* outcome/
* Find emoji shortcodes and replace them with the corresponding
* char
*/
function emojifyConsole(txt) {
return txt.replace(rxMoj, sc => {
return mojConsole(sc.substring(1, sc.length-1))
})
}
/* outcome/
* return the emoji for the given shortcode
*/
function mojConsole(code) {
if(!emoji.lib[code]) return `:${code}:`
return emoji.lib[code].char
}
/* outcome/
* return the emoji for the given shortcode
*/
function moj(code) {
if(!emoji.lib[code]) return `:${code}:`
let i = 0
const c = emoji.lib[code].char
let v = ""
while(i < c.length) {
let cp = c.codePointAt(i)
i++
if(cp > 65535) i++
v += `&#${cp};`
}
return v
}
/* way/
* check if the given text is nothing but emoji's
*/
function isJustEmojis(txt) {
txt = txt.replace(rxMoj, "")
return !txt.trim()
}
/* way/
* randomly pick one of the arguments (or one of the array
* of elements passed in)
*/
function oneOf(a) {
if(!Array.isArray(a)) a = Array.prototype.slice.call(arguments)
return (a && a.length)
? a[Math.floor(Math.random()*a.length)]
: ""
}
module.exports = {
userName,
greeting,
timeZone,
md,
emojify,
smiley,
anEmoji,
isJustEmojis,
oneOf,
moj,
emojifyConsole,
mojConsole,
}