Add overArgs

This commit is contained in:
Angelos Chalaris
2018-01-28 14:54:16 +02:00
parent 6b22af223a
commit b005826fde
2 changed files with 21 additions and 0 deletions

20
snippets/overArgs.md Normal file
View File

@ -0,0 +1,20 @@
### overArgs
Creates a function that invokes the provided function with its arguments transformed.
Use `Array.map()` to apply `transforms` to `args` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`.
```js
const overArgs = (fn, transforms) => (...args) =>
fn(...args.map((val, i) => transforms[i](val)));
```
```js
var func = overArgs(
function(x, y) {
return [x, y];
},
[square, doubled]
);
func(9, 3); // [81, 6]
```

View File

@ -162,6 +162,7 @@ once:function
onUserInputChange:browser,event,advanced
orderBy:object,array
over:adapter,function
overArgs:adapter,function
palindrome:string
parseCookie:utility,string
partial:function