sortByName Util Function

This commit is contained in:
Dragon Fire
2020-03-13 18:16:45 -04:00
parent 0956ecd732
commit edcbd9ba44
2 changed files with 12 additions and 4 deletions
+5 -4
View File
@@ -1,5 +1,6 @@
const Command = require('../../structures/Command');
const request = require('node-superfetch');
const { sortByName } = require('../../util/Util');
module.exports = class GenerateCreditCommand extends Command {
constructor(client) {
@@ -24,8 +25,8 @@ module.exports = class GenerateCreditCommand extends Command {
async run(msg) {
let credit = [];
const commands = this.client.registry.commands.filter(cmd => cmd.credit && cmd.credit.length > 1);
for (const command of commands.values()) {
for (const command of this.client.registry.commands.values()) {
if (!command.credit || command.credit.length <= 1) continue;
for (const cred of command.credit) {
const found = credit.find(c => c.name === cred.name);
if (found) {
@@ -48,9 +49,9 @@ module.exports = class GenerateCreditCommand extends Command {
});
}
}
credit = credit.sort((a, b) => a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1);
credit = sortByName(credit, 'name');
const mapped = credit
.map(c => `- [${c.name}](${c.url})\n${c.commands.map(cmd => {
.map(c => `- [${c.name}](${c.url})\n${sortByName(c.commands, 'name').map(cmd => {
if (!cmd.reasonURL) return ` * ${cmd.name} (${cmd.reason})`;
return ` * ${cmd.name} ([${cmd.reason}](${cmd.reasonURL}))`;
}).join('\n')}`);
+7
View File
@@ -50,6 +50,13 @@ module.exports = class Util {
return newArr;
}
static sortByName(arr, prop) {
return arr.sort((a, b) => {
if (prop) return a[prop].toLowerCase() > b[prop].toLowerCase() ? 1 : -1;
return a.toLowerCase() > b.toLowerCase() ? 1 : -1;
});
}
static firstUpperCase(text, split = ' ') {
return text.split(split).map(word => `${word.charAt(0).toUpperCase()}${word.slice(1)}`).join(' ');
}