Fix line endings
This commit is contained in:
@ -1,22 +1,22 @@
|
||||
---
|
||||
title: call
|
||||
tags: function,advanced
|
||||
---
|
||||
|
||||
Given a key and a set of arguments, call them when given a context.
|
||||
|
||||
- Use a closure to call `key` with `args` for the given `context`.
|
||||
|
||||
```js
|
||||
const call = (key, ...args) => context => context[key](...args);
|
||||
```
|
||||
|
||||
```js
|
||||
Promise.resolve([1, 2, 3])
|
||||
.then(call('map', x => 2 * x))
|
||||
.then(console.log); // [ 2, 4, 6 ]
|
||||
const map = call.bind(null, 'map');
|
||||
Promise.resolve([1, 2, 3])
|
||||
.then(map(x => 2 * x))
|
||||
.then(console.log); // [ 2, 4, 6 ]
|
||||
```
|
||||
---
|
||||
title: call
|
||||
tags: function,advanced
|
||||
---
|
||||
|
||||
Given a key and a set of arguments, call them when given a context.
|
||||
|
||||
- Use a closure to call `key` with `args` for the given `context`.
|
||||
|
||||
```js
|
||||
const call = (key, ...args) => context => context[key](...args);
|
||||
```
|
||||
|
||||
```js
|
||||
Promise.resolve([1, 2, 3])
|
||||
.then(call('map', x => 2 * x))
|
||||
.then(console.log); // [ 2, 4, 6 ]
|
||||
const map = call.bind(null, 'map');
|
||||
Promise.resolve([1, 2, 3])
|
||||
.then(map(x => 2 * x))
|
||||
.then(console.log); // [ 2, 4, 6 ]
|
||||
```
|
||||
|
||||
@ -1,20 +1,20 @@
|
||||
---
|
||||
title: collectInto
|
||||
tags: function,array,intermediate
|
||||
---
|
||||
|
||||
Changes a function that accepts an array into a variadic function.
|
||||
|
||||
- Given a function, return a closure that collects all inputs into an array-accepting function.
|
||||
|
||||
```js
|
||||
const collectInto = fn => (...args) => fn(args);
|
||||
```
|
||||
|
||||
```js
|
||||
const Pall = collectInto(Promise.all.bind(Promise));
|
||||
let p1 = Promise.resolve(1);
|
||||
let p2 = Promise.resolve(2);
|
||||
let p3 = new Promise(resolve => setTimeout(resolve, 2000, 3));
|
||||
Pall(p1, p2, p3).then(console.log); // [1, 2, 3] (after about 2 seconds)
|
||||
```
|
||||
---
|
||||
title: collectInto
|
||||
tags: function,array,intermediate
|
||||
---
|
||||
|
||||
Changes a function that accepts an array into a variadic function.
|
||||
|
||||
- Given a function, return a closure that collects all inputs into an array-accepting function.
|
||||
|
||||
```js
|
||||
const collectInto = fn => (...args) => fn(args);
|
||||
```
|
||||
|
||||
```js
|
||||
const Pall = collectInto(Promise.all.bind(Promise));
|
||||
let p1 = Promise.resolve(1);
|
||||
let p2 = Promise.resolve(2);
|
||||
let p3 = new Promise(resolve => setTimeout(resolve, 2000, 3));
|
||||
Pall(p1, p2, p3).then(console.log); // [1, 2, 3] (after about 2 seconds)
|
||||
```
|
||||
|
||||
@ -1,23 +1,23 @@
|
||||
---
|
||||
title: flip
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
Takes a function as an argument, then makes the first argument the last.
|
||||
|
||||
- Use argument destructuring and a closure with variadic arguments.
|
||||
- Splice the first argument, using the spread operator (`...`), to make it the last before applying the rest.
|
||||
|
||||
```js
|
||||
const flip = fn => (first, ...rest) => fn(...rest, first);
|
||||
```
|
||||
|
||||
```js
|
||||
let a = { name: 'John Smith' };
|
||||
let b = {};
|
||||
const mergeFrom = flip(Object.assign);
|
||||
let mergePerson = mergeFrom.bind(null, a);
|
||||
mergePerson(b); // == b
|
||||
b = {};
|
||||
Object.assign(b, a); // == b
|
||||
```
|
||||
---
|
||||
title: flip
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
Takes a function as an argument, then makes the first argument the last.
|
||||
|
||||
- Use argument destructuring and a closure with variadic arguments.
|
||||
- Splice the first argument, using the spread operator (`...`), to make it the last before applying the rest.
|
||||
|
||||
```js
|
||||
const flip = fn => (first, ...rest) => fn(...rest, first);
|
||||
```
|
||||
|
||||
```js
|
||||
let a = { name: 'John Smith' };
|
||||
let b = {};
|
||||
const mergeFrom = flip(Object.assign);
|
||||
let mergePerson = mergeFrom.bind(null, a);
|
||||
mergePerson(b); // == b
|
||||
b = {};
|
||||
Object.assign(b, a); // == b
|
||||
```
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
---
|
||||
title: spreadOver
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
Takes a variadic function and returns a function that accepts an array of arguments.
|
||||
|
||||
- Use a closure and the spread operator (`...`) to map the array of arguments to the inputs of the function.
|
||||
|
||||
```js
|
||||
const spreadOver = fn => argsArr => fn(...argsArr);
|
||||
```
|
||||
|
||||
```js
|
||||
const arrayMax = spreadOver(Math.max);
|
||||
arrayMax([1, 2, 3]); // 3
|
||||
```
|
||||
---
|
||||
title: spreadOver
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
Takes a variadic function and returns a function that accepts an array of arguments.
|
||||
|
||||
- Use a closure and the spread operator (`...`) to map the array of arguments to the inputs of the function.
|
||||
|
||||
```js
|
||||
const spreadOver = fn => argsArr => fn(...argsArr);
|
||||
```
|
||||
|
||||
```js
|
||||
const arrayMax = spreadOver(Math.max);
|
||||
arrayMax([1, 2, 3]); // 3
|
||||
```
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
---
|
||||
title: toRGBArray
|
||||
tags: string,browser,regexp,beginner
|
||||
---
|
||||
|
||||
Converts an `rgb()` color string to an array of values.
|
||||
|
||||
- Use `String.prototype.match()` to get an array of 3 string with the numeric values.
|
||||
- Use `Array.prototype.map()` in combination with `Number` to convert them into an array of numeric values.
|
||||
|
||||
```js
|
||||
const toRGBArray = rgbStr => rgbStr.match(/\d+/g).map(Number);
|
||||
```
|
||||
|
||||
```js
|
||||
toRGBArray('rgb(255, 12, 0)'); // [255, 12, 0]
|
||||
```
|
||||
---
|
||||
title: toRGBArray
|
||||
tags: string,browser,regexp,beginner
|
||||
---
|
||||
|
||||
Converts an `rgb()` color string to an array of values.
|
||||
|
||||
- Use `String.prototype.match()` to get an array of 3 string with the numeric values.
|
||||
- Use `Array.prototype.map()` in combination with `Number` to convert them into an array of numeric values.
|
||||
|
||||
```js
|
||||
const toRGBArray = rgbStr => rgbStr.match(/\d+/g).map(Number);
|
||||
```
|
||||
|
||||
```js
|
||||
toRGBArray('rgb(255, 12, 0)'); // [255, 12, 0]
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user