Add rearg
This commit is contained in:
25
snippets/rearg.md
Normal file
25
snippets/rearg.md
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
### rearg
|
||||||
|
|
||||||
|
Creates a function that invokes the provided function with its arguments arranged according to the specified indexes.
|
||||||
|
|
||||||
|
Use `Array.reduce()` and `Array.indexOf()` to reorder arguments based on `indexes` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const rearg = (fn, indexes) => (...args) =>
|
||||||
|
fn(
|
||||||
|
...args.reduce(
|
||||||
|
(acc, val, i) => ((acc[indexes.indexOf(i)] = val), acc),
|
||||||
|
Array.from({ length: indexes.length })
|
||||||
|
)
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
var rearged = rearg(
|
||||||
|
function(a, b, c) {
|
||||||
|
return [a, b, c];
|
||||||
|
},
|
||||||
|
[2, 0, 1]
|
||||||
|
);
|
||||||
|
rearged('b', 'c', 'a'); // ['a', 'b', 'c']
|
||||||
|
```
|
||||||
@ -187,6 +187,7 @@ randomIntArrayInRange:math,utility,random
|
|||||||
randomIntegerInRange:math,utility,random
|
randomIntegerInRange:math,utility,random
|
||||||
randomNumberInRange:math,utility,random
|
randomNumberInRange:math,utility,random
|
||||||
readFileLines:node,array,string
|
readFileLines:node,array,string
|
||||||
|
rearg:adapter,function
|
||||||
redirect:browser,url
|
redirect:browser,url
|
||||||
reducedFilter:array
|
reducedFilter:array
|
||||||
reduceSuccessive:array,function
|
reduceSuccessive:array,function
|
||||||
|
|||||||
Reference in New Issue
Block a user