From d7a9b7452312a7309d4451dcde99533fec50d766 Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Mon, 29 Apr 2019 19:49:41 +0300 Subject: [PATCH] src: Replace instanceof Array checks with Array.isArray and instanceof Buffer with Buffer.isBuffer (#3227) * src: Replace instanceof Array checks with Array.isArray * src: Buffer.isBuffer instead of instanceof Buffer --- src/WebSocket.js | 2 +- src/client/Client.js | 8 ++++---- src/client/websocket/WebSocketManager.js | 5 +++-- src/stores/GuildEmojiRoleStore.js | 4 ++-- src/stores/GuildMemberRoleStore.js | 4 ++-- src/structures/APIMessage.js | 8 ++++---- src/structures/Webhook.js | 2 +- src/structures/interfaces/TextBasedChannel.js | 4 ++-- src/util/BitField.js | 6 +++--- src/util/DataResolver.js | 4 ++-- src/util/Util.js | 4 ++-- 11 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/WebSocket.js b/src/WebSocket.js index c8f73972a..f3f893b40 100644 --- a/src/WebSocket.js +++ b/src/WebSocket.js @@ -22,7 +22,7 @@ exports.pack = erlpack ? erlpack.pack : JSON.stringify; exports.unpack = data => { if (!erlpack || data[0] === '{') return JSON.parse(data); - if (!(data instanceof Buffer)) data = Buffer.from(new Uint8Array(data)); + if (!Buffer.isBuffer(data)) data = Buffer.from(new Uint8Array(data)); return erlpack.unpack(data); }; diff --git a/src/client/Client.js b/src/client/Client.js index bdbc442cc..58d3c7db1 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -49,7 +49,7 @@ class Client extends BaseClient { if (this.options.totalShardCount === DefaultOptions.totalShardCount) { if ('TOTAL_SHARD_COUNT' in data) { this.options.totalShardCount = Number(data.TOTAL_SHARD_COUNT); - } else if (this.options.shards instanceof Array) { + } else if (Array.isArray(this.options.shards)) { this.options.totalShardCount = this.options.shards.length; } else { this.options.totalShardCount = this.options.shardCount; @@ -365,7 +365,7 @@ class Client extends BaseClient { if (options.shardCount !== 'auto' && (typeof options.shardCount !== 'number' || isNaN(options.shardCount))) { throw new TypeError('CLIENT_INVALID_OPTION', 'shardCount', 'a number or "auto"'); } - if (options.shards && !(options.shards instanceof Array)) { + if (options.shards && !Array.isArray(options.shards)) { throw new TypeError('CLIENT_INVALID_OPTION', 'shards', 'a number or array'); } if (options.shards && !options.shards.length) throw new RangeError('CLIENT_INVALID_PROVIDED_SHARDS'); @@ -385,7 +385,7 @@ class Client extends BaseClient { if (typeof options.disableEveryone !== 'boolean') { throw new TypeError('CLIENT_INVALID_OPTION', 'disableEveryone', 'a boolean'); } - if (!(options.partials instanceof Array)) { + if (!Array.isArray(options.partials)) { throw new TypeError('CLIENT_INVALID_OPTION', 'partials', 'an Array'); } if (typeof options.restWsBridgeTimeout !== 'number' || isNaN(options.restWsBridgeTimeout)) { @@ -394,7 +394,7 @@ class Client extends BaseClient { if (typeof options.restSweepInterval !== 'number' || isNaN(options.restSweepInterval)) { throw new TypeError('CLIENT_INVALID_OPTION', 'restSweepInterval', 'a number'); } - if (!(options.disabledEvents instanceof Array)) { + if (!Array.isArray(options.disabledEvents)) { throw new TypeError('CLIENT_INVALID_OPTION', 'disabledEvents', 'an Array'); } if (typeof options.retryLimit !== 'number' || isNaN(options.retryLimit)) { diff --git a/src/client/websocket/WebSocketManager.js b/src/client/websocket/WebSocketManager.js index 23dbadce8..6dd1a3f22 100644 --- a/src/client/websocket/WebSocketManager.js +++ b/src/client/websocket/WebSocketManager.js @@ -160,8 +160,9 @@ class WebSocketManager extends EventEmitter { } } - if (this.client.options.shards instanceof Array) { - const { shards } = this.client.options; + const { shards } = this.client.options; + + if (Array.isArray(shards)) { this.totalShards = shards.length; this.debug(`Spawning shards: ${shards.join(', ')}`); this.shardQueue = new Set(shards.map(id => new WebSocketShard(this, id))); diff --git a/src/stores/GuildEmojiRoleStore.js b/src/stores/GuildEmojiRoleStore.js index 2051161d9..ad3793b29 100644 --- a/src/stores/GuildEmojiRoleStore.js +++ b/src/stores/GuildEmojiRoleStore.js @@ -33,7 +33,7 @@ class GuildEmojiRoleStore extends Collection { */ add(roleOrRoles) { if (roleOrRoles instanceof Collection) return this.add(roleOrRoles.keyArray()); - if (!(roleOrRoles instanceof Array)) return this.add([roleOrRoles]); + if (!Array.isArray(roleOrRoles)) return this.add([roleOrRoles]); roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolve(r)); if (roleOrRoles.includes(null)) { @@ -52,7 +52,7 @@ class GuildEmojiRoleStore extends Collection { */ remove(roleOrRoles) { if (roleOrRoles instanceof Collection) return this.remove(roleOrRoles.keyArray()); - if (!(roleOrRoles instanceof Array)) return this.remove([roleOrRoles]); + if (!Array.isArray(roleOrRoles)) return this.remove([roleOrRoles]); roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolveID(r)); if (roleOrRoles.includes(null)) { diff --git a/src/stores/GuildMemberRoleStore.js b/src/stores/GuildMemberRoleStore.js index 527706299..824bca686 100644 --- a/src/stores/GuildMemberRoleStore.js +++ b/src/stores/GuildMemberRoleStore.js @@ -65,7 +65,7 @@ class GuildMemberRoleStore extends Collection { * @returns {Promise} */ async add(roleOrRoles, reason) { - if (roleOrRoles instanceof Collection || roleOrRoles instanceof Array) { + if (roleOrRoles instanceof Collection || Array.isArray(roleOrRoles)) { roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolve(r)); if (roleOrRoles.includes(null)) { throw new TypeError('INVALID_TYPE', 'roles', @@ -96,7 +96,7 @@ class GuildMemberRoleStore extends Collection { * @returns {Promise} */ async remove(roleOrRoles, reason) { - if (roleOrRoles instanceof Collection || roleOrRoles instanceof Array) { + if (roleOrRoles instanceof Collection || Array.isArray(roleOrRoles)) { roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolve(r)); if (roleOrRoles.includes(null)) { throw new TypeError('INVALID_TYPE', 'roles', diff --git a/src/structures/APIMessage.js b/src/structures/APIMessage.js index e6d67b686..a925892d0 100644 --- a/src/structures/APIMessage.js +++ b/src/structures/APIMessage.js @@ -198,7 +198,7 @@ class APIMessage { split() { if (!this.data) this.resolveData(); - if (!(this.data.content instanceof Array)) return [this]; + if (!Array.isArray(this.data.content)) return [this]; const apiMessages = []; @@ -287,7 +287,7 @@ class APIMessage { * @returns {MessageOptions|WebhookMessageOptions} */ static transformOptions(content, options, extra = {}, isWebhook = false) { - if (!options && typeof content === 'object' && !(content instanceof Array)) { + if (!options && typeof content === 'object' && !Array.isArray(content)) { options = content; content = undefined; } @@ -300,10 +300,10 @@ class APIMessage { return { content, files: [options], ...extra }; } - if (options instanceof Array) { + if (Array.isArray(options)) { const [embeds, files] = this.partitionMessageAdditions(options); return isWebhook ? { content, embeds, files, ...extra } : { content, embed: embeds[0], files, ...extra }; - } else if (content instanceof Array) { + } else if (Array.isArray(content)) { const [embeds, files] = this.partitionMessageAdditions(content); if (embeds.length || files.length) { return isWebhook ? { embeds, files, ...extra } : { embed: embeds[0], files, ...extra }; diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index 1ad7cba93..6c1a3db50 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -135,7 +135,7 @@ class Webhook { apiMessage = content.resolveData(); } else { apiMessage = APIMessage.create(this, content, options).resolveData(); - if (apiMessage.data.content instanceof Array) { + if (Array.isArray(apiMessage.data.content)) { return Promise.all(apiMessage.split().map(this.send.bind(this))); } } diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index 075bc6752..634dff33a 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -138,7 +138,7 @@ class TextBasedChannel { apiMessage = content.resolveData(); } else { apiMessage = APIMessage.create(this, content, options).resolveData(); - if (apiMessage.data.content instanceof Array) { + if (Array.isArray(apiMessage.data.content)) { return Promise.all(apiMessage.split().map(this.send.bind(this))); } } @@ -296,7 +296,7 @@ class TextBasedChannel { * .catch(console.error); */ async bulkDelete(messages, filterOld = false) { - if (messages instanceof Array || messages instanceof Collection) { + if (Array.isArray(messages) || messages instanceof Collection) { let messageIDs = messages instanceof Collection ? messages.keyArray() : messages.map(m => m.id || m); if (filterOld) { messageIDs = messageIDs.filter(id => diff --git a/src/util/BitField.js b/src/util/BitField.js index 96b07b829..424a8443d 100644 --- a/src/util/BitField.js +++ b/src/util/BitField.js @@ -32,7 +32,7 @@ class BitField { * @returns {boolean} */ has(bit) { - if (bit instanceof Array) return bit.every(p => this.has(p)); + if (Array.isArray(bit)) return bit.every(p => this.has(p)); bit = this.constructor.resolve(bit); return (this.bitfield & bit) === bit; } @@ -44,7 +44,7 @@ class BitField { * @returns {string[]} */ missing(bits, ...hasParams) { - if (!(bits instanceof Array)) bits = new this.constructor(bits).toArray(false); + if (!Array.isArray(bits)) bits = new this.constructor(bits).toArray(false); return bits.filter(p => !this.has(p, ...hasParams)); } @@ -136,7 +136,7 @@ class BitField { static resolve(bit = 0) { if (typeof bit === 'number' && bit >= 0) return bit; if (bit instanceof BitField) return bit.bitfield; - if (bit instanceof Array) return bit.map(p => this.resolve(p)).reduce((prev, p) => prev | p, 0); + if (Array.isArray(bit)) return bit.map(p => this.resolve(p)).reduce((prev, p) => prev | p, 0); if (typeof bit === 'string') return this.FLAGS[bit]; throw new RangeError('BITFIELD_INVALID'); } diff --git a/src/util/DataResolver.js b/src/util/DataResolver.js index 1b9057270..91933eb92 100644 --- a/src/util/DataResolver.js +++ b/src/util/DataResolver.js @@ -62,7 +62,7 @@ class DataResolver { * @returns {?string} */ static resolveBase64(data) { - if (data instanceof Buffer) return `data:image/jpg;base64,${data.toString('base64')}`; + if (Buffer.isBuffer(data)) return `data:image/jpg;base64,${data.toString('base64')}`; return data; } @@ -85,7 +85,7 @@ class DataResolver { * @returns {Promise} */ static resolveFile(resource) { - if (!browser && resource instanceof Buffer) return Promise.resolve(resource); + if (!browser && Buffer.isBuffer(resource)) return Promise.resolve(resource); if (browser && resource instanceof ArrayBuffer) return Promise.resolve(Util.convertToBuffer(resource)); if (typeof resource === 'string') { diff --git a/src/util/Util.js b/src/util/Util.js index 828569490..181d5589e 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -237,7 +237,7 @@ class Util { */ static resolveString(data) { if (typeof data === 'string') return data; - if (data instanceof Array) return data.join('\n'); + if (Array.isArray(data)) return data.join('\n'); return String(data); } @@ -286,7 +286,7 @@ class Util { if (color === 'RANDOM') return Math.floor(Math.random() * (0xFFFFFF + 1)); if (color === 'DEFAULT') return 0; color = Colors[color] || parseInt(color.replace('#', ''), 16); - } else if (color instanceof Array) { + } else if (Array.isArray(color)) { color = (color[0] << 16) + (color[1] << 8) + color[2]; }