emotesjs 0.0.24 → 0.0.25

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 +1 -42
  2. package/index.js +1 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -6,45 +6,4 @@
6
6
 
7
7
  Fast, dependency free, and responsive 7TV inline emotes parse
8
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.22/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
- ```
9
+ [Check the documentation here](https://darckfast.com/docs/emotesjs)
package/index.js CHANGED
@@ -1 +1 @@
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();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.#usePixelDensity=opts.usePixelDensity;this.#usePixelDensity||=false;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.isLoading=this.load();EmotesJS.instance=this}cache(){return JSON.stringify(Object.fromEntries(this.#cachedEmotes.entries()))}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)}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}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}"/>`;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;
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();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.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(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}"/>`;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};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "emotesjs",
3
3
  "description": "Fast, dependency free, and responsive 7TV inline emotes parse",
4
- "version": "0.0.24",
4
+ "version": "0.0.25",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "darckfast"