add snippet toKebabCase
This commit is contained in:
@ -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
|
||||
```
|
||||
|
||||
@ -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
|
||||
```
|
||||
|
||||
@ -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
|
||||
```
|
||||
|
||||
17
snippets/toKebabCase.md
Normal file
17
snippets/toKebabCase.md
Normal file
@ -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"
|
||||
```
|
||||
Reference in New Issue
Block a user