1 | # Keyu
|
2 |
|
3 | Key utilities you will need when you work in any javascript project.
|
4 |
|
5 | ## Utilities
|
6 |
|
7 | ### Functional programming
|
8 |
|
9 | #### Compose
|
10 |
|
11 | Composes N functions into another one, also accept functions that a return Promise.
|
12 |
|
13 | ```
|
14 | const {compose} = require('keyu');
|
15 |
|
16 | const sum1 = x => x+1;
|
17 | const mult2 = x => x*2;
|
18 | const dbSum = x => Promise.resolve(x+1);
|
19 |
|
20 | const sumAndMult = compose(mult2,sum1);
|
21 | const sumAndMultDB = compose(dbSum,mult2,sum1);
|
22 |
|
23 | sumAndMult(1) // 4
|
24 | sumAndMultDB(1) // Promise(5)
|
25 | ```
|
26 | #### Pipe
|
27 |
|
28 | Pipe N functions into another one, also accept functions that a return Promise.
|
29 |
|
30 | ```
|
31 | const {pipe} = require('keyu');
|
32 |
|
33 | const sum1 = x => x+1;
|
34 | const mult2 = x => x*2;
|
35 | const dbSum = x => Promise.resolve(x+1);
|
36 |
|
37 | const sumAndMult = pipe(sum1,mult2);
|
38 | const sumAndMultDB = pipe(sum1, mult2, dbSum);
|
39 |
|
40 | sumAndMult(1) // 4
|
41 | sumAndMultDB(1) // Promise(5)
|
42 | ```
|
43 |
|
44 | ### Objects
|
45 |
|
46 | #### Map
|
47 |
|
48 | Maps over every key of an object
|
49 | ```
|
50 | let obj = {a:1,b:2,c:3};
|
51 |
|
52 | console.log(obj.map(value => value+1)) // {a:2}
|
53 | console.log(obj.map((value, key) => `${key}:${value+1}`)) // {a:'a:2'}
|
54 | ```
|
55 |
|
56 | #### Filter
|
57 |
|
58 | Filters an object for each single key
|
59 | ```
|
60 | let obj = {a:1,b:2,c:3};
|
61 |
|
62 | console.log(obj.filter(value => value > 2)) // {c:3}
|
63 |
|
64 | console.log(obj.filter((value, key) => value > 1 && key === 'c')) // {c:3}
|
65 | ```
|
66 |
|