Skip to content

Commit

Permalink
Fix falsy properties on inspect/toJSON methods (#400)
Browse files Browse the repository at this point in the history
* Don't omit falsy values from structure inspection

Also fixes an issue with toJSON for messages having a typo'd prop name

* Fix Message#editedTimestamp being NaN
  • Loading branch information
eritbh authored and abalabahaha committed Jul 16, 2018
1 parent bec945f commit 94019f1
Show file tree
Hide file tree
Showing 23 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/structures/Base.js
Expand Up @@ -58,7 +58,7 @@ class Base {
// http://stackoverflow.com/questions/5905492/dynamic-function-name-in-javascript
var copy = new (new Function(`return function ${this.constructor.name}(){}`)());
for(var key in this) {
if(this.hasOwnProperty(key) && !key.startsWith("_") && this[key]) {
if(this.hasOwnProperty(key) && !key.startsWith("_") && this[key] !== undefined) {
copy[key] = this[key];
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/structures/Call.js
Expand Up @@ -51,10 +51,10 @@ class Call extends Base {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["endedTimestamp", "participants", "region", "ringing", "unavailable", "voiceStates"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
}

module.exports = Call;
module.exports = Call;
2 changes: 1 addition & 1 deletion lib/structures/CategoryChannel.js
Expand Up @@ -36,7 +36,7 @@ class CategoryChannel extends GuildChannel {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["channels"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/Channel.js
Expand Up @@ -22,7 +22,7 @@ class Channel extends Base {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["type"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/ExtendedUser.js
Expand Up @@ -26,7 +26,7 @@ class ExtendedUser extends User {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["email", "mfaEnabled", "premium" ,"verified"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/GroupChannel.js
Expand Up @@ -87,7 +87,7 @@ class GroupChannel extends PrivateChannel { // (╯°□°)╯︵ ┻━┻
toJSON() {
var base = super.toJSON(true);
for(var prop of ["icon", "name", "ownerID", "recipients"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/Guild.js
Expand Up @@ -543,7 +543,7 @@ class Guild extends Base {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["afkChannelID", "afkTimeout", "channels", "defaultNotifications", "emojis", "explicitContentFilter", "features", "icon", "joinedAt", "large", "maxPresences", "memberCount", "members", "mfaLevel", "name", "ownerID", "region", "roles", "splash", "unavailable", "verificationLevel"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/GuildAuditLogEntry.js
Expand Up @@ -118,7 +118,7 @@ class GuildAuditLogEntry extends Base {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["actionType", "after", "before", "channel", "count", "deleteMemberDays", "member", "membersRemoved", "reason", "role", "targetID", "user"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/GuildChannel.js
Expand Up @@ -136,7 +136,7 @@ class GuildChannel extends Channel {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["name", "nsfw", "parentID", "permissionOverwrites", "position"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/structures/GuildIntegration.js
Expand Up @@ -74,10 +74,10 @@ class GuildIntegration extends Base {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["account", "enabled", "enableEmoticons", "expireBehavior", "expireGracePeriod", "name", "roleID", "subscriberCount", "syncedAt", "syncing", "type", "user"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
}

module.exports = GuildIntegration;
module.exports = GuildIntegration;
2 changes: 1 addition & 1 deletion lib/structures/Invite.js
Expand Up @@ -70,7 +70,7 @@ class Invite extends Base {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["channel", "code", "createdAt", "guild", "maxAge", "maxUses", "memberCount", "presenceCount", "revoked", "temporary", "uses"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/Member.js
Expand Up @@ -196,7 +196,7 @@ class Member extends Base {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["game", "joinedAt", "nick", "roles", "status", "user", "voiceState"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/structures/Message.js
Expand Up @@ -108,7 +108,7 @@ class Message extends Base {
}

this.pinned = data.pinned !== undefined ? !!data.pinned : this.pinned;
this.editedTimestamp = data.edited_timestamp !== undefined ? Date.parse(data.edited_timestamp) : this.editedTimestamp;
this.editedTimestamp = data.edited_timestamp !== null ? Date.parse(data.edited_timestamp) : this.editedTimestamp;
this.tts = data.tts !== undefined ? data.tts : this.tts;
this.attachments = data.attachments !== undefined ? data.attachments : this.attachments;
this.embeds = data.embeds !== undefined ? data.embeds : this.embeds;
Expand Down Expand Up @@ -260,8 +260,8 @@ class Message extends Base {

toJSON() {
var base = super.toJSON(true);
for(var prop of ["attachments", "author", "content", "editTimestamp", "embeds", "hit", "mentionEveryone", "mentions", "pinned", "reactions", "roleMentions", "timestamp", "tts", "type"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
for(var prop of ["attachments", "author", "content", "editedTimestamp", "embeds", "hit", "mentionEveryone", "mentions", "pinned", "reactions", "roleMentions", "timestamp", "tts", "type"]) {
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/structures/Permission.js
Expand Up @@ -58,10 +58,10 @@ class Permission extends Base {
delete base.id;
}
for(var prop of ["allow", "deny"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
}

module.exports = Permission;
module.exports = Permission;
2 changes: 1 addition & 1 deletion lib/structures/PermissionOverwrite.js
Expand Up @@ -18,7 +18,7 @@ class PermissionOverwrite extends Permission {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["type"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/structures/PrivateChannel.js
Expand Up @@ -207,10 +207,10 @@ class PrivateChannel extends Channel {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["call", "lastCall", "lastMessageID", "messages", "recipient"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
}

module.exports = PrivateChannel;
module.exports = PrivateChannel;
2 changes: 1 addition & 1 deletion lib/structures/Relationship.js
Expand Up @@ -29,7 +29,7 @@ class Relationship extends Base {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["game", "status", "type", "user"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/Role.js
Expand Up @@ -83,7 +83,7 @@ class Role extends Base {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["color", "hoist", "managed", "mentionable", "name", "permissions", "position"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/TextChannel.js
Expand Up @@ -253,7 +253,7 @@ class TextChannel extends GuildChannel {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["lastMessageID", "lastPinTimestamp", "messages", "topic"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/UnavailableGuild.js
Expand Up @@ -18,7 +18,7 @@ class UnavailableGuild extends Base {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["unavailable"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/User.js
Expand Up @@ -133,7 +133,7 @@ class User extends Base {
toJSON() {
var base = super.toJSON(true);
for(var prop of ["avatar", "bot", "discriminator", "username"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/VoiceChannel.js
Expand Up @@ -77,7 +77,7 @@ class VoiceChannel extends GuildChannel {
toJSON() {
var base = super.toJSON();
for(var prop of ["bitrate", "userLimit", "voiceMembers"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/VoiceState.js
Expand Up @@ -36,7 +36,7 @@ class VoiceState extends Base {
toJSON() {
var base = super.toJSON();
for(var prop of ["channelID", "deaf", "mute", "selfMute", "selfDeaf", "sessionID", "suppress"]) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
Expand Down

0 comments on commit 94019f1

Please sign in to comment.