forked from reduxjs/redux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompose.ts
68 lines (59 loc) · 1.43 KB
/
compose.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { compose } from 'redux'
// adapted from DefinitelyTyped/compose-function
const numberToNumber = (a: number): number => a + 2
const numberToString = (a: number): string => 'foo'
const stringToNumber = (a: string): number => 5
const t1: number = compose(
numberToNumber,
numberToNumber
)(5)
const t2: string = compose(
numberToString,
numberToNumber
)(5)
const t3: string = compose(
numberToString,
stringToNumber
)('f')
const t4: (a: string) => number = compose(
(f: (a: string) => number) => (p: string) => 5,
(f: (a: number) => string) => (p: string) => 4
)(numberToString)
const t5: number = compose(
stringToNumber,
numberToString,
numberToNumber
)(5)
const t6: string = compose(
numberToString,
stringToNumber,
numberToString,
numberToNumber
)(5)
// rest signature
const t7: string = compose<string>(
numberToString,
numberToNumber,
stringToNumber,
numberToString,
stringToNumber
)('fo')
const multiArgFn = (a: string, b: number, c: boolean): string => 'foo'
const t8: string = compose(multiArgFn)('bar', 42, true)
const t9: number = compose(
stringToNumber,
multiArgFn
)('bar', 42, true)
const t10: string = compose(
numberToString,
stringToNumber,
multiArgFn
)('bar', 42, true)
const t11: number = compose(
stringToNumber,
numberToString,
stringToNumber,
multiArgFn
)('bar', 42, true)
const funcs = [stringToNumber, numberToString, stringToNumber]
const t12 = compose(...funcs)('bar')