From 0e7f8d5dc96bbe0899e262f88461dc0551009024 Mon Sep 17 00:00:00 2001 From: Frangu Vlad Date: Sun, 25 Feb 2018 15:09:05 +0200 Subject: [PATCH] misc: Improve util.toTitleCase (#201) * Improve util.toTitleCase Allows defining custom strings for toTitleCase * Requested changes * Suggested changes x2 * kyra's suggestion --- CHANGELOG.md | 1 + src/lib/util/util.js | 15 ++++++++++++++- typings/index.d.ts | 10 ++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd8a7d0048..3f033722f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ NOTE: For the contributors, you add new entries to this document following this ### Added +- [[#201](https://github.com/dirigeants/klasa/pull/201)] Improve `util.toTitleCase`. (KingDGrizzle) - [[#186](https://github.com/dirigeants/klasa/pull/186)] Added a **load** command. (kyranet) - [[#176](https://github.com/dirigeants/klasa/pull/176)] Added `categorychannel` type to `ArgResolver`. (kyranet) - [[#166](https://github.com/dirigeants/klasa/pull/166)] Added support for TypeScript's `export default` in the loader. (kyranet) diff --git a/src/lib/util/util.js b/src/lib/util/util.js index 8d63930fd2..68a381f7a8 100644 --- a/src/lib/util/util.js +++ b/src/lib/util/util.js @@ -62,7 +62,7 @@ class Util { * @returns {string} */ static toTitleCase(str) { - return str.replace(/[A-Za-zÀ-ÖØ-öø-ÿ]\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()); + return str.replace(/[A-Za-zÀ-ÖØ-öø-ÿ]\S*/g, (txt) => Util.titleCaseVariants[txt] || txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()); } /** @@ -371,4 +371,17 @@ Util.exec = promisify(exec); */ Util.sleep = promisify(setTimeout); +/** + * Object with certain title case variants for words + * @since 0.5.0 + * @type {Object} + * @static + */ +Util.titleCaseVariants = { + textchannel: 'TextChannel', + voicechannel: 'VoiceChannel', + categorychannel: 'CategoryChannel', + guildmember: 'GuildMember' +}; + module.exports = Util; diff --git a/typings/index.d.ts b/typings/index.d.ts index 27e253a9aa..08cb3ec205 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1297,6 +1297,8 @@ declare module 'klasa' { public static toTitleCase(str: string): string; public static tryParse(value: string): object; private static initClean(client: KlasaClient): void; + + public static titleCaseVariants: TitleCaseVariants; } export { Util as util }; @@ -1918,6 +1920,14 @@ declare module 'klasa' { set(value: T): void; }; + export type TitleCaseVariants = { + textchannel: 'TextChannel'; + voicechannel: 'VoiceChannel'; + categorychannel: 'CategoryChannel'; + guildmember: 'GuildMember'; + [key: string]: string; + }; + //#endregion }