Tag, lint, build

This commit is contained in:
Angelos Chalaris
2017-12-15 12:53:09 +02:00
parent 14543a15b6
commit 453026fe3b
8 changed files with 94 additions and 32 deletions

View File

@ -1,9 +1,9 @@
### Array sample
Use `Math.random()` to generate a random number, multiply it with `Array.length` and round it of to the nearest whole number using `Math.floor()`. Works with strings too.
Use `Math.random()` to generate a random number, multiply it with `length` and round it of to the nearest whole number using `Math.floor()`.
This method also works with strings.
```js
const sample = arr => arr[Math.floor(Math.random() * arr.length)];
// sample([3, 7, 9, 11]) -> 9
```
// sample([3, 7, 9, 11]) -> 9
```

View File

@ -1,12 +1,9 @@
### Without
### Array without
Use `Array.filter()` to create an array excluding all given values
Use `Array.filter()` to create an array excluding all given values.
```js
const without = (arr, ...args) => arr.filter(v => args.indexOf(v) === -1)
const without = (arr, ...args) => arr.filter(v => args.indexOf(v) === -1);
// without[2, 1, 2, 3], 1, 2) -> [3]
// without([2, 1, 2, 3, 4, 5, 5, 5, 3, 2, 7, 7], 3, 1, 5, 2) -> [ 4, 7, 7 ]
```

View File

@ -1,17 +1,16 @@
### Array zip
Use `Math.max.apply` to get the longest array in the arguments.
Creates an array with that length as return value and use `Array.from` with a map-function to create an array of grouped elements.
Use `Math.max.apply()` to get the longest array in the arguments.
Creates an array with that length as return value and use `Array.from()` with a map-function to create an array of grouped elements.
If lengths of the argument-arrays vary, `undefined` is used where no value could be found.
```js
const zip = (...arrays) => {
const maxLength = Math.max.apply(null, arrays.map(a => a.length));
return Array.from({length: maxLength}).map((_, i) => {
return Array.from({length: arrays.length}, (_, k) => arrays[k][i])
return Array.from({length: arrays.length}, (_, k) => arrays[k][i]);
})
}
//zip(['a', 'b'], [1, 2], [true, false]); -> [['a', 1, true], ['b', 2, false]]
//zip(['a'], [1, 2], [true, false]); -> [['a', 1, true], [undefined, 2, false]]
```

View File

@ -1,7 +1,8 @@
### Deep flatten array
Use recursion.
Use `[].concat()` and the spread operator `...` to flatten an array, and recursively flatten each element that is an array.
Use `Array.concat()` with an empty array (`[]`) and the spread operator (`...`) to flatten an array.
Rrecursively flatten each element that is an array.
```js
const deepFlatten = arr => [].concat(...arr.map(v => Array.isArray(v) ? deepFlatten(v) : v));

View File

@ -1,15 +1,14 @@
### Pipe
Use `Array.reduce()` to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
Use `Array.reduce()` to perform left-to-right function composition.
The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
```js
const pipe = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)))
const pipe = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
/*
const add5 = x => x + 5
const multiply = (x, y) => x * y
const multiply = (x, y) => x * y
const multiplyAndAdd5 = pipe(multiply, add5)
multiplyAndAdd5(5, 2) -> 15
*/
```

View File

@ -1,4 +1,4 @@
### takeRight
### Take right
Use `Array.slice()` to create a slice of the array with `n` elements taken from the end.
@ -6,5 +6,4 @@ Use `Array.slice()` to create a slice of the array with `n` elements taken from
const takeRight = (arr, n = 1) => arr.slice(arr.length - n, arr.length);
// takeRight([1, 2, 3], 2) -> [ 2, 3 ]
// takeRight([1, 2, 3]) -> [3]
// takeRight([1, 2, 3]) -> []
```
```