@mkody/twitch-emoticons 2.7.0 → 2.8.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.
Files changed (45) hide show
  1. package/.eslintrc.json +143 -143
  2. package/.github/codeql/codeql-config.yml +4 -4
  3. package/.github/workflows/codeql.yml +50 -50
  4. package/.github/workflows/eslint.yml +50 -50
  5. package/.github/workflows/release.yml +22 -0
  6. package/.github/workflows/yarn-test.yml +17 -17
  7. package/.jsdoc.json +39 -39
  8. package/.nvmrc +1 -0
  9. package/LICENSE +22 -22
  10. package/README.md +36 -4
  11. package/docs/BTTVEmote.html +1641 -1319
  12. package/docs/Channel.html +823 -823
  13. package/docs/Collection.html +797 -797
  14. package/docs/Emote.html +906 -801
  15. package/docs/EmoteFetcher.html +3680 -2946
  16. package/docs/EmoteParser.html +811 -811
  17. package/docs/FFZEmote.html +1795 -1473
  18. package/docs/SevenTV.html +1190 -1190
  19. package/docs/SevenTVEmote.html +1705 -1395
  20. package/docs/TwitchEmote.html +1637 -1315
  21. package/docs/index.html +230 -206
  22. package/docs/struct_BTTVEmote.js.html +162 -132
  23. package/docs/struct_Channel.js.html +127 -127
  24. package/docs/struct_Emote.js.html +151 -138
  25. package/docs/struct_EmoteFetcher.js.html +427 -399
  26. package/docs/struct_EmoteParser.js.html +153 -153
  27. package/docs/struct_FFZEmote.js.html +185 -146
  28. package/docs/struct_SevenTVEmote.js.html +178 -141
  29. package/docs/struct_TwitchEmote.js.html +159 -130
  30. package/docs/util_Collection.js.html +150 -150
  31. package/package.json +57 -57
  32. package/src/index.js +12 -12
  33. package/src/struct/BTTVEmote.js +90 -60
  34. package/src/struct/Channel.js +55 -55
  35. package/src/struct/Emote.js +79 -66
  36. package/src/struct/EmoteFetcher.js +355 -327
  37. package/src/struct/EmoteParser.js +81 -81
  38. package/src/struct/EmoteTypeMapper.js +13 -0
  39. package/src/struct/FFZEmote.js +113 -74
  40. package/src/struct/SevenTVEmote.js +106 -69
  41. package/src/struct/TwitchEmote.js +87 -58
  42. package/src/util/Collection.js +78 -78
  43. package/src/util/Constants.js +31 -31
  44. package/test/index.js +482 -174
  45. package/typings/index.d.ts +115 -98
