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
This commit is contained in:
Vlad Frangu
2019-04-29 19:49:41 +03:00
committed by Amish Shah
parent 9b0f4b298d
commit d7a9b74523
11 changed files with 26 additions and 25 deletions

View File

@@ -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);
};

View File

@@ -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)) {

View File

@@ -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)));

View File

@@ -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)) {

View File

@@ -65,7 +65,7 @@ class GuildMemberRoleStore extends Collection {
* @returns {Promise<GuildMember>}
*/
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<GuildMember>}
*/
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',

View File

@@ -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 };

View File

@@ -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)));
}
}

View File

@@ -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 =>

View File

@@ -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');
}

View File

@@ -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<Buffer>}
*/
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') {

View File

@@ -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];
}