emotesjs 0.0.16 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +32 -27
- package/package.json +7 -2
package/index.js
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
1
|
class EmotesJS {
|
|
2
|
-
cachedEmotes = new Map()
|
|
3
|
-
isReady = false
|
|
4
|
-
|
|
2
|
+
#cachedEmotes = new Map()
|
|
3
|
+
#isReady = false
|
|
4
|
+
#requireColon = true
|
|
5
|
+
#height = "1.65rem"
|
|
6
|
+
#format = "WEBP"
|
|
7
|
+
#allowedOrigins = "https://cdn.7tv.app"
|
|
8
|
+
|
|
9
|
+
loadedEmotes = 0
|
|
5
10
|
channelId = 0
|
|
6
|
-
|
|
7
|
-
height = "1.65rem"
|
|
8
|
-
format = "WEBP"
|
|
9
|
-
allowedOrigins = "https://cdn.7tv.app"
|
|
11
|
+
isLoading = Promise.resolve()
|
|
10
12
|
static instance
|
|
11
13
|
|
|
12
14
|
constructor(opts) {
|
|
13
|
-
if (EmotesJS.instance) {
|
|
15
|
+
if (EmotesJS.instance && EmotesJS.instance.channelId !== 0) {
|
|
14
16
|
return EmotesJS.instance
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
if (opts) {
|
|
18
20
|
this.channelId = opts.channelId
|
|
19
|
-
this
|
|
20
|
-
this
|
|
21
|
-
this
|
|
21
|
+
this.#requireColon = opts.requireColon
|
|
22
|
+
this.#requireColon ||= false
|
|
23
|
+
this.#height = opts.height || this.#height
|
|
24
|
+
this.#format = opts.format || this.#format
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
this.isLoading = this.load()
|
|
@@ -58,48 +61,50 @@ class EmotesJS {
|
|
|
58
61
|
let name = emote.data.name
|
|
59
62
|
let url = `https:${emote.data.host.url}`
|
|
60
63
|
|
|
61
|
-
if (!url.includes(this
|
|
64
|
+
if (!url.includes(this.#allowedOrigins)) {
|
|
62
65
|
continue
|
|
63
66
|
}
|
|
64
67
|
|
|
65
|
-
let files = emote.data.host.files.filter(x => x.format === this
|
|
66
|
-
let srcset = files.reduce((acc, curr) => `${url}/${curr.name} ${curr.width}w, ${acc}`,
|
|
67
|
-
let elementString = `<img srcset="${srcset}" alt="${name}" style="height:${this
|
|
68
|
+
let files = emote.data.host.files.filter(x => x.format === this.#format)
|
|
69
|
+
let srcset = files.reduce((acc, curr) => `${url}/${curr.name} ${curr.width}w, ${acc}`, "")
|
|
70
|
+
let elementString = `<img srcset="${srcset}" alt="${name}" style="height:${this.#height}"/>`
|
|
68
71
|
|
|
69
|
-
this
|
|
72
|
+
this.#cachedEmotes.set(name, elementString)
|
|
70
73
|
}
|
|
71
74
|
|
|
72
|
-
this.
|
|
75
|
+
this.loadedEmotes = this.#cachedEmotes.size
|
|
76
|
+
|
|
77
|
+
this.#isReady = true
|
|
73
78
|
}
|
|
74
79
|
|
|
75
80
|
parse(text) {
|
|
76
|
-
if (!text || !this
|
|
77
|
-
console.log(
|
|
81
|
+
if (!text || !this.#isReady) {
|
|
82
|
+
console.log("emotes not ready")
|
|
78
83
|
return text
|
|
79
84
|
}
|
|
80
85
|
|
|
81
|
-
if (this
|
|
82
|
-
console.log(
|
|
86
|
+
if (this.#isReady && this.#cachedEmotes.size === 0) {
|
|
87
|
+
console.log("no emotes loaded")
|
|
83
88
|
return text
|
|
84
89
|
}
|
|
85
90
|
|
|
86
|
-
let words = text.split(
|
|
91
|
+
let words = text.split(" ")
|
|
87
92
|
|
|
88
|
-
let fullText =
|
|
93
|
+
let fullText = ""
|
|
89
94
|
for (let i = 0; i < words.length; i++) {
|
|
90
95
|
let word = words[i]
|
|
91
96
|
|
|
92
97
|
|
|
93
|
-
if (this
|
|
98
|
+
if (this.#requireColon && !word.startsWith(":")) {
|
|
94
99
|
fullText += word + " "
|
|
95
100
|
continue
|
|
96
101
|
}
|
|
97
102
|
|
|
98
|
-
let wordKey = word.replaceAll(
|
|
99
|
-
let emote = this
|
|
103
|
+
let wordKey = word.replaceAll(":", "")
|
|
104
|
+
let emote = this.#cachedEmotes.get(wordKey)
|
|
100
105
|
|
|
101
106
|
if (emote) {
|
|
102
|
-
fullText += emote +
|
|
107
|
+
fullText += emote + " "
|
|
103
108
|
continue;
|
|
104
109
|
}
|
|
105
110
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "emotesjs",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.18",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "darckfast"
|
|
@@ -22,5 +22,10 @@
|
|
|
22
22
|
"7tv",
|
|
23
23
|
"emotes"
|
|
24
24
|
],
|
|
25
|
-
"scripts": {
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "vitest run"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"vitest": "^3.2.4"
|
|
30
|
+
}
|
|
26
31
|
}
|