From 17b4f37615065e0800d92c0fd096be358e48e9e7 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Sun, 24 Dec 2017 18:19:35 +0530 Subject: [PATCH 1/3] update toKebabCase.md --- snippets/toKebabCase.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/snippets/toKebabCase.md b/snippets/toKebabCase.md index 7d8fe09dd..5e482426d 100644 --- a/snippets/toKebabCase.md +++ b/snippets/toKebabCase.md @@ -1,16 +1,28 @@ ### toKebabCase Converts a string to [kebab case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles). -Use `replace()` to add spaces before capital letters, convert `toLowerCase()`, then `replace()` underscores and spaces with hyphens. -Also check if a string starts with a hyphen and remove it if yes. +Breaks the string into words. +A word is defined as following:- +-> Beginning with two or more capital letters, e.g. XML or FM +-> Begin with a capital letter followed by lower case letters with optional trailing numbers, e.g. Hello or Http2 +-> Contain nothing but lower case letters with optional trailing numbers, e.g. hello or http2 +-> Individual upper letters, e.g T.M.N.T +-> Groups of numbers, e.g. 555-555-5555 + +For more detailed explanation of this Regex [Visit this Site](https://regex101.com/r/bMCgAB/1) ```js const toKebabCase = str => { - str = str.replace(/([A-Z])/g," $1").toLowerCase().replace(/_/g,' ').replace(/-/g,' ').replace(/\s\s+/g, ' ').replace(/\s/g,'-'); - return str.startsWith('-') ? str.slice(1,str.length) : str; + let regex = rx = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g; + let arr = str.match(regex); + arr = arr.forEach(x =>{ + return s.toLowerCase(); + }); + return arr.join('-') } // toKebabCase("camelCase") -> 'camel-case' // toKebabCase("some text") -> 'some-text' // toKebabCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some-mixed-string-with-spaces-underscores-and-hyphens' // toKebabCase("AllThe-small Things") -> "all-the-small-things" +// toKebabCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLandHTML') -> "i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html" ``` From aded8fcfc0da4324d82febb638752d8ac8a8f951 Mon Sep 17 00:00:00 2001 From: David Wu Date: Sun, 24 Dec 2017 14:44:26 +0100 Subject: [PATCH 2/3] Correct faults Your version didn't work. There was a declaration fault alongside with an identifier fault as Array.forEach does not return anything. --- snippets/toKebabCase.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/snippets/toKebabCase.md b/snippets/toKebabCase.md index 5e482426d..0e3fd0a3c 100644 --- a/snippets/toKebabCase.md +++ b/snippets/toKebabCase.md @@ -14,11 +14,9 @@ For more detailed explanation of this Regex [Visit this Site](https://regex101.c ```js const toKebabCase = str => { let regex = rx = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g; - let arr = str.match(regex); - arr = arr.forEach(x =>{ - return s.toLowerCase(); - }); - return arr.join('-') + return str.match(regex).map(x =>{ + return x.toLowerCase(); + }).join('-'); } // toKebabCase("camelCase") -> 'camel-case' // toKebabCase("some text") -> 'some-text' From 2434369774e80e2d41a01ef00f3ff3b31b7ca205 Mon Sep 17 00:00:00 2001 From: David Wu Date: Sun, 24 Dec 2017 14:45:17 +0100 Subject: [PATCH 3/3] Update toKebabCase.md --- snippets/toKebabCase.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/toKebabCase.md b/snippets/toKebabCase.md index 0e3fd0a3c..78294e993 100644 --- a/snippets/toKebabCase.md +++ b/snippets/toKebabCase.md @@ -22,5 +22,5 @@ const toKebabCase = str => { // toKebabCase("some text") -> 'some-text' // toKebabCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some-mixed-string-with-spaces-underscores-and-hyphens' // toKebabCase("AllThe-small Things") -> "all-the-small-things" -// toKebabCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLandHTML') -> "i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html" +// toKebabCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML') -> "i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html" ```