From 264ee8e7f1760a16662640866028415ffe9f0c95 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 14 Dec 2016 23:10:34 +0100 Subject: [PATCH] Equals updates (#987) * Docs update Changed the param names and param descriptions to be consistent. * Added === comparison Changed Presence.equals and Game.equals to first compare using === * Collection.equals fix Now returns false when collection is undefined, instead of crashing :100: --- src/structures/GroupDMChannel.js | 2 +- src/structures/Guild.js | 2 +- src/structures/GuildChannel.js | 2 +- src/structures/Presence.js | 26 +++++++++++++------------- src/structures/Role.js | 2 +- src/structures/User.js | 2 +- src/util/Collection.js | 1 + 7 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/structures/GroupDMChannel.js b/src/structures/GroupDMChannel.js index 7f1ab8ea9..ada3a90f4 100644 --- a/src/structures/GroupDMChannel.js +++ b/src/structures/GroupDMChannel.js @@ -89,7 +89,7 @@ class GroupDMChannel extends Channel { * Whether this channel equals another channel. It compares all properties, so for most operations * it is advisable to just compare `channel.id === channel2.id` as it is much faster and is often * what most users need. - * @param {GroupDMChannel} channel The channel to compare to + * @param {GroupDMChannel} channel Channel to compare with * @returns {boolean} */ equals(channel) { diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 33cbb2425..2d679d117 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -692,7 +692,7 @@ class Guild { * Whether this Guild equals another Guild. It compares all properties, so for most operations * it is advisable to just compare `guild.id === guild2.id` as it is much faster and is often * what most users need. - * @param {Guild} guild The guild to compare + * @param {Guild} guild Guild to compare with * @returns {boolean} */ equals(guild) { diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index 807150e9a..a93078972 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -259,7 +259,7 @@ class GuildChannel extends Channel { /** * Checks if this channel has the same type, topic, position, name, overwrites and ID as another channel. * In most cases, a simple `channel.id === channel2.id` will do, and is much faster too. - * @param {GuildChannel} channel The channel to compare this channel to + * @param {GuildChannel} channel Channel to compare with * @returns {boolean} */ equals(channel) { diff --git a/src/structures/Presence.js b/src/structures/Presence.js index 05db21aaa..ddca5fb34 100644 --- a/src/structures/Presence.js +++ b/src/structures/Presence.js @@ -28,14 +28,14 @@ class Presence { /** * Whether this presence is equal to another - * @param {Presence} other the presence to compare + * @param {Presence} presence Presence to compare with * @returns {boolean} */ - equals(other) { - return ( - other && - this.status === other.status && - this.game ? this.game.equals(other.game) : !other.game + equals(presence) { + return this === presence || ( + presence && + this.status === presence.status && + this.game ? this.game.equals(presence.game) : !presence.game ); } } @@ -75,15 +75,15 @@ class Game { /** * Whether this game is equal to another game - * @param {Game} other the other game to compare + * @param {Game} game Game to compare with * @returns {boolean} */ - equals(other) { - return ( - other && - this.name === other.name && - this.type === other.type && - this.url === other.url + equals(game) { + return this === game || ( + game && + this.name === game.name && + this.type === game.type && + this.url === game.url ); } } diff --git a/src/structures/Role.js b/src/structures/Role.js index ce1b09025..5936da0fc 100644 --- a/src/structures/Role.js +++ b/src/structures/Role.js @@ -302,7 +302,7 @@ class Role { * Whether this role equals another role. It compares all properties, so for most operations * it is advisable to just compare `role.id === role2.id` as it is much faster and is often * what most users need. - * @param {Role} role The role to compare to + * @param {Role} role Role to compare with * @returns {boolean} */ equals(role) { diff --git a/src/structures/User.js b/src/structures/User.js index 0e1ce8036..ae0461590 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -230,7 +230,7 @@ class User { /** * Checks if the user is equal to another. It compares username, ID, discriminator, status and the game being played. * It is recommended to compare equality by using `user.id === user2.id` unless you want to compare all properties. - * @param {User} user The user to compare + * @param {User} user User to compare with * @returns {boolean} */ equals(user) { diff --git a/src/util/Collection.js b/src/util/Collection.js index 2b6ba1537..f90079bae 100644 --- a/src/util/Collection.js +++ b/src/util/Collection.js @@ -338,6 +338,7 @@ class Collection extends Map { * @returns {boolean} Whether the collections have identical contents */ equals(collection) { + if (!collection) return false; if (this === collection) return true; if (this.size !== collection.size) return false; return !this.find((value, key) => {