@mkody/twitch-emoticons 2.9.6 → 3.0.0-beta.2

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 (80) hide show
  1. package/README.md +530 -169
  2. package/dist/TwitchEmoticons.cjs +950 -0
  3. package/dist/TwitchEmoticons.esm.min.js +7 -0
  4. package/dist/TwitchEmoticons.esm.min.js.map +7 -0
  5. package/dist/TwitchEmoticons.min.js +2 -20
  6. package/dist/TwitchEmoticons.min.js.map +4 -4
  7. package/package.json +99 -72
  8. package/src/index.js +38 -12
  9. package/src/struct/BTTVEmote.js +72 -74
  10. package/src/struct/Channel.js +48 -48
  11. package/src/struct/Emote.js +71 -71
  12. package/src/struct/EmoteFetcher.js +358 -322
  13. package/src/struct/EmoteParser.js +95 -84
  14. package/src/struct/EmoteTypeMapper.js +9 -9
  15. package/src/struct/FFZEmote.js +91 -93
  16. package/src/struct/SevenTVEmote.js +88 -90
  17. package/src/struct/TwitchEmote.js +72 -71
  18. package/src/util/Collection.js +69 -55
  19. package/src/util/Constants.js +30 -30
  20. package/typings/index.d.cts +254 -0
  21. package/typings/index.d.ts +216 -99
  22. package/.jsdoc.json +0 -46
  23. package/.nvmrc +0 -1
  24. package/docs/BTTVEmote.html +0 -1532
  25. package/docs/Channel.html +0 -824
  26. package/docs/Collection.html +0 -798
  27. package/docs/Emote.html +0 -832
  28. package/docs/EmoteFetcher.html +0 -3741
  29. package/docs/EmoteParser.html +0 -1187
  30. package/docs/FFZEmote.html +0 -1686
  31. package/docs/SevenTVEmote.html +0 -1596
  32. package/docs/TwitchEmote.html +0 -1528
  33. package/docs/fonts/Montserrat/Montserrat-Bold.eot +0 -0
  34. package/docs/fonts/Montserrat/Montserrat-Bold.ttf +0 -0
  35. package/docs/fonts/Montserrat/Montserrat-Bold.woff +0 -0
  36. package/docs/fonts/Montserrat/Montserrat-Bold.woff2 +0 -0
  37. package/docs/fonts/Montserrat/Montserrat-Regular.eot +0 -0
  38. package/docs/fonts/Montserrat/Montserrat-Regular.ttf +0 -0
  39. package/docs/fonts/Montserrat/Montserrat-Regular.woff +0 -0
  40. package/docs/fonts/Montserrat/Montserrat-Regular.woff2 +0 -0
  41. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.eot +0 -0
  42. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.svg +0 -978
  43. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.ttf +0 -0
  44. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff +0 -0
  45. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff2 +0 -0
  46. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.eot +0 -0
  47. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.svg +0 -1049
  48. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.ttf +0 -0
  49. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff +0 -0
  50. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff2 +0 -0
  51. package/docs/index.html +0 -233
  52. package/docs/scripts/collapse.js +0 -39
  53. package/docs/scripts/commonNav.js +0 -28
  54. package/docs/scripts/linenumber.js +0 -25
  55. package/docs/scripts/nav.js +0 -12
  56. package/docs/scripts/polyfill.js +0 -4
  57. package/docs/scripts/prettify/Apache-License-2.0.txt +0 -202
  58. package/docs/scripts/prettify/lang-css.js +0 -2
  59. package/docs/scripts/prettify/prettify.js +0 -28
  60. package/docs/scripts/search.js +0 -99
  61. package/docs/struct_BTTVEmote.js.html +0 -162
  62. package/docs/struct_Channel.js.html +0 -132
  63. package/docs/struct_Emote.js.html +0 -159
  64. package/docs/struct_EmoteFetcher.js.html +0 -429
  65. package/docs/struct_EmoteParser.js.html +0 -172
  66. package/docs/struct_FFZEmote.js.html +0 -185
  67. package/docs/struct_SevenTVEmote.js.html +0 -180
  68. package/docs/struct_TwitchEmote.js.html +0 -159
  69. package/docs/styles/jsdoc.css +0 -776
  70. package/docs/styles/prettify.css +0 -80
  71. package/docs/util_Collection.js.html +0 -151
  72. package/eslint.config.mjs +0 -215
  73. package/jest.config.js +0 -198
  74. package/test/BTTV.test.js +0 -48
  75. package/test/FFZ.test.js +0 -71
  76. package/test/SevenTV.test.js +0 -71
  77. package/test/ToFromObject.test.js +0 -156
  78. package/test/Twitch.test.js +0 -64
  79. package/test/__snapshots__/ToFromObject.test.js.snap +0 -121
  80. package/test/other.test.js +0 -72
