From 144894cca50edb115bebc9cce929c01814e75892 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Fri, 22 Dec 2017 22:44:51 +0530 Subject: [PATCH] add snippet toKebabCase --- snippets/arrayMin.md | 2 +- snippets/arraySum.md | 2 +- snippets/gcd.md | 10 +++------- snippets/toKebabCase.md | 17 +++++++++++++++++ 4 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 snippets/toKebabCase.md diff --git a/snippets/arrayMin.md b/snippets/arrayMin.md index 5bdd95559..6df9b799a 100644 --- a/snippets/arrayMin.md +++ b/snippets/arrayMin.md @@ -5,6 +5,6 @@ Returns the minimum value in an array. Use `Math.min()` combined with the spread operator (`...`) to get the minimum value in the array. ```js -const arrayMin = arr => Math.min(...arr); +const arrayMin = zzarr => Math.min(...arr); // arrayMin([10, 1, 5]) -> 1 ``` diff --git a/snippets/arraySum.md b/snippets/arraySum.md index 265c7fc60..31533d929 100644 --- a/snippets/arraySum.md +++ b/snippets/arraySum.md @@ -5,6 +5,6 @@ Returns the sum of an array of numbers. Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`. ```js -const arraySum = arr => arr.reduce((acc, val) => acc + val, 0); +const arraySum = zarr => arr.reduce((acc, val) => acc + val, 0); // arraySum([1,2,3,4]) -> 10 ``` diff --git a/snippets/gcd.md b/snippets/gcd.md index bda988f05..34fb21833 100644 --- a/snippets/gcd.md +++ b/snippets/gcd.md @@ -1,16 +1,12 @@ ### gcd -Calculates the greatest common divisor between two or more numbers numbers. +Calculates the greatest common divisor between two numbers. -The helper function uses recursion. -The helper case takes two arguments x and y +Use recursion. Base case is when `y` equals `0`. In this case, return `x`. Otherwise, return the GCD of `y` and the remainder of the division `x/y`. ```js -const gcd = (...arr) => { - const gcdHelper = (x, y) => !y ? x : gcd(y, x % y); - return arr.reduce((a,b) => gcdHelper(a,b)) -} +const gcd = (x, y) => !y ? x : gcd(y, x % y); // gcd (8, 36) -> 4 ``` diff --git a/snippets/toKebabCase.md b/snippets/toKebabCase.md new file mode 100644 index 000000000..9d5432325 --- /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(/\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" +```