perf: use logical assignments instead of if statements (#6693)

This commit is contained in:
Almeida
2021-10-03 13:59:57 +01:00
committed by GitHub
parent 9eb9591473
commit e9daa31eaf
9 changed files with 17 additions and 21 deletions

View File

@@ -7,7 +7,7 @@ module.exports = (client, { d: data }, shard) => {
if (client.user) { if (client.user) {
client.user._patch(data.user); client.user._patch(data.user);
} else { } else {
if (!ClientUser) ClientUser = require('../../../structures/ClientUser'); ClientUser ??= require('../../../structures/ClientUser');
client.user = new ClientUser(client, data.user); client.user = new ClientUser(client, data.user);
client.users.cache.set(client.user.id, client.user); client.users.cache.set(client.user.id, client.user);
} }

View File

@@ -93,9 +93,7 @@ class GuildBanManager extends CachedManager {
if (!options) return this._fetchMany(); if (!options) return this._fetchMany();
const user = this.client.users.resolveId(options); const user = this.client.users.resolveId(options);
if (user) return this._fetchSingle({ user, cache: true }); if (user) return this._fetchSingle({ user, cache: true });
if (options.user) { options.user &&= this.client.users.resolveId(options.user);
options.user = this.client.users.resolveId(options.user);
}
if (!options.user) { if (!options.user) {
if ('cache' in options) return this._fetchMany(options.cache); if ('cache' in options) return this._fetchMany(options.cache);
return Promise.reject(new Error('FETCH_BAN_RESOLVE_ID')); return Promise.reject(new Error('FETCH_BAN_RESOLVE_ID'));

View File

@@ -133,10 +133,8 @@ class GuildChannelManager extends CachedManager {
name, name,
{ type, topic, nsfw, bitrate, userLimit, parent, permissionOverwrites, position, rateLimitPerUser, reason } = {}, { type, topic, nsfw, bitrate, userLimit, parent, permissionOverwrites, position, rateLimitPerUser, reason } = {},
) { ) {
if (parent) parent = this.client.channels.resolveId(parent); parent &&= this.client.channels.resolveId(parent);
if (permissionOverwrites) { permissionOverwrites &&= permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild));
permissionOverwrites = permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild));
}
const data = await this.client.api.guilds(this.guild.id).channels.post({ const data = await this.client.api.guilds(this.guild.id).channels.post({
data: { data: {

View File

@@ -188,8 +188,8 @@ class GuildManager extends CachedManager {
explicitContentFilter = ExplicitContentFilterLevels[explicitContentFilter]; explicitContentFilter = ExplicitContentFilterLevels[explicitContentFilter];
} }
for (const channel of channels) { for (const channel of channels) {
if (channel.type) channel.type = ChannelTypes[channel.type.toUpperCase()]; channel.type &&= ChannelTypes[channel.type.toUpperCase()];
if (channel.type) channel.type = typeof channel.type === 'number' ? channel.type : ChannelTypes[channel.type]; channel.type &&= typeof channel.type === 'number' ? channel.type : ChannelTypes[channel.type];
channel.parent_id = channel.parentId; channel.parent_id = channel.parentId;
delete channel.parentId; delete channel.parentId;
channel.user_limit = channel.userLimit; channel.user_limit = channel.userLimit;
@@ -201,17 +201,17 @@ class GuildManager extends CachedManager {
if (typeof overwrite.type === 'string') { if (typeof overwrite.type === 'string') {
overwrite.type = OverwriteTypes[overwrite.type]; overwrite.type = OverwriteTypes[overwrite.type];
} }
if (overwrite.allow) overwrite.allow = Permissions.resolve(overwrite.allow).toString(); overwrite.allow &&= Permissions.resolve(overwrite.allow).toString();
if (overwrite.deny) overwrite.deny = Permissions.resolve(overwrite.deny).toString(); overwrite.deny &&= Permissions.resolve(overwrite.deny).toString();
} }
channel.permission_overwrites = channel.permissionOverwrites; channel.permission_overwrites = channel.permissionOverwrites;
delete channel.permissionOverwrites; delete channel.permissionOverwrites;
} }
for (const role of roles) { for (const role of roles) {
if (role.color) role.color = resolveColor(role.color); role.color &&= resolveColor(role.color);
if (role.permissions) role.permissions = Permissions.resolve(role.permissions).toString(); role.permissions &&= Permissions.resolve(role.permissions).toString();
} }
if (systemChannelFlags) systemChannelFlags = SystemChannelFlags.resolve(systemChannelFlags); systemChannelFlags &&= SystemChannelFlags.resolve(systemChannelFlags);
const data = await this.client.api.guilds.post({ const data = await this.client.api.guilds.post({
data: { data: {

View File

@@ -248,7 +248,7 @@ class GuildMemberManager extends CachedManager {
_data.channel_id = null; _data.channel_id = null;
_data.channel = undefined; _data.channel = undefined;
} }
if (_data.roles) _data.roles = _data.roles.map(role => (role instanceof Role ? role.id : role)); _data.roles &&= _data.roles.map(role => (role instanceof Role ? role.id : role));
let endpoint = this.client.api.guilds(this.guild.id); let endpoint = this.client.api.guilds(this.guild.id);
if (id === this.client.user.id) { if (id === this.client.user.id) {
const keys = Object.keys(_data); const keys = Object.keys(_data);

View File

@@ -129,7 +129,7 @@ class RoleManager extends CachedManager {
*/ */
async create(options = {}) { async create(options = {}) {
let { name, color, hoist, permissions, position, mentionable, reason } = options; let { name, color, hoist, permissions, position, mentionable, reason } = options;
if (color) color = resolveColor(color); color &&= resolveColor(color);
if (typeof permissions !== 'undefined') permissions = new Permissions(permissions); if (typeof permissions !== 'undefined') permissions = new Permissions(permissions);
const data = await this.client.api.guilds(this.guild.id).roles.post({ const data = await this.client.api.guilds(this.guild.id).roles.post({

View File

@@ -60,7 +60,7 @@ class StageInstanceManager extends CachedManager {
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true); if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
let { topic, privacyLevel } = options; let { topic, privacyLevel } = options;
if (privacyLevel) privacyLevel = typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel]; privacyLevel &&= typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel];
const data = await this.client.api['stage-instances'].post({ const data = await this.client.api['stage-instances'].post({
data: { data: {
@@ -122,7 +122,7 @@ class StageInstanceManager extends CachedManager {
let { topic, privacyLevel } = options; let { topic, privacyLevel } = options;
if (privacyLevel) privacyLevel = typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel]; privacyLevel &&= typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel];
const data = await this.client.api('stage-instances', channelId).patch({ const data = await this.client.api('stage-instances', channelId).patch({
data: { data: {

View File

@@ -294,7 +294,7 @@ class GuildChannel extends Channel {
* .catch(console.error); * .catch(console.error);
*/ */
async edit(data, reason) { async edit(data, reason) {
if (data.parent) data.parent = this.client.channels.resolveId(data.parent); data.parent &&= this.client.channels.resolveId(data.parent);
if (typeof data.position !== 'undefined') { if (typeof data.position !== 'undefined') {
const updatedChannels = await Util.setPosition( const updatedChannels = await Util.setPosition(

View File

@@ -245,7 +245,7 @@ class Webhook {
if (avatar && !(typeof avatar === 'string' && avatar.startsWith('data:'))) { if (avatar && !(typeof avatar === 'string' && avatar.startsWith('data:'))) {
avatar = await DataResolver.resolveImage(avatar); avatar = await DataResolver.resolveImage(avatar);
} }
if (channel) channel = channel?.id ?? channel; channel &&= channel.id ?? channel;
const data = await this.client.api.webhooks(this.id, channel ? undefined : this.token).patch({ const data = await this.client.api.webhooks(this.id, channel ? undefined : this.token).patch({
data: { name, avatar, channel_id: channel }, data: { name, avatar, channel_id: channel },
reason, reason,