Skip to content
This repository has been archived by the owner on Aug 28, 2020. It is now read-only.

Commit

Permalink
misc: Improve util.toTitleCase (#201)
Browse files Browse the repository at this point in the history
* Improve util.toTitleCase
Allows defining custom strings for toTitleCase

* Requested changes

* Suggested changes x2

* kyra's suggestion
  • Loading branch information
Frangu Vlad authored and bdistin committed Feb 25, 2018
1 parent dec6b20 commit 0e7f8d5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion src/lib/util/util.js
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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;
10 changes: 10 additions & 0 deletions typings/index.d.ts
Expand Up @@ -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 };
Expand Down Expand Up @@ -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

}

0 comments on commit 0e7f8d5

Please sign in to comment.