emotesjs 0.0.15 → 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.
Files changed (2) hide show
  1. package/index.js +40 -31
  2. 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
- isLoading = Promise.resolve()
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
- requireColon = true
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.requireColon = opts.requireColon
20
- this.height = opts.height || this.height
21
- this.format = opts.format || this.format
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,51 +61,57 @@ 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.allowedOrigins)) {
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.format)
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.height}"/>`
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.cachedEmotes.set(name, elementString)
72
+ this.#cachedEmotes.set(name, elementString)
70
73
  }
71
74
 
72
- this.isReady = true
75
+ this.loadedEmotes = this.#cachedEmotes.size
76
+
77
+ this.#isReady = true
73
78
  }
74
79
 
75
80
  parse(text) {
76
- if (!text || !this.isReady) {
77
- console.log('emotes not ready')
81
+ if (!text || !this.#isReady) {
82
+ console.log("emotes not ready")
78
83
  return text
79
84
  }
80
85
 
81
- if (this.isReady && this.cachedEmotes.size === 0) {
82
- console.log('no emotes loaded')
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
- if (this.requireColon && word.startsWith(":")) {
93
- let wordKey = word.replace(':', '')
94
- let emote = this.cachedEmotes.get(wordKey)
95
97
 
96
- if (emote) {
97
- fullText += emote + ' '
98
- continue;
99
- }
98
+ if (this.#requireColon && !word.startsWith(":")) {
99
+ fullText += word + " "
100
+ continue
101
+ }
102
+
103
+ let wordKey = word.replaceAll(":", "")
104
+ let emote = this.#cachedEmotes.get(wordKey)
105
+
106
+ if (emote) {
107
+ fullText += emote + " "
108
+ continue;
100
109
  }
101
110
 
102
111
  fullText += word + " "
103
112
  }
104
113
 
105
- return fullText
114
+ return fullText.trim()
106
115
  }
107
116
 
108
117
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "emotesjs",
3
3
  "description": "",
4
- "version": "0.0.15",
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
  }