1
0
Fork 0

Implement Array#map

refactor/models
Bsian 2019-11-01 15:00:25 +00:00
parent db7785c749
commit da2435fbcc
No known key found for this signature in database
GPG Key ID: 097FB9A291026091
3 changed files with 13 additions and 42 deletions

View File

@ -78,12 +78,12 @@ export default class Collection<V> extends Map<string, V> {
/** /**
* Return an array with the results of applying the given function to each element * 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<U>(callbackfn: (value?: V, index?: number, array?: V[]) => U): U[] {
const arr = []; const arr = [];
for (const item of this.values()) { for (const item of this.values()) {
arr.push(func(item)); arr.push(callbackfn(item));
} }
return arr; return arr;
} }
@ -102,22 +102,6 @@ export default class Collection<V> extends Map<string, V> {
return arr; 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. * 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 * @param func A function that takes an object and returns true if it matches
@ -131,20 +115,6 @@ export default class Collection<V> extends Map<string, V> {
return false; 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 * Update an object
* @param key The key of the object * @param key The key of the object

View File

@ -55,7 +55,7 @@ export default class Util {
} }
if (!resolvedCommand) return Promise.resolve({ cmd: null, args }); if (!resolvedCommand) return Promise.resolve({ cmd: null, args });
let parentLabel = `${command}`; let parentLabel = '';
let hasSubCommands = true; let hasSubCommands = true;
while (hasSubCommands) { while (hasSubCommands) {
if (!resolvedCommand.subcommands.size) { if (!resolvedCommand.subcommands.size) {
@ -63,12 +63,12 @@ export default class Util {
} else if (!args[0]) { } else if (!args[0]) {
hasSubCommands = false; break; hasSubCommands = false; break;
} else if (resolvedCommand.subcommands.has(args[0])) { } else if (resolvedCommand.subcommands.has(args[0])) {
resolvedCommand = resolvedCommand.subcommands.get(args[0]); parentLabel += `${resolvedCommand.name} `;
parentLabel += ` ${args[0]}`; args.shift(); resolvedCommand = resolvedCommand.subcommands.get(args[0]); args.shift();
} else { } else {
for (const subCmd of resolvedCommand.subcommands.toArray()) { for (const subCmd of resolvedCommand.subcommands.toArray()) {
if (subCmd.aliases.includes(args[0])) { if (subCmd.aliases.includes(args[0])) {
resolvedCommand = subCmd; parentLabel += ` ${args[0]}`; args.shift(); break; parentLabel += `${resolvedCommand.name} `; resolvedCommand = subCmd; args.shift(); break;
} }
} }
} }

View File

@ -19,7 +19,7 @@ export default class Help extends Command {
if (!args[0]) { if (!args[0]) {
const cmdList: Command[] = []; const cmdList: Command[] = [];
this.client.commands.forEach((c) => cmdList.push(c)); 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 aliases = c.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', ');
const perms: string[] = []; const perms: string[] = [];
let allowedRoles = c.permissions && c.permissions.roles && c.permissions.roles.map((r) => `<@&${r}>`).join(', '); 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); } if (allowedRoles) { allowedRoles = `**Roles:** ${allowedRoles}`; perms.push(allowedRoles); }
let allowedUsers = cmd.permissions && cmd.permissions.users && cmd.permissions.users.map((u) => `<@${u}>`).join(', '); let allowedUsers = cmd.permissions && cmd.permissions.users && cmd.permissions.users.map((u) => `<@${u}>`).join(', ');
if (allowedUsers) { allowedUsers = `**Users:** ${allowedUsers}`; perms.push(allowedUsers); } if (allowedUsers) { allowedUsers = `**Users:** ${allowedUsers}`; perms.push(allowedUsers); }
const displayedPerms = perms.length ? `**Permissions:**\n${perms.join('\n')}` : ''; const displayedPerms = perms.length ? `\n**Permissions:**\n${perms.join('\n')}` : '';
const aliases = cmd.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', '); 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(); const embed = new RichEmbed();
embed.setTimestamp(); embed.setFooter(`Requested by ${message.author.username}#${message.author.discriminator}`, message.author.avatarURL); 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); 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}\n**Aliases:** ${aliases}\n${displayedPerms}`; const description = `**Description**: ${cmd.description}\n**Usage:** ${cmd.usage}${aliases}${displayedPerms}${subcommands}`;
embed.setDescription(description); embed.setDescription(description);
// @ts-ignore // @ts-ignore
message.channel.createMessage({ embed }); message.channel.createMessage({ embed });