@@ -1,429 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
-
5
- <meta charset="utf-8">
6
- <title>struct/EmoteFetcher.js - Documentation</title>
7
-
8
-
9
- <script src="scripts/prettify/prettify.js"></script>
10
- <script src="scripts/prettify/lang-css.js"></script>
11
- <!--[if lt IE 9]>
12
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
13
- <![endif]-->
14
- <link type="text/css" rel="stylesheet" href="styles/prettify.css">
15
- <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
16
- <script src="scripts/nav.js" defer></script>
17
-
18
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
19
- </head>
20
- <body>
21
-
22
- <input type="checkbox" id="nav-trigger" class="nav-trigger" />
23
- <label for="nav-trigger" class="navicon-button x">
24
- <div class="navicon"></div>
25
- </label>
26
-
27
- <label for="nav-trigger" class="overlay"></label>
28
-
29
- <nav >
30
-
31
-
32
- <h2><a href="index.html">Home</a></h2><h2><a href="https://github.com/mkody/twitch-emoticons" target="_blank" class="menu-item" id="github_link" >GitHub</a></h2><h3>Classes</h3><ul><li><a href="BTTVEmote.html">BTTVEmote</a><ul class='methods'><li data-type='method'><a href="BTTVEmote.html#toLink">toLink</a></li><li data-type='method'><a href="BTTVEmote.html#toObject">toObject</a></li><li data-type='method'><a href="BTTVEmote.html#.fromObject">fromObject</a></li></ul></li><li><a href="Channel.html">Channel</a><ul class='methods'><li data-type='method'><a href="Channel.html#fetchBTTVEmotes">fetchBTTVEmotes</a></li><li data-type='method'><a href="Channel.html#fetchFFZEmotes">fetchFFZEmotes</a></li><li data-type='method'><a href="Channel.html#fetchSevenTVEmotes">fetchSevenTVEmotes</a></li></ul></li><li><a href="Emote.html">Emote</a><ul class='methods'><li data-type='method'><a href="Emote.html#toObject">toObject</a></li></ul></li><li><a href="EmoteFetcher.html">EmoteFetcher</a><ul class='methods'><li data-type='method'><a href="EmoteFetcher.html#fetchTwitchEmotes">fetchTwitchEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchBTTVEmotes">fetchBTTVEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchFFZEmotes">fetchFFZEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchSevenTVEmotes">fetchSevenTVEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fromObject">fromObject</a></li></ul></li><li><a href="EmoteParser.html">EmoteParser</a><ul class='methods'><li data-type='method'><a href="EmoteParser.html#parse">parse</a></li></ul></li><li><a href="FFZEmote.html">FFZEmote</a><ul class='methods'><li data-type='method'><a href="FFZEmote.html#toLink">toLink</a></li><li data-type='method'><a href="FFZEmote.html#toObject">toObject</a></li><li data-type='method'><a href="FFZEmote.html#.fromObject">fromObject</a></li></ul></li><li><a href="SevenTVEmote.html">SevenTVEmote</a><ul class='methods'><li data-type='method'><a href="SevenTVEmote.html#toLink">toLink</a></li><li data-type='method'><a href="SevenTVEmote.html#toObject">toObject</a></li><li data-type='method'><a href="SevenTVEmote.html#.fromObject">fromObject</a></li></ul></li><li><a href="TwitchEmote.html">TwitchEmote</a><ul class='methods'><li data-type='method'><a href="TwitchEmote.html#toLink">toLink</a></li><li data-type='method'><a href="TwitchEmote.html#toObject">toObject</a></li><li data-type='method'><a href="TwitchEmote.html#.fromObject">fromObject</a></li></ul></li><li><a href="Collection.html">Collection</a><ul class='methods'><li data-type='method'><a href="Collection.html#find">find</a></li><li data-type='method'><a href="Collection.html#filter">filter</a></li><li data-type='method'><a href="Collection.html#map">map</a></li></ul></li></ul>
33
-
34
- </nav>
35
-
36
- <div id="main">
37
-
38
- <h1 class="page-title">struct/EmoteFetcher.js</h1>
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
- <section>
47
- <article>
48
- <pre class="prettyprint source linenums"><code>const BTTVEmote = require('./BTTVEmote');
49
- const Channel = require('./Channel');
50
- const Collection = require('../util/Collection');
51
- const Constants = require('../util/Constants');
52
- const FFZEmote = require('./FFZEmote');
53
- const SevenTVEmote = require('./SevenTVEmote');
54
- const TwitchEmote = require('./TwitchEmote');
55
-
56
- const axios = require('axios');
57
- const { ApiClient } = require('@twurple/api');
58
- const { AppTokenAuthProvider } = require('@twurple/auth');
59
-
60
- class EmoteFetcher {
61
- /**
62
- * Fetches and caches emotes.
63
- * @param {string} clientId The client id for the twitch api.
64
- * @param {string} clientSecret The client secret for the twitch api.
65
- * @param {object} options Additional options.
66
- * @param {ApiClient} options.apiClient - Bring your own Twurple ApiClient.
67
- */
68
- constructor(clientId, clientSecret, options) {
69
- if (options &amp;&amp; options.apiClient) {
70
- this.apiClient = options.apiClient;
71
- } else if (clientId !== undefined &amp;&amp; clientSecret !== undefined) {
72
- const authProvider = new AppTokenAuthProvider(clientId, clientSecret);
73
-
74
- /**
75
- * Twitch api client.
76
- */
77
- this.apiClient = new ApiClient({ authProvider });
78
- }
79
-
80
- /**
81
- * Cached emotes.
82
- * Collectionped by emote code to Emote instance.
83
- * @type {Collection&lt;string, Emote>}
84
- */
85
- this.emotes = new Collection();
86
-
87
- /**
88
- * Cached channels.
89
- * Collectionped by name to Channel instance.
90
- * @type {Collection&lt;string, Channel>}
91
- */
92
- this.channels = new Collection();
93
-
94
- /**
95
- * Save if we fetched FFZ's modifier emotes once.
96
- * @type {boolean}
97
- */
98
- this.ffzModifiersFetched = false;
99
- }
100
-
101
- /**
102
- * The global channel for Twitch, BTTV and 7TV.
103
- * @readonly
104
- * @type {?Channel}
105
- */
106
- get globalChannel() {
107
- return this.channels.get(null);
108
- }
109
-
110
- /**
111
- * Sets up a channel
112
- * @private
113
- * @param {int} channel_id - ID of the channel.
114
- * @param {string} [format=null] - The type file format to use (webp/avif).
115
- * @throws {Error} When Twitch Client ID or Client Secret were not provided.
116
- * @returns {Channel}
117
- */
118
- _setupChannel(channel_id, format = null) {
119
- let channel = this.channels.get(channel_id);
120
- if (!channel) {
121
- channel = new Channel(this, channel_id);
122
- this.channels.set(channel_id, channel);
123
- }
124
- if (format) channel.format = format;
125
- return channel;
126
- }
127
-
128
- /**
129
- * Gets the raw Twitch emotes data for a channel.
130
- * @private
131
- * @param {int} id - ID of the channel.
132
- * @returns {Promise&lt;object[]>}
133
- */
134
- _getRawTwitchEmotes(id) {
135
- if (!this.apiClient) {
136
- throw new Error('Client id or client secret not provided.');
137
- }
138
-
139
- if (id) {
140
- return this.apiClient.chat.getChannelEmotes(id);
141
- } else {
142
- return this.apiClient.chat.getGlobalEmotes();
143
- }
144
- }
145
-
146
- /**
147
- * Converts and caches a raw twitch emote.
148
- * @private
149
- * @param {int} channel_id - ID of the channel.
150
- * @param {object} data - Raw data.
151
- * @param {TwitchEmote} [existing_emote=null] - Existing emote to cache.
152
- * @returns {TwitchEmote}
153
- */
154
- _cacheTwitchEmote(channel_id, data, existing_emote = null) {
155
- const channel = this._setupChannel(channel_id);
156
- const emote = existing_emote || new TwitchEmote(channel, data.id, data);
157
- this.emotes.set(emote.code, emote);
158
- channel.emotes.set(emote.code, emote);
159
- return emote;
160
- }
161
-
162
- /**
163
- * Gets the raw BTTV emotes data for a channel.
164
- * Use `null` for the global emotes channel.
165
- * @private
166
- * @param {int} [id=null] - ID of the channel.
167
- * @returns {Promise&lt;object[]>}
168
- */
169
- _getRawBTTVEmotes(id) {
170
- const endpoint = !id
171
- ? Constants.BTTV.Global
172
- : Constants.BTTV.Channel(id); // eslint-disable-line new-cap
173
-
174
- return axios.get(endpoint).then(req => {
175
- // Global emotes
176
- if (req.data instanceof Array) return req.data;
177
- // Channel emotes
178
- return [...req.data.channelEmotes, ...req.data.sharedEmotes];
179
- });
180
- }
181
-
182
- /**
183
- * Converts and caches a raw BTTV emote.
184
- * @private
185
- * @param {int} channel_id - ID of the channel.
186
- * @param {object} data - Raw data.
187
- * @param {BTTVEmote} [existing_emote=null] - Existing emote to cache.
188
- * @returns {BTTVEmote}
189
- */
190
- _cacheBTTVEmote(channel_id, data, existing_emote = null) {
191
- const channel = this._setupChannel(channel_id);
192
- const emote = existing_emote || new BTTVEmote(channel, data.id, data);
193
- this.emotes.set(emote.code, emote);
194
- channel.emotes.set(emote.code, emote);
195
- return emote;
196
- }
197
-
198
- /**
199
- * Gets the raw FFZ emote data from a set.
200
- * @private
201
- * @param {int} id - ID of the set.
202
- * @returns {Promise&lt;object[]>}
203
- */
204
- _getRawFFZEmoteSet(id) {
205
- const endpoint = Constants.FFZ.Set(id); // eslint-disable-line new-cap
206
-
207
- return axios.get(endpoint).then(req => {
208
- return req.data.set.emoticons;
209
- });
210
- }
211
-
212
- /**
213
- * Gets the raw FFZ emotes data for a channel.
214
- * @private
215
- * @param {int} id - ID of the channel.
216
- * @returns {Promise&lt;object[]>}
217
- */
218
- _getRawFFZEmotes(id) {
219
- const endpoint = Constants.FFZ.Channel(id); // eslint-disable-line new-cap
220
-
221
- return axios.get(endpoint).then(req => {
222
- const emotes = [];
223
- for (const key of Object.keys(req.data.sets)) {
224
- const set = req.data.sets[key];
225
- emotes.push(...set.emoticons);
226
- }
227
-
228
- return emotes;
229
- });
230
- }
231
-
232
- /**
233
- * Converts and caches a raw FFZ emote.
234
- * @private
235
- * @param {int} channel_id - ID of the channel.
236
- * @param {object} data - Raw data.
237
- * @param {FFZEmote} [existing_emote=null] - Existing emote to cache.
238
- * @returns {FFZEmote}
239
- */
240
- _cacheFFZEmote(channel_id, data, existing_emote = null) {
241
- const channel = this._setupChannel(channel_id);
242
- const emote = existing_emote || new FFZEmote(channel, data.id, data);
243
- this.emotes.set(emote.code, emote);
244
- channel.emotes.set(emote.code, emote);
245
- return emote;
246
- }
247
-
248
- /**
249
- * Gets the raw 7TV emotes data for a channel.
250
- * @private
251
- * @param {int} [id=null] - ID of the channel.
252
- * @returns {Promise&lt;object[]>}
253
- */
254
- _getRawSevenTVEmotes(id) {
255
- const endpoint = !id
256
- ? Constants.SevenTV.Global
257
- : Constants.SevenTV.Channel(id); // eslint-disable-line new-cap
258
-
259
- return axios.get(endpoint).then(req => req.data);
260
- }
261
-
262
- /**
263
- * Converts and caches a raw 7TV emote.
264
- * @private
265
- * @param {int} channel_id - ID of the channel.
266
- * @param {object} data - Raw data.
267
- * @param {string} format - The type file format to use (webp/avif).
268
- * @param {SevenTVEmote} [existing_emote=null] - Existing emote to cache.
269
- * @returns {SevenTVEmote}
270
- */
271
- _cacheSevenTVEmote(channel_id, data, format, existing_emote = null) {
272
- const channel = this._setupChannel(channel_id, format);
273
- const emote = existing_emote || new SevenTVEmote(channel, data.id, data);
274
- this.emotes.set(emote.code, emote);
275
- channel.emotes.set(emote.code, emote);
276
- return emote;
277
- }
278
-
279
- /**
280
- * Fetches the Twitch emotes for a channel.
281
- * Use `null` for the global emotes channel.
282
- * @param {int} [channel=null] - ID of the channel.
283
- * @returns {Promise&lt;Collection&lt;string, TwitchEmote>>}
284
- */
285
- fetchTwitchEmotes(channel = null) {
286
- return this._getRawTwitchEmotes(channel).then(rawEmotes => {
287
- for (const emote of rawEmotes) {
288
- this._cacheTwitchEmote(channel, {
289
- code: emote.name, id: emote.id, formats: emote.formats
290
- });
291
- }
292
-
293
- return this.channels.get(channel).emotes.filter(e => e.type === 'twitch');
294
- });
295
- }
296
-
297
- /**
298
- * Fetches the BTTV emotes for a channel.
299
- * Use `null` for the global emotes channel.
300
- * @param {int} [channel=null] - ID of the channel.
301
- * @returns {Promise&lt;Collection&lt;string, BTTVEmote>>}
302
- */
303
- fetchBTTVEmotes(channel = null) {
304
- return this._getRawBTTVEmotes(channel).then(rawEmotes => {
305
- for (const data of rawEmotes) {
306
- this._cacheBTTVEmote(channel, data);
307
- }
308
-
309
- return this.channels.get(channel).emotes.filter(e => e.type === 'bttv');
310
- });
311
- }
312
-
313
- /**
314
- * Fetches the FFZ emotes for a channel.
315
- * @param {int} [channel=null] - ID of the channel.
316
- * @returns {Promise&lt;Collection&lt;string, FFZEmote>>}
317
- */
318
- async fetchFFZEmotes(channel = null) {
319
- // Fetch modifier emotes at least once
320
- if (!this.ffzModifiersFetched) {
321
- this.ffzModifiersFetched = true;
322
-
323
- await this._getRawFFZEmoteSet(Constants.FFZ.sets.Modifiers).then(rawEmotes => {
324
- for (const data of rawEmotes) {
325
- this._cacheFFZEmote(null, data);
326
- }
327
- });
328
- }
329
-
330
- // If no channel specified, fetch the Global set
331
- if (!channel) {
332
- return this._getRawFFZEmoteSet(Constants.FFZ.sets.Global).then(rawEmotes => {
333
- for (const data of rawEmotes) {
334
- this._cacheFFZEmote(channel, data);
335
- }
336
-
337
- return this.channels.get(channel).emotes.filter(e => e.type === 'ffz');
338
- });
339
- }
340
-
341
- return this._getRawFFZEmotes(channel).then(rawEmotes => {
342
- for (const data of rawEmotes) {
343
- this._cacheFFZEmote(channel, data);
344
- }
345
-
346
- return this.channels.get(channel).emotes.filter(e => e.type === 'ffz');
347
- });
348
- }
349
-
350
- /**
351
- * Fetches the 7TV emotes for a channel.
352
- * @param {int} [channel=null] - ID of the channel.
353
- * @param {('webp'|'avif')} [format='webp'] - The type file format to use (webp/avif).
354
- * @returns {Promise&lt;Collection&lt;string, SevenTVEmote>>}
355
- */
356
- fetchSevenTVEmotes(channel = null, format = 'webp') {
357
- return this._getRawSevenTVEmotes(channel).then(rawEmotes => {
358
- if ('emotes' in rawEmotes) {
359
- // From an emote set (like "global")
360
- for (const data of rawEmotes.emotes) {
361
- this._cacheSevenTVEmote(channel, data, format);
362
- }
363
- } else {
364
- // From users
365
- for (const data of rawEmotes.emote_set.emotes) {
366
- this._cacheSevenTVEmote(channel, data, format);
367
- }
368
- }
369
-
370
- return this.channels.get(channel).emotes.filter(e => e.type === '7tv');
371
- });
372
- }
373
-
374
- /**
375
- * Converts emote Objects to emotes
376
- * @param {object} [emotesArray] - An array of emote objects
377
- * @throws {TypeError} When an emote has an unknown type.
378
- * @returns {Emote[]}
379
- */
380
- fromObject(emotesArray) {
381
- const emotes = [];
382
- const classMap = {
383
- bttv: { class: BTTVEmote, cache: (emoteObject, channel_id, existing_emote) => this._cacheBTTVEmote(channel_id, null, existing_emote) },
384
- ffz: { class: FFZEmote, cache: (emoteObject, channel_id, existing_emote) => this._cacheFFZEmote(channel_id, null, existing_emote) },
385
- '7tv': { class: SevenTVEmote, cache: (emoteObject, channel_id, existing_emote) => this._cacheSevenTVEmote(channel_id, null, emoteObject.imageType, existing_emote) },
386
- twitch: { class: TwitchEmote, cache: (emoteObject, channel_id, existing_emote) => this._cacheTwitchEmote(channel_id, null, existing_emote) }
387
- };
388
- for (const emoteObject of emotesArray) {
389
- const { type } = emoteObject;
390
- if (!Object.keys(classMap).includes(type)) {
391
- throw new TypeError(`Unknown type: ${type}`);
392
- }
393
-
394
- const emoteClass = classMap[type].class;
395
- this._setupChannel(emoteObject.channel_id, type === '7tv' ? emoteObject.imageType : null);
396
- const emote = emoteClass.fromObject(emoteObject, this.channels.get(emoteObject.channel_id));
397
- classMap[type].cache(emoteObject, emoteObject.channel_id, emote);
398
- emotes.push(emote);
399
- }
400
- return emotes;
401
- }
402
- }
403
-
404
- module.exports = EmoteFetcher;
405
- </code></pre>
406
- </article>
407
- </section>
408
-
409
-
410
-
411
-
412
-
413
-
414
- </div>
415
-
416
- <br class="clear">
417
-
418
- <footer>
419
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
420
- </footer>
421
-
422
- <script>prettyPrint();</script>
423
- <script src="scripts/polyfill.js"></script>
424
- <script src="scripts/linenumber.js"></script>
425
-
426
-
427
-
428
- </body>
429
- </html>
@@ -1,172 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
-
5
- <meta charset="utf-8">
6
- <title>struct/EmoteParser.js - Documentation</title>
7
-
8
-
9
- <script src="scripts/prettify/prettify.js"></script>
10
- <script src="scripts/prettify/lang-css.js"></script>
11
- <!--[if lt IE 9]>
12
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
13
- <![endif]-->
14
- <link type="text/css" rel="stylesheet" href="styles/prettify.css">
15
- <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
16
- <script src="scripts/nav.js" defer></script>
17
-
18
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
19
- </head>
20
- <body>
21
-
22
- <input type="checkbox" id="nav-trigger" class="nav-trigger" />
23
- <label for="nav-trigger" class="navicon-button x">
24
- <div class="navicon"></div>
25
- </label>
26
-
27
- <label for="nav-trigger" class="overlay"></label>
28
-
29
- <nav >
30
-
31
-
32
- <h2><a href="index.html">Home</a></h2><h2><a href="https://github.com/mkody/twitch-emoticons" target="_blank" class="menu-item" id="github_link" >GitHub</a></h2><h3>Classes</h3><ul><li><a href="BTTVEmote.html">BTTVEmote</a><ul class='methods'><li data-type='method'><a href="BTTVEmote.html#toLink">toLink</a></li><li data-type='method'><a href="BTTVEmote.html#toObject">toObject</a></li><li data-type='method'><a href="BTTVEmote.html#.fromObject">fromObject</a></li></ul></li><li><a href="Channel.html">Channel</a><ul class='methods'><li data-type='method'><a href="Channel.html#fetchBTTVEmotes">fetchBTTVEmotes</a></li><li data-type='method'><a href="Channel.html#fetchFFZEmotes">fetchFFZEmotes</a></li><li data-type='method'><a href="Channel.html#fetchSevenTVEmotes">fetchSevenTVEmotes</a></li></ul></li><li><a href="Emote.html">Emote</a><ul class='methods'><li data-type='method'><a href="Emote.html#toObject">toObject</a></li></ul></li><li><a href="EmoteFetcher.html">EmoteFetcher</a><ul class='methods'><li data-type='method'><a href="EmoteFetcher.html#fetchTwitchEmotes">fetchTwitchEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchBTTVEmotes">fetchBTTVEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchFFZEmotes">fetchFFZEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchSevenTVEmotes">fetchSevenTVEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fromObject">fromObject</a></li></ul></li><li><a href="EmoteParser.html">EmoteParser</a><ul class='methods'><li data-type='method'><a href="EmoteParser.html#parse">parse</a></li></ul></li><li><a href="FFZEmote.html">FFZEmote</a><ul class='methods'><li data-type='method'><a href="FFZEmote.html#toLink">toLink</a></li><li data-type='method'><a href="FFZEmote.html#toObject">toObject</a></li><li data-type='method'><a href="FFZEmote.html#.fromObject">fromObject</a></li></ul></li><li><a href="SevenTVEmote.html">SevenTVEmote</a><ul class='methods'><li data-type='method'><a href="SevenTVEmote.html#toLink">toLink</a></li><li data-type='method'><a href="SevenTVEmote.html#toObject">toObject</a></li><li data-type='method'><a href="SevenTVEmote.html#.fromObject">fromObject</a></li></ul></li><li><a href="TwitchEmote.html">TwitchEmote</a><ul class='methods'><li data-type='method'><a href="TwitchEmote.html#toLink">toLink</a></li><li data-type='method'><a href="TwitchEmote.html#toObject">toObject</a></li><li data-type='method'><a href="TwitchEmote.html#.fromObject">fromObject</a></li></ul></li><li><a href="Collection.html">Collection</a><ul class='methods'><li data-type='method'><a href="Collection.html#find">find</a></li><li data-type='method'><a href="Collection.html#filter">filter</a></li><li data-type='method'><a href="Collection.html#map">map</a></li></ul></li></ul>
33
-
34
- </nav>
35
-
36
- <div id="main">
37
-
38
- <h1 class="page-title">struct/EmoteParser.js</h1>
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
- <section>
47
- <article>
48
- <pre class="prettyprint source linenums"><code>const Constants = require('../util/Constants');
49
-
50
- class EmoteParser {
51
- /**
52
- * A parser to replace text with emotes.
53
- * @param {EmoteFetcher} fetcher - The fetcher to use the cache of.
54
- * @param {object} [options={}] - Options for the parser.
55
- * @param {string} [options.template=''] - The template to be used.
56
- * The strings that can be interpolated are:
57
- * - `{link}` The link of the emote.
58
- * - `{name}` The name of the emote.
59
- * - `{size}` The size of the image.
60
- * - `{creator}` The channel/owner name of the emote.
61
- * @param {string} [options.type='markdown'] - The type of the parser.
62
- * Can be one of `markdown`, `html`, `bbcode`, or `plain`.
63
- * If the `template` option is provided, this is ignored.
64
- * @param {RegExp} [options.match=/:(.+?):/g] - The regular expression that matches an emote.
65
- * Must be a global regex, with one capture group for the emote code.
66
- */
67
- constructor(fetcher, options = {}) {
68
- /**
69
- * The emote fetcher being used.
70
- * @type {EmoteFetcher}
71
- */
72
- this.fetcher = fetcher;
73
-
74
- /**
75
- * The parser options.
76
- * @type {object}
77
- */
78
- this.options = Object.assign({
79
- template: '',
80
- type: 'markdown',
81
- match: /:(.+?):/g
82
- }, options);
83
-
84
- this._validateOptions(this.options);
85
- }
86
-
87
- /**
88
- * Validates the parser options.
89
- * @private
90
- * @param {object} [options] - Options for the parser.
91
- * @param {string} [options.template] - The template to be used.
92
- * The strings that can be interpolated are:
93
- * - `{link}` The link of the emote.
94
- * - `{name}` The name of the emote.
95
- * - `{size}` The size of the image.
96
- * - `{creator}` The channel/owner name of the emote.
97
- * @param {string} [options.type] - The type of the parser.
98
- * Can be one of `markdown`, `html`, `bbcode`, or `plain`.
99
- * If the `template` option is provided, this is ignored.
100
- * @param {RegExp} [options.match] - The regular expression that matches an emote.
101
- * Must be a global regex, with one capture group for the emote code.
102
- * @throws {TypeError} When template is not a string.
103
- * @throws {TypeError} When type is not one of the supported types.
104
- * @throws {TypeError} When match is not a global RegExp.
105
- */
106
- _validateOptions(options) {
107
- if (options.template &amp;&amp; typeof options.template !== 'string') {
108
- throw new TypeError('Template must be a string');
109
- }
110
-
111
- if (!['markdown', 'html', 'bbcode', 'plain'].includes(options.type)) {
112
- throw new TypeError('Parse type must be one of `markdown`, `html`, `bbcode`, or `plain`');
113
- }
114
-
115
- if (!(options.match instanceof RegExp) || !options.match.global) {
116
- throw new TypeError('Match must be a global RegExp.');
117
- }
118
- }
119
-
120
- /**
121
- * Parses text.
122
- * @param {string} text - Text to parse.
123
- * @param {number} size - Size for emotes.
124
- * @returns {string}
125
- */
126
- parse(text, size = 0) {
127
- const parsed = text.replace(this.options.match, (matched, id) => {
128
- const emote = this.fetcher.emotes.get(id);
129
- if (!emote) return matched;
130
- if (emote.modifier) return '';
131
-
132
- const template = this.options.template || Constants.Templates[this.options.type];
133
- const link = emote.toLink(size);
134
- const res = template
135
- .replace(/{link}/g, link)
136
- .replace(/{name}/g, emote.code)
137
- .replace(/{size}/g, size)
138
- .replace(/{creator}/g, emote.ownerName || 'global');
139
-
140
- return res;
141
- });
142
-
143
- return parsed;
144
- }
145
- }
146
-
147
- module.exports = EmoteParser;
148
- </code></pre>
149
- </article>
150
- </section>
151
-
152
-
153
-
154
-
155
-
156
-
157
- </div>
158
-
159
- <br class="clear">
160
-
161
- <footer>
162
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
163
- </footer>
164
-
165
- <script>prettyPrint();</script>
166
- <script src="scripts/polyfill.js"></script>
167
- <script src="scripts/linenumber.js"></script>
168
-
169
-
170
-
171
- </body>
172
- </html>