-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathforward.ts
33 lines (31 loc) · 896 Bytes
/
forward.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import type { λ } from "./types.ts";
/**
* Given a function and its nth..last arguments, return a function accepting
* arguments 0..n-1.
*
* @example
* ```
* const divide = (dividend: number, divisor: number) => dividend / divisor
*
* // (dividend: number) => number
* const divideBy2 = forward(divide, 2)
*
* // prints: 0.5
* console.log(divideBy2(1))
* ```
*
* @example
* ```
* const fetchUrl = async (protocol: string, domain: string, path: string) =>
* await fetch(`${protocol}://${domain}/${path}`)
*
* const fetchRepo = forward(fetchUrl, 'github.com', 'MathisBullinger/froebel')
*
* const viaHTTPS = await fetchRepo('https')
* ```
*/
const forward = <T extends λ, PR extends any[]>(fun: T, ...argsRight: PR) =>
(
...argsLeft: Parameters<T> extends [...infer PL, ...PR] ? PL : never
): ReturnType<T> => fun(...argsLeft, ...argsRight);
export default forward;