Skip to content

Commit

Permalink
perf: use logical assignments instead of if statements (#6693)
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx committed Oct 3, 2021
1 parent 9eb9591 commit e9daa31
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/client/websocket/handlers/READY.js
Expand Up @@ -7,7 +7,7 @@ module.exports = (client, { d: data }, shard) => {
if (client.user) {
client.user._patch(data.user);
} else {
if (!ClientUser) ClientUser = require('../../../structures/ClientUser');
ClientUser ??= require('../../../structures/ClientUser');
client.user = new ClientUser(client, data.user);
client.users.cache.set(client.user.id, client.user);
}
Expand Down
4 changes: 1 addition & 3 deletions src/managers/GuildBanManager.js
Expand Up @@ -93,9 +93,7 @@ class GuildBanManager extends CachedManager {
if (!options) return this._fetchMany();
const user = this.client.users.resolveId(options);
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 ('cache' in options) return this._fetchMany(options.cache);
return Promise.reject(new Error('FETCH_BAN_RESOLVE_ID'));
Expand Down
6 changes: 2 additions & 4 deletions src/managers/GuildChannelManager.js
Expand Up @@ -133,10 +133,8 @@ class GuildChannelManager extends CachedManager {
name,
{ type, topic, nsfw, bitrate, userLimit, parent, permissionOverwrites, position, rateLimitPerUser, reason } = {},
) {
if (parent) parent = this.client.channels.resolveId(parent);
if (permissionOverwrites) {
permissionOverwrites = permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild));
}
parent &&= this.client.channels.resolveId(parent);
permissionOverwrites &&= permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild));

const data = await this.client.api.guilds(this.guild.id).channels.post({
data: {
Expand Down
14 changes: 7 additions & 7 deletions src/managers/GuildManager.js
Expand Up @@ -188,8 +188,8 @@ class GuildManager extends CachedManager {
explicitContentFilter = ExplicitContentFilterLevels[explicitContentFilter];
}
for (const channel of channels) {
if (channel.type) channel.type = ChannelTypes[channel.type.toUpperCase()];
if (channel.type) channel.type = typeof channel.type === 'number' ? channel.type : ChannelTypes[channel.type];
channel.type &&= ChannelTypes[channel.type.toUpperCase()];
channel.type &&= typeof channel.type === 'number' ? channel.type : ChannelTypes[channel.type];
channel.parent_id = channel.parentId;
delete channel.parentId;
channel.user_limit = channel.userLimit;
Expand All @@ -201,17 +201,17 @@ class GuildManager extends CachedManager {
if (typeof overwrite.type === 'string') {
overwrite.type = OverwriteTypes[overwrite.type];
}
if (overwrite.allow) overwrite.allow = Permissions.resolve(overwrite.allow).toString();
if (overwrite.deny) overwrite.deny = Permissions.resolve(overwrite.deny).toString();
overwrite.allow &&= Permissions.resolve(overwrite.allow).toString();
overwrite.deny &&= Permissions.resolve(overwrite.deny).toString();
}
channel.permission_overwrites = channel.permissionOverwrites;
delete channel.permissionOverwrites;
}
for (const role of roles) {
if (role.color) role.color = resolveColor(role.color);
if (role.permissions) role.permissions = Permissions.resolve(role.permissions).toString();
role.color &&= resolveColor(role.color);
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({
data: {
Expand Down
2 changes: 1 addition & 1 deletion src/managers/GuildMemberManager.js
Expand Up @@ -248,7 +248,7 @@ class GuildMemberManager extends CachedManager {
_data.channel_id = null;
_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);
if (id === this.client.user.id) {
const keys = Object.keys(_data);
Expand Down
2 changes: 1 addition & 1 deletion src/managers/RoleManager.js
Expand Up @@ -129,7 +129,7 @@ class RoleManager extends CachedManager {
*/
async create(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);

const data = await this.client.api.guilds(this.guild.id).roles.post({
Expand Down
4 changes: 2 additions & 2 deletions src/managers/StageInstanceManager.js
Expand Up @@ -60,7 +60,7 @@ class StageInstanceManager extends CachedManager {
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
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({
data: {
Expand Down Expand Up @@ -122,7 +122,7 @@ class StageInstanceManager extends CachedManager {

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({
data: {
Expand Down
2 changes: 1 addition & 1 deletion src/structures/GuildChannel.js
Expand Up @@ -294,7 +294,7 @@ class GuildChannel extends Channel {
* .catch(console.error);
*/
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') {
const updatedChannels = await Util.setPosition(
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Webhook.js
Expand Up @@ -245,7 +245,7 @@ class Webhook {
if (avatar && !(typeof avatar === 'string' && avatar.startsWith('data:'))) {
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({
data: { name, avatar, channel_id: channel },
reason,
Expand Down

0 comments on commit e9daa31

Please sign in to comment.