emotesjs 0.0.18 → 0.0.21

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 (3) hide show
  1. package/README.md +50 -0
  2. package/index.js +1 -120
  3. package/package.json +32 -30
package/README.md ADDED
@@ -0,0 +1,50 @@
1
+ ![banner](.github/images/banner.png)
2
+
3
+ ![NPM Version](https://img.shields.io/npm/v/emotesjs) ![npm package minimized gzipped size](https://img.shields.io/bundlejs/size/emotesjs)
4
+
5
+ # EmotesJS
6
+
7
+ Fast, dependency free, and responsive 7TV inline emotes parse
8
+
9
+ ## Usage
10
+
11
+ ### Installation
12
+ ```sh
13
+ pnpm i emotesjs
14
+ ```
15
+ or using a CDN
16
+ ```html
17
+ <script src="https://cdn.jsdelivr.net/npm/emotesjs@0.0.21/index.min.js"></script>
18
+ ```
19
+
20
+ ### Parsing
21
+ ```js
22
+ // const { EmotesJS } = require('emotesjs')
23
+ import { EmotesJS } from 'emotesjs'
24
+
25
+ let emotes = new EmotesJS({ channelId: 123456 })
26
+
27
+ // optional - once fetched, the emotes will be cached in memory
28
+ await emotes.isLoading
29
+
30
+ let html = emotes.parse('this is pretty Pog')
31
+
32
+ console.log(html)
33
+ // this is pretty <img srcset="https://cdn.7tv.app/emote/01EZTCN91800012PTN006Q50PR/4x.webp 128w,
34
+ // https://cdn.7tv.app/emote/01EZTCN91800012PTN006Q50PR/3x.webp 96w,
35
+ // https://cdn.7tv.app/emote/01EZTCN91800012PTN006Q50PR/2x.webp 64w,
36
+ // https://cdn.7tv.app/emote/01EZTCN91800012PTN006Q50PR/1x.webp 32w"
37
+ // alt="Pog" style="height:1.65rem"
38
+ // />
39
+ ```
40
+
41
+ ## Configuration
42
+
43
+ ```js
44
+ new EmotesJS({
45
+ channelId: 123456, // twitch channel id (or user id) to load the emotes
46
+ colon: false, // if true, the string must start with : e.g. :Pog
47
+ height: '1.65rem', // the element <img> height
48
+ format: 'WEBP' // or AVIF
49
+ })
50
+ ```
package/index.js CHANGED
@@ -1,120 +1 @@
1
- class EmotesJS {
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
10
- channelId = 0
11
- isLoading = Promise.resolve()
12
- static instance
13
-
14
- constructor(opts) {
15
- if (EmotesJS.instance && EmotesJS.instance.channelId !== 0) {
16
- return EmotesJS.instance
17
- }
18
-
19
- if (opts) {
20
- this.channelId = opts.channelId
21
- this.#requireColon = opts.requireColon
22
- this.#requireColon ||= false
23
- this.#height = opts.height || this.#height
24
- this.#format = opts.format || this.#format
25
- }
26
-
27
- this.isLoading = this.load()
28
- EmotesJS.instance = this
29
- }
30
-
31
- async load() {
32
- let globalProm = fetch("https://7tv.io/v3/emote-sets/global").then(async r => {
33
- if (r.ok) {
34
- return r.json()
35
- }
36
-
37
- throw new Error("fetch unsuccessful")
38
- })
39
-
40
- let chProm = fetch("https://7tv.io/v3/users/twitch/" + this.channelId).then(async r => {
41
- if (r.ok) {
42
- return r.json()
43
- }
44
-
45
- throw new Error("fetch unsuccessful")
46
- })
47
-
48
- let [global, ch] = await Promise.allSettled([globalProm, chProm])
49
-
50
- let rawEmotes = []
51
-
52
- if (ch.status === "fulfilled") {
53
- rawEmotes.push(...ch.value.emote_set.emotes)
54
- }
55
-
56
- if (global.status === "fulfilled") {
57
- rawEmotes.push(...global.value.emotes)
58
- }
59
-
60
- for (let emote of rawEmotes) {
61
- let name = emote.data.name
62
- let url = `https:${emote.data.host.url}`
63
-
64
- if (!url.includes(this.#allowedOrigins)) {
65
- continue
66
- }
67
-
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}"/>`
71
-
72
- this.#cachedEmotes.set(name, elementString)
73
- }
74
-
75
- this.loadedEmotes = this.#cachedEmotes.size
76
-
77
- this.#isReady = true
78
- }
79
-
80
- parse(text) {
81
- if (!text || !this.#isReady) {
82
- console.log("emotes not ready")
83
- return text
84
- }
85
-
86
- if (this.#isReady && this.#cachedEmotes.size === 0) {
87
- console.log("no emotes loaded")
88
- return text
89
- }
90
-
91
- let words = text.split(" ")
92
-
93
- let fullText = ""
94
- for (let i = 0; i < words.length; i++) {
95
- let word = words[i]
96
-
97
-
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;
109
- }
110
-
111
- fullText += word + " "
112
- }
113
-
114
- return fullText.trim()
115
- }
116
-
117
- }
118
-
119
- module.exports.EmotesJS = EmotesJS
120
-
1
+ class EmotesJS{#cachedEmotes=new Map;#isReady=false;#colon=true;#height="1.65rem";#format="WEBP";#allowedOrigins="https://cdn.7tv.app";total=0;channelId=0;isLoading=Promise.resolve();static instance;constructor(opts){if(EmotesJS.instance&&EmotesJS.instance.channelId!==0){return EmotesJS.instance}if(opts){this.channelId=opts.channelId;this.#colon=opts.colon;this.#colon||=false;this.#height=opts.height||this.#height;this.#format=opts.format||this.#format}this.isLoading=this.load();EmotesJS.instance=this}async load(){let globalProm=fetch("https://7tv.io/v3/emote-sets/global").then(async r=>{if(r.ok){return r.json()}throw new Error("fetch unsuccessful")});let chProm=fetch("https://7tv.io/v3/users/twitch/"+this.channelId).then(async r=>{if(r.ok){return r.json()}throw new Error("fetch unsuccessful")});let[global,ch]=await Promise.allSettled([globalProm,chProm]);let rawEmotes=[];if(ch.status==="fulfilled"){rawEmotes.push(...ch.value.emote_set.emotes)}if(global.status==="fulfilled"){rawEmotes.push(...global.value.emotes)}for(let emote of rawEmotes){let name=emote.data.name;let url=`https:${emote.data.host.url}`;if(!url.includes(this.#allowedOrigins)){continue}let files=emote.data.host.files.filter(x=>x.format===this.#format);let srcset=files.reduce((acc,curr)=>`${url}/${curr.name} ${curr.width}w, ${acc}`,"");let elementString=`<img srcset="${srcset}" alt="${name}" style="height:${this.#height}"/>`;this.#cachedEmotes.set(name,elementString)}this.total=this.#cachedEmotes.size;this.#isReady=true}parse(text){if(!text){console.log("no text to parse emotes");return text}if(!this.#isReady){console.log("emotes are not ready");return text}if(this.#cachedEmotes.size===0){console.log("no emotes loaded");return text}let words=text.split(" ");let fullText="";for(let i=0;i<words.length;i++){let word=words[i];if(this.#colon&&!word.startsWith(":")){fullText+=word+" ";continue}let wordKey=word.replaceAll(":","");let emote=this.#cachedEmotes.get(wordKey);if(emote){fullText+=emote+" ";continue}fullText+=word+" "}return fullText.trim()}}module.exports.EmotesJS=EmotesJS;
package/package.json CHANGED
@@ -1,31 +1,33 @@
1
1
  {
2
- "name": "emotesjs",
3
- "description": "",
4
- "version": "0.0.18",
5
- "license": "MIT",
6
- "author": {
7
- "name": "darckfast"
8
- },
9
- "engines": {
10
- "node": ">=22"
11
- },
12
- "files": [
13
- "index.js"
14
- ],
15
- "main": "index.js",
16
- "repository": {
17
- "type": "git",
18
- "url": "git://github.com/darckfast/emotesjs.git"
19
- },
20
- "keywords": [
21
- "twitch",
22
- "7tv",
23
- "emotes"
24
- ],
25
- "scripts": {
26
- "test": "vitest run"
27
- },
28
- "devDependencies": {
29
- "vitest": "^3.2.4"
30
- }
31
- }
2
+ "name": "emotesjs",
3
+ "description": "Fast, dependency free, and responsive 7TV inline emotes parse",
4
+ "version": "0.0.21",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "darckfast"
8
+ },
9
+ "engines": {
10
+ "node": ">=22"
11
+ },
12
+ "files": [
13
+ "index.js"
14
+ ],
15
+ "main": "index.js",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git://github.com/darckfast/emotesjs.git"
19
+ },
20
+ "keywords": [
21
+ "twitch",
22
+ "7tv",
23
+ "emotes",
24
+ "responsive"
25
+ ],
26
+ "devDependencies": {
27
+ "terser": "^5.43.1",
28
+ "vitest": "^3.2.4"
29
+ },
30
+ "scripts": {
31
+ "test": "vitest run"
32
+ }
33
+ }