From edcbd9ba44cc0fc52c5c20a570d0fb104384fda5 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Fri, 13 Mar 2020 18:16:45 -0400 Subject: [PATCH] sortByName Util Function --- commands/readme/generate-credit.js | 9 +++++---- util/Util.js | 7 +++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/commands/readme/generate-credit.js b/commands/readme/generate-credit.js index a345f8fa..b7e069b6 100644 --- a/commands/readme/generate-credit.js +++ b/commands/readme/generate-credit.js @@ -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')}`); diff --git a/util/Util.js b/util/Util.js index 587099f0..632af12b 100644 --- a/util/Util.js +++ b/util/Util.js @@ -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(' '); }