From da2435fbcc3e2dc48369c4e7a1ec70e05b591573 Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 15:00:25 +0000 Subject: [PATCH] Implement Array#map --- src/class/Collection.ts | 36 +++--------------------------------- src/class/Util.ts | 8 ++++---- src/commands/help.ts | 11 ++++++----- 3 files changed, 13 insertions(+), 42 deletions(-) diff --git a/src/class/Collection.ts b/src/class/Collection.ts index 9f810dc..71e8a07 100644 --- a/src/class/Collection.ts +++ b/src/class/Collection.ts @@ -78,12 +78,12 @@ export default class Collection extends Map { /** * Return an array with the results of applying the given function to each element - * @param func A function that takes an object and returns something + * @param callbackfn A function that takes an object and returns something */ - map(func: Function) { + map(callbackfn: (value?: V, index?: number, array?: V[]) => U): U[] { const arr = []; for (const item of this.values()) { - arr.push(func(item)); + arr.push(callbackfn(item)); } return arr; } @@ -102,22 +102,6 @@ export default class Collection extends Map { return arr; } - /** - * Reduce values by function - * @param callbackFn Function to execute on each element in the array - * @param initialValue Value to use as the first argument to the first call of the callback - * @returns Accumulator - */ - reduce(func: Function, initialValue = 0) { - const iter = this.values(); - let val: any; - let result = initialValue === undefined ? iter.next().value : initialValue; - while ((val = iter.next().value) !== undefined) { // eslint-disable-line - result = func(result, val); - } - return result; - } - /** * Test if at least one element passes the test implemented by the provided function. Returns true if yes, or false if not. * @param func A function that takes an object and returns true if it matches @@ -131,20 +115,6 @@ export default class Collection extends Map { return false; } - /** - * Test if all elements passe the test implemented by the provided function. Returns true if yes, or false if not. - * @param func A function that takes an object and returns true if it matches - * @returns An array containing booleans that matched - */ - every(func: Function): boolean[] { - const array = []; - for (const item of this.values()) { - if (func(item)) array.push(true); - else array.push(false); - } - return array; - } - /** * Update an object * @param key The key of the object diff --git a/src/class/Util.ts b/src/class/Util.ts index 6ae38eb..279f6a9 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -55,7 +55,7 @@ export default class Util { } if (!resolvedCommand) return Promise.resolve({ cmd: null, args }); - let parentLabel = `${command}`; + let parentLabel = ''; let hasSubCommands = true; while (hasSubCommands) { if (!resolvedCommand.subcommands.size) { @@ -63,12 +63,12 @@ export default class Util { } else if (!args[0]) { hasSubCommands = false; break; } else if (resolvedCommand.subcommands.has(args[0])) { - resolvedCommand = resolvedCommand.subcommands.get(args[0]); - parentLabel += ` ${args[0]}`; args.shift(); + parentLabel += `${resolvedCommand.name} `; + resolvedCommand = resolvedCommand.subcommands.get(args[0]); args.shift(); } else { for (const subCmd of resolvedCommand.subcommands.toArray()) { if (subCmd.aliases.includes(args[0])) { - resolvedCommand = subCmd; parentLabel += ` ${args[0]}`; args.shift(); break; + parentLabel += `${resolvedCommand.name} `; resolvedCommand = subCmd; args.shift(); break; } } } diff --git a/src/commands/help.ts b/src/commands/help.ts index d44954f..909f748 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -19,7 +19,7 @@ export default class Help extends Command { if (!args[0]) { const cmdList: Command[] = []; this.client.commands.forEach((c) => cmdList.push(c)); - const commands = this.client.commands.map((c: Command) => { + const commands = this.client.commands.map((c) => { const aliases = c.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', '); const perms: string[] = []; let allowedRoles = c.permissions && c.permissions.roles && c.permissions.roles.map((r) => `<@&${r}>`).join(', '); @@ -51,12 +51,13 @@ export default class Help extends Command { if (allowedRoles) { allowedRoles = `**Roles:** ${allowedRoles}`; perms.push(allowedRoles); } let allowedUsers = cmd.permissions && cmd.permissions.users && cmd.permissions.users.map((u) => `<@${u}>`).join(', '); if (allowedUsers) { allowedUsers = `**Users:** ${allowedUsers}`; perms.push(allowedUsers); } - const displayedPerms = perms.length ? `**Permissions:**\n${perms.join('\n')}` : ''; - const aliases = cmd.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', '); + const displayedPerms = perms.length ? `\n**Permissions:**\n${perms.join('\n')}` : ''; + const aliases = cmd.aliases.length ? `\n**Aliases:** ${cmd.aliases.map((alias) => `${this.client.config.prefix}${cmd.parentName}${alias}`).join(', ')}` : ''; + const subcommands = cmd.subcommands.size ? `\n**Subcommands**${cmd.subcommands.map((s) => s.parentName + s.name).join(', ')}` : ''; const embed = new RichEmbed(); embed.setTimestamp(); embed.setFooter(`Requested by ${message.author.username}#${message.author.discriminator}`, message.author.avatarURL); - embed.setTitle(`${this.client.config.prefix}${cmd.parentName}`); embed.setAuthor(`${this.client.user.username}#${this.client.user.discriminator}`, this.client.user.avatarURL); - const description = `**Description**: ${cmd.description}\n**Usage:** ${cmd.usage}\n**Aliases:** ${aliases}\n${displayedPerms}`; + embed.setTitle(`${this.client.config.prefix}${cmd.parentName}${cmd.name}`); embed.setAuthor(`${this.client.user.username}#${this.client.user.discriminator}`, this.client.user.avatarURL); + const description = `**Description**: ${cmd.description}\n**Usage:** ${cmd.usage}${aliases}${displayedPerms}${subcommands}`; embed.setDescription(description); // @ts-ignore message.channel.createMessage({ embed });