From c49c77f80e3d2a465832a0dc412afe8b7ef45bc9 Mon Sep 17 00:00:00 2001 From: ccjmne Date: Fri, 19 Oct 2018 03:49:34 +0200 Subject: [PATCH 1/3] Add toTitleCase snippet and update tag_database accordingly --- snippets/toTitleCase.md | 22 ++++++++++++++++++++++ tag_database | 1 + 2 files changed, 23 insertions(+) create mode 100644 snippets/toTitleCase.md diff --git a/snippets/toTitleCase.md b/snippets/toTitleCase.md new file mode 100644 index 000000000..2108dddbd --- /dev/null +++ b/snippets/toTitleCase.md @@ -0,0 +1,22 @@ +### toTitleCase + +Converts a string to title case. + +The algorithm featured here breaks down as follows: +1. Break the string into words, using a [Regular Expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) +2. Capitalize each of them +3. Stich them back together, using the whitespace `' '` character as a separator + +```js +const toTitleCase = str => str + .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) + .map(x => x.charAt(0).toUpperCase() + x.slice(1).toLowerCase()) + .join(' '); +``` + +```js +toTitleCase('some_database_field_name'); // 'Some Database Field Name' +toTitleCase('Some label that needs to be title-cased'); // 'Some Label That Needs To Be Title Cased' +toTitleCase('some-package-name'); // 'Some Package Name' +toTitleCase('some-mixed_string with spaces_underscores-and-hyphens'); // 'Some Mixed String With Spaces Underscores And Hyphens' +``` diff --git a/tag_database b/tag_database index cd05988b1..aaa61a47e 100644 --- a/tag_database +++ b/tag_database @@ -301,6 +301,7 @@ tomorrow:date,intermediate toOrdinalSuffix:utility,math,intermediate toSafeInteger:math,beginner toSnakeCase:string,regexp,intermediate +toTitleCase:string,regepx,intermediate transform:object,array,intermediate triggerEvent:browser,event,intermediate truncateString:string,beginner From 6fb735eb09740380d77f14d1fa87e2ab795c1c3a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 19 Oct 2018 08:36:58 +0300 Subject: [PATCH 2/3] Update toTitleCase.md --- snippets/toTitleCase.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/snippets/toTitleCase.md b/snippets/toTitleCase.md index 2108dddbd..2ae492d78 100644 --- a/snippets/toTitleCase.md +++ b/snippets/toTitleCase.md @@ -2,10 +2,7 @@ Converts a string to title case. -The algorithm featured here breaks down as follows: -1. Break the string into words, using a [Regular Expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) -2. Capitalize each of them -3. Stich them back together, using the whitespace `' '` character as a separator +Break the string into words, using a regexp, and combine them capitalizing the first letter of each word and adding a whitespace between them. ```js const toTitleCase = str => str From 2408b4d435f18d171b4cc95c550ebb017388ab70 Mon Sep 17 00:00:00 2001 From: Robert Mennell Date: Fri, 19 Oct 2018 17:58:40 +0200 Subject: [PATCH 3/3] Update snippets/toTitleCase.md Co-Authored-By: ccjmne --- snippets/toTitleCase.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/toTitleCase.md b/snippets/toTitleCase.md index 2ae492d78..bdd601c0e 100644 --- a/snippets/toTitleCase.md +++ b/snippets/toTitleCase.md @@ -7,7 +7,7 @@ Break the string into words, using a regexp, and combine them capitalizing the f ```js const toTitleCase = str => str .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) - .map(x => x.charAt(0).toUpperCase() + x.slice(1).toLowerCase()) + .map(x => x.charAt(0).toUpperCase() + x.slice(1)) .join(' '); ```