From 0a1eebcda53cc5d24d66f7ef5de015b86703a929 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Sat, 23 Dec 2017 18:45:16 +0530 Subject: [PATCH 1/5] add toKebabCase --- snippets/repeatString.md | 0 snippets/toKebabCase.md | 17 +++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 snippets/repeatString.md create mode 100644 snippets/toKebabCase.md diff --git a/snippets/repeatString.md b/snippets/repeatString.md new file mode 100644 index 000000000..e69de29bb diff --git a/snippets/toKebabCase.md b/snippets/toKebabCase.md new file mode 100644 index 000000000..2eaee9f2c --- /dev/null +++ b/snippets/toKebabCase.md @@ -0,0 +1,17 @@ +### toKebabCase + +Converts a string to snakecase. +Use `replace()` to add spaces before capital letters, convert `toLowerCase()`, then `replace()` and undesrsocres and spaces with hyphens. +Also check if a string starts with hyphen and if yes remove it. + +```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; +} +// toKebabCase("camelCase") -> 'camel-case' +// toKebabCase("some text") -> 'some-text' +// toKebabCase("some-javascript-property") -> 'some-javascript-property' +// 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" +``` From 18d8d03110b14cb3d47bfbaf569bacdd02686f7c Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Sat, 23 Dec 2017 18:53:41 +0530 Subject: [PATCH 2/5] add repeatString --- snippets/repeatString.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/snippets/repeatString.md b/snippets/repeatString.md index e69de29bb..fbccf52d2 100644 --- a/snippets/repeatString.md +++ b/snippets/repeatString.md @@ -0,0 +1,17 @@ +### toKebabCase + +Repeats a string n times using String.repeat() + +If no string is provided the default is `""` and the default number of times is 1 + +```js +const repeatString = (str="",num=1) => { + return num >= 0 ? str.repeat(num) : str; +} +// toRepeatString("abc",3) -> 'abcabcabc' +// toRepeatString("abc",0) -> '' +// toRepeatString("abc") -> 'abc' +// toRepeatString("abc",-1) -> 'abc' +// toRepeatString() -> '' +// toRepeatString(undefined,3) -> '' +``` From 546ce67fe851e8360bf8ac396ff655ee5d4fd9df Mon Sep 17 00:00:00 2001 From: David Wu Date: Sat, 23 Dec 2017 15:59:44 +0100 Subject: [PATCH 3/5] Update repeatString.md --- snippets/repeatString.md | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/snippets/repeatString.md b/snippets/repeatString.md index fbccf52d2..619fe767b 100644 --- a/snippets/repeatString.md +++ b/snippets/repeatString.md @@ -1,17 +1,14 @@ -### toKebabCase +### repeatString -Repeats a string n times using String.repeat() +Repeats a string n times using `String.repeat()` -If no string is provided the default is `""` and the default number of times is 1 +If no string is provided the default is `""` and the default number of times is 2. ```js -const repeatString = (str="",num=1) => { +const repeatString = (str="",num=2) => { return num >= 0 ? str.repeat(num) : str; } -// toRepeatString("abc",3) -> 'abcabcabc' -// toRepeatString("abc",0) -> '' -// toRepeatString("abc") -> 'abc' -// toRepeatString("abc",-1) -> 'abc' -// toRepeatString() -> '' -// toRepeatString(undefined,3) -> '' +// repeatString("abc",3) -> 'abcabcabc' +// repeatString("abc",0) -> '' +// repeatString("abc") -> 'abcabc' ``` From f0d899b6c85b6120d28169c7835f4406dac523ad Mon Sep 17 00:00:00 2001 From: David Wu Date: Sat, 23 Dec 2017 16:02:20 +0100 Subject: [PATCH 4/5] Update repeatString.md --- snippets/repeatString.md | 1 - 1 file changed, 1 deletion(-) diff --git a/snippets/repeatString.md b/snippets/repeatString.md index 619fe767b..888965713 100644 --- a/snippets/repeatString.md +++ b/snippets/repeatString.md @@ -9,6 +9,5 @@ const repeatString = (str="",num=2) => { return num >= 0 ? str.repeat(num) : str; } // repeatString("abc",3) -> 'abcabcabc' -// repeatString("abc",0) -> '' // repeatString("abc") -> 'abcabc' ``` From bf613ac86c65f68b46e4c233ac08182c5b952030 Mon Sep 17 00:00:00 2001 From: David Wu Date: Sat, 23 Dec 2017 16:10:56 +0100 Subject: [PATCH 5/5] Update toKebabCase.md --- snippets/toKebabCase.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/snippets/toKebabCase.md b/snippets/toKebabCase.md index 2eaee9f2c..7d8fe09dd 100644 --- a/snippets/toKebabCase.md +++ b/snippets/toKebabCase.md @@ -1,8 +1,8 @@ ### toKebabCase -Converts a string to snakecase. -Use `replace()` to add spaces before capital letters, convert `toLowerCase()`, then `replace()` and undesrsocres and spaces with hyphens. -Also check if a string starts with hyphen and if yes remove it. +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. ```js const toKebabCase = str => { @@ -11,7 +11,6 @@ const toKebabCase = str => { } // toKebabCase("camelCase") -> 'camel-case' // toKebabCase("some text") -> 'some-text' -// toKebabCase("some-javascript-property") -> 'some-javascript-property' // 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" ```