1 | # Keyu
|
2 |
|
3 | [![Build Status](https://travis-ci.org/nerac/keyu.svg?branch=master)](https://travis-ci.org/nerac/keyu)
|
4 | [![install size](https://packagephobia.now.sh/badge?p=keyu)](https://packagephobia.now.sh/result?p=keyu)
|
5 | [![Known Vulnerabilities](https://snyk.io/test/npm/keyu/badge.svg)](https://snyk.io/test/npm/keyu)
|
6 |
|
7 | [![NPM](https://nodei.co/npm/keyu.png)](https://nodei.co/npm/keyu/)
|
8 |
|
9 | **Key u**tilities that you mis when you develop a javascript project.
|
10 |
|
11 | ## Utilities
|
12 |
|
13 | ### Functional programming
|
14 |
|
15 | #### Compose
|
16 |
|
17 | Compose N functions, works with promises too.
|
18 |
|
19 | ```
|
20 | const {compose} = require('keyu');
|
21 |
|
22 | const sum1 = x => x+1;
|
23 | const mult2 = x => x*2;
|
24 | const dbSum = x => Promise.resolve(x+1);
|
25 |
|
26 | const sumAndMult = compose(mult2,sum1);
|
27 | const sumAndMultDB = compose(dbSum,mult2,sum1);
|
28 |
|
29 | sumAndMult(1) // 4
|
30 | sumAndMultDB(1) // Promise(5)
|
31 | ```
|
32 |
|
33 | #### Pipe
|
34 |
|
35 | Compose N functions in reverse order, works promises too.
|
36 |
|
37 | ```
|
38 | const {pipe} = require('keyu');
|
39 |
|
40 | const sum1 = x => x+1;
|
41 | const mult2 = x => x*2;
|
42 | const dbSum = x => Promise.resolve(x+1);
|
43 |
|
44 | const sumAndMult = pipe(sum1,mult2);
|
45 | const sumAndMultDB = pipe(sum1, mult2, dbSum);
|
46 |
|
47 | sumAndMult(1) // 4
|
48 | sumAndMultDB(1) // Promise(5)
|
49 | ```
|
50 |
|
51 | ### Objects
|
52 |
|
53 | #### Map
|
54 |
|
55 | Maps an object based on the mapping function passed.
|
56 |
|
57 | ```
|
58 | let obj = {a:1,b:2,c:3};
|
59 |
|
60 | console.log(obj.map(value => value+1)) // {a:2}
|
61 | console.log(obj.map((value, key) => `${key}:${value+1}`)) // {a:'a:2'}
|
62 | ```
|
63 |
|
64 | functional programmming implementation:
|
65 |
|
66 | ```
|
67 | const {map,compose} = require('keyu');
|
68 |
|
69 | const sum1 = compose(map(x => x+1))
|
70 |
|
71 | sum1([1]) // [2]
|
72 | sum1({a:1}) // {a:2}
|
73 | ```
|
74 |
|
75 | #### Filter
|
76 |
|
77 | Filters an object based on the filter function passed.
|
78 |
|
79 | ```
|
80 | let obj = {a:1,b:2,c:3};
|
81 |
|
82 | console.log(obj.filter(value => value > 2)) // {c:3}
|
83 |
|
84 | console.log(obj.filter((value, key) => value > 1 && key === 'c')) // {c:3}
|
85 | ```
|
86 |
|
87 | functional programmming implementation:
|
88 |
|
89 | ```
|
90 | const {filter,compose} = require('keyu');
|
91 |
|
92 | const greaterThan5 = compose(filter(x => x > 5 ))
|
93 |
|
94 | greaterThan5([1,2,3,4,5,6,7,8]) // [6,7,8]
|
95 | greaterThan5({a:1,b:2,c:3,d:4,e:5,f:6,g:7}) // {f:6,g:7}
|
96 | ```
|
97 |
|
98 | ### Concurrency
|
99 |
|
100 | #### Promise.any
|
101 |
|
102 | Promise.all executes an array of promises in parallel and returns all of them either they are sucessful or not without failing.
|
103 |
|
104 | ```
|
105 | Promise.any([Promise.resolve(1),Promise.reject(2), Promise.resolve(3)]).then(console.log) // [ { value: 1 }, { error: 2 }, { value: 3 } ]
|
106 | ```
|
107 |
|
108 | #### Promise.best
|
109 |
|
110 | Promise.best execute an array of promises in parallel and returns the first one to be sucessful.
|
111 |
|
112 | ```
|
113 | Promise.best([Promise.reject(1),Promise.resolve(2), Promise.resolve(3)]).then(console.log) // 2
|
114 |
|
115 | Promise.best([Promise.reject(1),Promise.reject(2), Promise.reject(3)]).catch(console.log) // [1,2,3]
|
116 | ```
|
117 |
|
118 | ### Conversions
|
119 |
|
120 | #### jsonOr
|
121 |
|
122 | Converts safetly any input into a json, if it fails returns the default function value.
|
123 |
|
124 | ```
|
125 | jsonOr(33)('{"a":1}') // -> {a:1}
|
126 | jsonOr(33)(null) // -> 33
|
127 | jsonOr(value => 33)(null) // -> 33
|
128 | ```
|
129 |
|
130 | #### floatOr
|
131 |
|
132 | Converts safetly any input into a float, if it fails returns the default function value.
|
133 |
|
134 | ```
|
135 | floatOr(33)('11.33') // -> 11.33
|
136 | floatOr(33)(null) // -> 33
|
137 | floatOr(value => 33)(null) // -> 33
|
138 | ```
|
139 |
|
140 | #### intOr
|
141 |
|
142 | Converts safetly any input into an integer, if it fails returns the default function value.
|
143 |
|
144 | ```
|
145 | intOr(33)('11') // -> 11
|
146 | intOr(33)(null) // -> 33
|
147 | intOr(value => 33)(null) // -> 33
|
148 | ```
|
149 |
|
150 | #### setPrecision
|
151 |
|
152 | Set the amount of decimals for any given float safelty or returns cero precision.
|
153 |
|
154 | ```
|
155 | setPrecision(3,'1.1234') // -> 1.234
|
156 | ```
|