emotesjs 0.0.27 → 1.0.0

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/README.md CHANGED
@@ -6,4 +6,26 @@
6
6
 
7
7
  Fast, dependency free, and responsive 7TV inline emotes parse
8
8
 
9
+ ## Basic usage
10
+
11
+ ```sh
12
+ npm i emotesjs
13
+ ```
14
+
15
+ ```ts
16
+ import { EmotesJS } from 'emotesjs'
17
+
18
+ let emotes = new EmotesJS({ channelId: 123456 })
19
+
20
+ let html = emotes.parse('this is pretty Pog')
21
+
22
+ console.log(html)
23
+ // this is pretty <img srcset="https://cdn.7tv.app/emote/01EZTCN91800012PTN006Q50PR/4x.webp 128w,
24
+ // https://cdn.7tv.app/emote/01EZTCN91800012PTN006Q50PR/3x.webp 96w,
25
+ // https://cdn.7tv.app/emote/01EZTCN91800012PTN006Q50PR/2x.webp 64w,
26
+ // https://cdn.7tv.app/emote/01EZTCN91800012PTN006Q50PR/1x.webp 32w"
27
+ // alt="Pog" style="height:1.65rem"
28
+ // />
29
+ ```
30
+
9
31
  [Check the documentation here](https://darckfast.com/docs/emotesjs)
@@ -0,0 +1,124 @@
1
+ interface SevenTVChannelEmotes {
2
+ id: string;
3
+ platform: string;
4
+ username: string;
5
+ display_name: string;
6
+ linked_at: number;
7
+ emote_capacity: number;
8
+ emote_set_id: string;
9
+ emote_set: EmoteSet;
10
+ user: User;
11
+ }
12
+ interface EmoteSet {
13
+ id: string;
14
+ name: string;
15
+ flags: number;
16
+ tags: any[];
17
+ immutable: boolean;
18
+ privileged: boolean;
19
+ emotes: Emote[];
20
+ emote_count: number;
21
+ capacity: number;
22
+ owner: any;
23
+ }
24
+ interface Emote {
25
+ id: string;
26
+ name: string;
27
+ flags: number;
28
+ timestamp: number;
29
+ actor_id?: string;
30
+ data: Data;
31
+ origin_id: any;
32
+ }
33
+ interface Data {
34
+ id: string;
35
+ name: string;
36
+ flags: number;
37
+ lifecycle: number;
38
+ state: string[];
39
+ listed: boolean;
40
+ animated: boolean;
41
+ owner?: Owner;
42
+ host: Host;
43
+ tags?: string[];
44
+ }
45
+ interface Owner {
46
+ id: string;
47
+ username: string;
48
+ display_name: string;
49
+ avatar_url?: string;
50
+ style: Style;
51
+ role_ids: string[];
52
+ connections: Connection[];
53
+ }
54
+ interface Style {
55
+ color?: number;
56
+ paint_id?: string;
57
+ badge_id?: string;
58
+ }
59
+ interface Connection {
60
+ id: string;
61
+ platform: string;
62
+ username: string;
63
+ display_name: string;
64
+ linked_at: number;
65
+ emote_capacity: number;
66
+ emote_set_id: string;
67
+ emote_set: any;
68
+ }
69
+ interface Host {
70
+ url: string;
71
+ files: File[];
72
+ }
73
+ interface File {
74
+ name: string;
75
+ static_name: string;
76
+ width: number;
77
+ height: number;
78
+ frame_count: number;
79
+ size: number;
80
+ format: string;
81
+ }
82
+ interface User {
83
+ id: string;
84
+ username: string;
85
+ display_name: string;
86
+ created_at: number;
87
+ avatar_url: string;
88
+ style: Style;
89
+ emote_sets: EmoteSet[];
90
+ editors: Editor[];
91
+ roles: string[];
92
+ connections: Connection[];
93
+ }
94
+ interface Editor {
95
+ id: string;
96
+ permissions: number;
97
+ visible: boolean;
98
+ added_at: number;
99
+ }
100
+ interface Opts {
101
+ channelId?: number;
102
+ only?: Array<string>;
103
+ usePixelDensity?: boolean;
104
+ colon?: boolean;
105
+ height?: string;
106
+ format?: string;
107
+ cache?: string;
108
+ proxy?: string;
109
+ }
110
+ declare class EmotesJS {
111
+ #private;
112
+ total: number;
113
+ channelId: number;
114
+ isLoading: Promise<void>;
115
+ proxy: string;
116
+ static instance: EmotesJS;
117
+ constructor(opts: Opts);
118
+ static fromCache(cache: string): EmotesJS;
119
+ cache(): string;
120
+ load(only?: Array<string>): Promise<void>;
121
+ parse(text: string): string;
122
+ }
123
+
124
+ export { type Connection, type Data, type Editor, type Emote, type EmoteSet, EmotesJS, type File, type Host, type Owner, type SevenTVChannelEmotes, type Style, type User };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ var u=class a{#e=new Map;#s=!0;#t="1.65rem";#i="WEBP";#r="https://cdn.7tv.app";#n=!1;#a=!1;total=0;channelId=0;isLoading=Promise.resolve();proxy="";static instance;constructor(e){if(a.instance&&a.instance.channelId!==0)return a.instance;let i;e&&(i=e.only,this.channelId=e.channelId||0,this.#s=!!e.colon,this.#t=e.height||this.#t,this.#i=e.format||this.#i,this.#a=!!e.usePixelDensity,e.cache&&typeof e.cache=="string"&&(this.#e=new Map(Object.entries(JSON.parse(e.cache))),this.total=this.#e.size,this.#n=!0),this.proxy=e.proxy||""),this.isLoading=this.load(i),a.instance=this}static fromCache(e){return new a({cache:e})}cache(){return JSON.stringify(Object.fromEntries(this.#e.entries()))}async load(e=[]){let i=fetch("https://7tv.io/v3/emote-sets/global").then(t=>{if(t.ok)return t.json();throw new Error("fetch unsuccessful")}),s=fetch("https://7tv.io/v3/users/twitch/"+this.channelId).then(t=>{if(t.ok)return t.json();throw new Error("fetch unsuccessful")}),[r,n]=await Promise.allSettled([i,s]),o=[];n.status==="fulfilled"&&o.push(...n.value.emote_set.emotes),r.status==="fulfilled"&&o.push(...r.value.emotes),this.#e.size!==0&&this.#e.clear();for(let t of o){let c=t.data.name,l=`https:${t.data.host.url}`;if(!l.includes(this.#r)||(this.proxy&&(l=l.replace(this.#r,this.proxy)),e.length>0&&!e.includes(c)))continue;let f=`<img srcset="${t.data.host.files.filter(m=>m.format===this.#i).reduce((m,h)=>{let g=`${h.width}w`;return this.#a&&([g]=h.name.split(".")||""),`${l}/${h.name} ${g}, ${m}`},"")}" alt="${c}" style="height:${this.#t}" crossorigin/>`;this.#e.set(c,f)}this.total=this.#e.size,this.#n=!0}parse(e){if(!e)return console.log("no text to parse emotes"),"";if(!this.#n)return console.log("emotes are not ready"),e;if(this.#e.size===0)return console.log("no emotes loaded"),e;let i=e.split(" "),s="";for(let r=0;r<i.length;r++){let n=i[r];if(this.#s&&!n?.startsWith(":")){s+=n+" ";continue}let o=n?.replaceAll(":","")||"",t=this.#e.get(o);if(t){s+=t+" ";continue}s+=n+" "}return s.trim()}};export{u as EmotesJS};
package/package.json CHANGED
@@ -1,32 +1,45 @@
1
1
  {
2
- "name": "emotesjs",
3
- "description": "Fast, dependency free, and responsive 7TV inline emotes parse",
4
- "version": "0.0.27",
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
- "vitest": "^3.2.4"
28
- },
29
- "scripts": {
30
- "test": "vitest run"
31
- }
32
- }
2
+ "name": "emotesjs",
3
+ "description": "Fast, dependency free, and responsive 7TV inline emotes parse",
4
+ "version": "1.0.0",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "darckfast"
8
+ },
9
+ "engines": {
10
+ "node": ">=22"
11
+ },
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "main": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "type": "module",
18
+ "exports": {
19
+ ".": {
20
+ "import": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ }
24
+ }
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git://github.com/darckfast/emotesjs.git"
29
+ },
30
+ "keywords": [
31
+ "twitch",
32
+ "7tv",
33
+ "emotes",
34
+ "responsive"
35
+ ],
36
+ "scripts": {
37
+ "test": "vitest run",
38
+ "prepublishOnly": "npx tsup && npx terser ./dist/index.js -o ./dist/index.js"
39
+ },
40
+ "devDependencies": {
41
+ "tsup": "^8.5.1",
42
+ "typescript": "^6.0.3",
43
+ "vitest": "^3.2.4"
44
+ }
45
+ }
package/index.js DELETED
@@ -1 +0,0 @@
1
- class EmotesJS{#cachedEmotes=new Map;#colon=true;#height="1.65rem";#format="WEBP";#allowedOrigins="https://cdn.7tv.app";#isReady=false;#usePixelDensity=false;total=0;channelId=0;isLoading=Promise.resolve();proxy="";static instance;constructor(opts){if(EmotesJS.instance&&EmotesJS.instance.channelId!==0){return EmotesJS.instance}let only;if(opts){only=opts.only;this.channelId=opts.channelId||0;this.#colon=Boolean(opts.colon);this.#height=opts.height||this.#height;this.#format=opts.format||this.#format;this.#usePixelDensity=Boolean(opts.usePixelDensity);if(opts.cache&&typeof opts.cache==="string"){this.#cachedEmotes=new Map(Object.entries(JSON.parse(opts.cache)));this.total=this.#cachedEmotes.size;this.#isReady=true}this.proxy=opts.proxy}this.isLoading=this.load(only);EmotesJS.instance=this}static fromCache(cache){return new EmotesJS({cache:cache})}cache(){return JSON.stringify(Object.fromEntries(this.#cachedEmotes.entries()))}async load(only=[]){let globalProm=fetch("https://7tv.io/v3/emote-sets/global").then(r=>{if(r.ok){return r.json()}throw new Error("fetch unsuccessful")});let chProm=fetch("https://7tv.io/v3/users/twitch/"+this.channelId).then(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)}if(this.#cachedEmotes.size!==0){this.#cachedEmotes.clear()}for(let emote of rawEmotes){let name=emote.data.name;let url=`https:${emote.data.host.url}`;if(!url.includes(this.#allowedOrigins)){continue}if(this.proxy){url=url.replace(this.#allowedOrigins,this.proxy)}if(only.length>0&&!only.includes(name)){continue}let files=emote.data.host.files.filter(x=>x.format===this.#format);let srcset=files.reduce((acc,curr)=>{let w=`${curr.width}w`;if(this.#usePixelDensity){[w]=curr.name.split(".")}return`${url}/${curr.name} ${w}, ${acc}`},"");let elementString=`<img srcset="${srcset}" alt="${name}" style="height:${this.#height}" crossorigin/>`;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""}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};