From 9d11def40e2dcdf002f2580843aa400259a2654a Mon Sep 17 00:00:00 2001 From: atomiks Date: Mon, 25 Dec 2017 02:01:25 +1100 Subject: [PATCH] Fix kebabCase styling + remove global variable + handle empty string --- snippets/toKebabCase.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/snippets/toKebabCase.md b/snippets/toKebabCase.md index 78294e993..bfee82b01 100644 --- a/snippets/toKebabCase.md +++ b/snippets/toKebabCase.md @@ -12,12 +12,11 @@ A word is defined as following:- For more detailed explanation of this Regex [Visit this Site](https://regex101.com/r/bMCgAB/1) ```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; - return str.match(regex).map(x =>{ - return x.toLowerCase(); - }).join('-'); -} +const toKebabCase = str => + 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.toLowerCase()) + .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'