@@ -1,81 +1,81 @@
1
- const Constants = require('../util/Constants');
2
-
3
- class EmoteParser {
4
- /**
5
- * A parser to replace text with emotes.
6
- * @param {EmoteFetcher} fetcher - The fetcher to use the cache of.
7
- * @param {Object} [options={}] - Options for the parser.
8
- * @param {string} [options.template] - The template to be used.
9
- * The strings that can be interpolated are:
10
- * - `{link}` The link of the emote.
11
- * - `{name}` The name of the emote.
12
- * - `{size}` The size of the image.
13
- * - `{creator}` The channel/owner name of the emote.
14
- * @param {string} [options.type='markdown'] - The type of the parser.
15
- * Can be one of `markdown`, `html`, `bbcode`, or `plain`.
16
- * If the `template` option is provided, this is ignored.
17
- * @param {RegExp} [options.match=/:(.+?):/g] - The regular expression that matches an emote.
18
- * Must be a global regex, with one capture group for the emote code.
19
- */
20
- constructor(fetcher, options = {}) {
21
- /**
22
- * The emote fetcher being used.
23
- * @type {EmoteFetcher}
24
- */
25
- this.fetcher = fetcher;
26
-
27
- /**
28
- * The parser options.
29
- * @type {Object}
30
- */
31
- this.options = Object.assign({
32
- template: '',
33
- type: 'markdown',
34
- match: /:(.+?):/g
35
- }, options);
36
-
37
- this._validateOptions(this.options);
38
- }
39
-
40
- _validateOptions(options) {
41
- if (options.template && typeof options.template !== 'string') {
42
- throw new TypeError('Template must be a string');
43
- }
44
-
45
- if (!['markdown', 'html', 'bbcode', 'plain'].includes(options.type)) {
46
- throw new TypeError('Parse type must be one of `markdown`, `html`, `bbcode`, or `plain`');
47
- }
48
-
49
- if (!(options.match instanceof RegExp) || !options.match.global) {
50
- throw new TypeError('Match must be a global RegExp.');
51
- }
52
- }
53
-
54
- /**
55
- * Parses text.
56
- * @param {string} text - Text to parse.
57
- * @param {number} size - Size for emotes.
58
- * @returns {string}
59
- */
60
- parse(text, size = 0) {
61
- const parsed = text.replace(this.options.match, (matched, id) => {
62
- const emote = this.fetcher.emotes.get(id);
63
- if (!emote) return matched;
64
- if (emote.modifier) return '';
65
-
66
- const template = this.options.template || Constants.Templates[this.options.type];
67
- const link = emote.toLink(size);
68
- const res = template
69
- .replace(/{link}/g, link)
70
- .replace(/{name}/g, emote.code)
71
- .replace(/{size}/g, size)
72
- .replace(/{creator}/g, emote.ownerName || 'global');
73
-
74
- return res;
75
- });
76
-
77
- return parsed;
78
- }
79
- }
80
-
81
- module.exports = EmoteParser;
1
+ const Constants = require('../util/Constants');
2
+
3
+ class EmoteParser {
4
+ /**
5
+ * A parser to replace text with emotes.
6
+ * @param {EmoteFetcher} fetcher - The fetcher to use the cache of.
7
+ * @param {Object} [options={}] - Options for the parser.
8
+ * @param {string} [options.template] - The template to be used.
9
+ * The strings that can be interpolated are:
10
+ * - `{link}` The link of the emote.
11
+ * - `{name}` The name of the emote.
12
+ * - `{size}` The size of the image.
13
+ * - `{creator}` The channel/owner name of the emote.
14
+ * @param {string} [options.type='markdown'] - The type of the parser.
15
+ * Can be one of `markdown`, `html`, `bbcode`, or `plain`.
16
+ * If the `template` option is provided, this is ignored.
17
+ * @param {RegExp} [options.match=/:(.+?):/g] - The regular expression that matches an emote.
18
+ * Must be a global regex, with one capture group for the emote code.
19
+ */
20
+ constructor(fetcher, options = {}) {
21
+ /**
22
+ * The emote fetcher being used.
23
+ * @type {EmoteFetcher}
24
+ */
25
+ this.fetcher = fetcher;
26
+
27
+ /**
28
+ * The parser options.
29
+ * @type {Object}
30
+ */
31
+ this.options = Object.assign({
32
+ template: '',
33
+ type: 'markdown',
34
+ match: /:(.+?):/g
35
+ }, options);
36
+
37
+ this._validateOptions(this.options);
38
+ }
39
+
40
+ _validateOptions(options) {
41
+ if (options.template && typeof options.template !== 'string') {
42
+ throw new TypeError('Template must be a string');
43
+ }
44
+
45
+ if (!['markdown', 'html', 'bbcode', 'plain'].includes(options.type)) {
46
+ throw new TypeError('Parse type must be one of `markdown`, `html`, `bbcode`, or `plain`');
47
+ }
48
+
49
+ if (!(options.match instanceof RegExp) || !options.match.global) {
50
+ throw new TypeError('Match must be a global RegExp.');
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Parses text.
56
+ * @param {string} text - Text to parse.
57
+ * @param {number} size - Size for emotes.
58
+ * @returns {string}
59
+ */
60
+ parse(text, size = 0) {
61
+ const parsed = text.replace(this.options.match, (matched, id) => {
62
+ const emote = this.fetcher.emotes.get(id);
63
+ if (!emote) return matched;
64
+ if (emote.modifier) return '';
65
+
66
+ const template = this.options.template || Constants.Templates[this.options.type];
67
+ const link = emote.toLink(size);
68
+ const res = template
69
+ .replace(/{link}/g, link)
70
+ .replace(/{name}/g, emote.code)
71
+ .replace(/{size}/g, size)
72
+ .replace(/{creator}/g, emote.ownerName || 'global');
73
+
74
+ return res;
75
+ });
76
+
77
+ return parsed;
78
+ }
79
+ }
80
+
81
+ module.exports = EmoteParser;
@@ -0,0 +1,13 @@
1
+ const BTTVEmote = require('./BTTVEmote');
2
+ const FFZEmote = require('./FFZEmote');
3
+ const SevenTVEmote = require('./SevenTVEmote');
4
+ const TwitchEmote = require('./TwitchEmote');
5
+
6
+ class EmoteTypeMapper {
7
+ static getClassByType(type) {
8
+ const classMap = { BTTVEmote, FFZEmote, SevenTVEmote, TwitchEmote };
9
+ return classMap[type] || null;
10
+ }
11
+ }
12
+
13
+ module.exports = EmoteTypeMapper;
@@ -1,74 +1,113 @@
1
- const Emote = require('./Emote');
2
- const Constants = require('../util/Constants');
3
-
4
- /** @extends Emote */
5
- class FFZEmote extends Emote {
6
- /**
7
- * An FFZ emote.
8
- * @param {Channel} channel - Channel this emote belongs to.
9
- * @param {string} id - ID of the emote.
10
- * @param {data} data - The raw emote data.
11
- */
12
- constructor(channel, id, data) {
13
- super(channel, id, data);
14
- this.type = 'ffz';
15
- }
16
-
17
- /**
18
- * The channel of this emote's creator.
19
- * Not guaranteed to contain the emote, or be cached.
20
- * @readonly
21
- * @type {?Channel}
22
- */
23
- get owner() {
24
- return this.fetcher.channels.get(this.ownerName);
25
- }
26
-
27
- _setup(data) {
28
- super._setup(data);
29
- this.code = data.name;
30
-
31
- /**
32
- * The name of the emote creator's channel.
33
- * @type {string}
34
- */
35
- this.ownerName = data.owner.name;
36
-
37
- /**
38
- * Available image sizes.
39
- * @type {string[]}
40
- */
41
- this.sizes = 'animated' in data ? Object.keys(data.animated) : Object.keys(data.urls);
42
-
43
- /**
44
- * If emote is animated.
45
- * @type {boolean}
46
- */
47
- this.animated = 'animated' in data;
48
-
49
- /**
50
- * The image type of the emote.
51
- * @type {string}
52
- */
53
- this.imageType = 'animated' in data ? 'webp' : 'png';
54
-
55
- /**
56
- * If the emote is a modifier and should be hidden.
57
- * @type {boolean}
58
- */
59
- this.modifier = data.modifier && (data.modifier_flags & 1) !== 0;
60
- }
61
-
62
- /**
63
- * Gets the image link of the emote.
64
- * @param {number} size - The size of the image.
65
- * @returns {string}
66
- */
67
- toLink(size = 0) {
68
- size = this.sizes[size];
69
- if (this.animated) return Constants.FFZ.CDNAnimated(this.id, size); // eslint-disable-line new-cap
70
- return Constants.FFZ.CDN(this.id, size); // eslint-disable-line new-cap
71
- }
72
- }
73
-
74
- module.exports = FFZEmote;
1
+ const Emote = require('./Emote');
2
+ const Constants = require('../util/Constants');
3
+
4
+ /** @extends Emote */
5
+ class FFZEmote extends Emote {
6
+ /**
7
+ * An FFZ emote.
8
+ * @param {Channel} channel - Channel this emote belongs to.
9
+ * @param {string} id - ID of the emote.
10
+ * @param {data} data - The raw emote data.
11
+ */
12
+ constructor(channel, id, data) {
13
+ super(channel, id, data);
14
+ this.type = 'ffz';
15
+ }
16
+
17
+ /**
18
+ * The channel of this emote's creator.
19
+ * Not guaranteed to contain the emote, or be cached.
20
+ * @readonly
21
+ * @type {?Channel}
22
+ */
23
+ get owner() {
24
+ return this.fetcher.channels.get(this.ownerName);
25
+ }
26
+
27
+ _setup(data) {
28
+ super._setup(data);
29
+ this.code = data.name;
30
+
31
+ /**
32
+ * The name of the emote creator's channel.
33
+ * @type {?string}
34
+ */
35
+ this.ownerName = 'owner' in data ? data.owner.name : null;
36
+
37
+ /**
38
+ * Available image sizes.
39
+ * @type {string[]}
40
+ */
41
+ this.sizes = 'animated' in data ? Object.keys(data.animated) : Object.keys(data.urls);
42
+
43
+ /**
44
+ * If emote is animated.
45
+ * @type {boolean}
46
+ */
47
+ this.animated = 'animated' in data;
48
+
49
+ /**
50
+ * The image type of the emote.
51
+ * @type {string}
52
+ */
53
+ this.imageType = 'animated' in data ? 'webp' : 'png';
54
+
55
+ /**
56
+ * If the emote is a modifier and should be hidden.
57
+ * @type {boolean}
58
+ */
59
+ this.modifier = data.modifier && (data.modifier_flags & 1) !== 0;
60
+ }
61
+
62
+ /**
63
+ * Gets the image link of the emote.
64
+ * @param {number} size - The size of the image.
65
+ * @returns {string}
66
+ */
67
+ toLink(size = 0) {
68
+ size = this.sizes[size];
69
+ if (this.animated) return Constants.FFZ.CDNAnimated(this.id, size); // eslint-disable-line new-cap
70
+ return Constants.FFZ.CDN(this.id, size); // eslint-disable-line new-cap
71
+ }
72
+
73
+ /**
74
+ * Override for `toObject`.
75
+ * Will result in an Object representation of a FFZEmote
76
+ * @returns {Object}
77
+ */
78
+ toObject() {
79
+ return Object.assign({}, super.toObject(), {
80
+ animated: this.animated,
81
+ sizes: this.sizes,
82
+ ownerName: this.ownerName,
83
+ type: this.type,
84
+ modifier: this.modifier
85
+ });
86
+ }
87
+
88
+ /**
89
+ * Converts an emote Object into a FFZEmote
90
+ * @param {Object} [emoteObject] - Object representation of this emote
91
+ * @param {Channel} [channel=null] - Channel this emote belongs to.
92
+ * @returns {FFZEmote}
93
+ */
94
+ static fromObject(emoteObject, channel = null) {
95
+ // Need to convert sizes array to object, e.g. [1, 2, 4] -> { 1: '1', 2: '2', 4: '4' }
96
+ const sizes_obj = emoteObject.sizes.reduce((acc, curr) => {
97
+ acc[curr] = curr;
98
+ return acc;
99
+ }, {});
100
+ return new FFZEmote(channel, emoteObject.id,
101
+ {
102
+ code: emoteObject.code,
103
+ name: emoteObject.code,
104
+ urls: sizes_obj,
105
+ ...emoteObject.animated ? { animated: sizes_obj } : {},
106
+ owner: { name: emoteObject.ownerName },
107
+ modifier: emoteObject.modifier,
108
+ modifier_flags: emoteObject.modifier
109
+ });
110
+ }
111
+ }
112
+
113
+ module.exports = FFZEmote;
@@ -1,69 +1,106 @@
1
- const Emote = require('./Emote');
2
- const Constants = require('../util/Constants');
3
-
4
- /** @extends Emote */
5
- class SevenTVEmote extends Emote {
6
- /**
7
- * A 7TV emote.
8
- * @param {Channel} channel - Channel this emote belongs to.
9
- * @param {string} id - ID of the emote.
10
- * @param {data} data - The raw emote data.
11
- */
12
- constructor(channel, id, data) {
13
- super(channel, id, data);
14
- this.type = '7tv';
15
- }
16
-
17
- /**
18
- * The channel of this emote's creator.
19
- * Not guaranteed to contain the emote, or be cached.
20
- * @readonly
21
- * @type {?Channel}
22
- */
23
- get owner() {
24
- return this.fetcher.channels.get(this.ownerName);
25
- }
26
-
27
- _setup(data) {
28
- super._setup(data);
29
- this.code = data.name;
30
-
31
- /**
32
- * The name of the emote creator's channel.
33
- * @type {string}
34
- */
35
- this.ownerName = data.owner.display_name;
36
-
37
- /**
38
- * Available image sizes.
39
- * @type {string[]}
40
- */
41
- this.sizes = data.host.files
42
- .filter(el => el.format === this.channel.format.toUpperCase())
43
- .map(el => el.name);
44
-
45
- /**
46
- * If emote is animated.
47
- * @type {boolean}
48
- */
49
- this.animated = data.animated;
50
-
51
- /**
52
- * The image type of the emote.
53
- * @type {string}
54
- */
55
- this.imageType = this.channel.format;
56
- }
57
-
58
- /**
59
- * Gets the image link of the emote.
60
- * @param {number} size - The size of the image.
61
- * @returns {string}
62
- */
63
- toLink(size = 0) {
64
- size = this.sizes[size];
65
- return Constants.SevenTV.CDN(this.id, size); // eslint-disable-line new-cap
66
- }
67
- }
68
-
69
- module.exports = SevenTVEmote;
1
+ const Emote = require('./Emote');
2
+ const Constants = require('../util/Constants');
3
+
4
+ /** @extends Emote */
5
+ class SevenTVEmote extends Emote {
6
+ /**
7
+ * A 7TV emote.
8
+ * @param {Channel} channel - Channel this emote belongs to.
9
+ * @param {string} id - ID of the emote.
10
+ * @param {data} data - The raw emote data.
11
+ */
12
+ constructor(channel, id, data) {
13
+ super(channel, id, data);
14
+ this.type = '7tv';
15
+ }
16
+
17
+ /**
18
+ * The channel of this emote's creator.
19
+ * Not guaranteed to contain the emote, or be cached.
20
+ * @readonly
21
+ * @type {?Channel}
22
+ */
23
+ get owner() {
24
+ return this.fetcher.channels.get(this.ownerName);
25
+ }
26
+
27
+ _setup(data) {
28
+ super._setup(data);
29
+ this.code = data.name;
30
+
31
+ /**
32
+ * The name of the emote creator's channel.
33
+ * @type {?string}
34
+ */
35
+ this.ownerName = 'owner' in data ? data.owner.display_name : null;
36
+
37
+ /**
38
+ * Available image sizes.
39
+ * @type {string[]}
40
+ */
41
+ this.sizes = data.host.files
42
+ .filter(el => el.format === this.channel.format.toUpperCase())
43
+ .map(el => el.name);
44
+
45
+ /**
46
+ * If emote is animated.
47
+ * @type {boolean}
48
+ */
49
+ this.animated = data.animated;
50
+
51
+ /**
52
+ * The image type of the emote.
53
+ * @type {string}
54
+ */
55
+ this.imageType = this.channel.format;
56
+ }
57
+
58
+ /**
59
+ * Gets the image link of the emote.
60
+ * @param {number} size - The size of the image.
61
+ * @returns {string}
62
+ */
63
+ toLink(size = 0) {
64
+ size = this.sizes[size];
65
+ return Constants.SevenTV.CDN(this.id, size); // eslint-disable-line new-cap
66
+ }
67
+
68
+ /**
69
+ * Override for `toObject`.
70
+ * Will result in an Object representation of a SevenTVEmote
71
+ * @returns {Object}
72
+ */
73
+ toObject() {
74
+ return Object.assign({}, super.toObject(), {
75
+ animated: this.animated,
76
+ sizes: this.sizes,
77
+ ownerName: this.ownerName,
78
+ type: this.type,
79
+ imageType: this.imageType
80
+ });
81
+ }
82
+
83
+ /**
84
+ * Converts an emote Object into a SevenTVEmote
85
+ * @param {Object} [emoteObject] - Object representation of this emote
86
+ * @param {Channel} [channel] - Channel this emote belongs to.
87
+ * @returns {SevenTVEmote}
88
+ */
89
+ static fromObject(emoteObject, channel) {
90
+ const sizes = emoteObject.sizes.map(size => { return { format: channel.format.toUpperCase(), name: size }; });
91
+ return new SevenTVEmote(channel, emoteObject.id,
92
+ {
93
+ code: emoteObject.code,
94
+ name: emoteObject.code,
95
+ animated: emoteObject.animated,
96
+ owner: {
97
+ display_name: emoteObject.ownerName
98
+ },
99
+ host: {
100
+ files: sizes
101
+ }
102
+ });
103
+ }
104
+ }
105
+
106
+ module.exports = SevenTVEmote;