UNPKG

4.65 kBMarkdownView Raw
1![logo](logo.png)
2
3[![Build Status](https://travis-ci.org/nerac/keyu.svg?branch=master)](https://travis-ci.org/nerac/keyu)
4[![Coverage Status](https://coveralls.io/repos/github/nerac/keyu/badge.svg?branch=master)](https://coveralls.io/github/nerac/keyu?branch=master)
5[![install size](https://packagephobia.now.sh/badge?p=keyu)](https://packagephobia.now.sh/result?p=keyu)
6[![Known Vulnerabilities](https://snyk.io/test/npm/keyu/badge.svg)](https://snyk.io/test/npm/keyu)
7
8[![NPM](https://nodei.co/npm/keyu.png)](https://nodei.co/npm/keyu/)
9
10**Keyu** (key utilities) provides **flexible** utilities to help you build **solid** projects <u>without repeating yourself</u>.
11
12With this utilities you will be able to:
13
14- Build some functional programming code.
15- Work with asyncronous code like if it was syncronous\*.
16- Iterate objects in same way you iterate arrays.
17- Apply some advanced concurrency patterns ([Rob Pike](https://www.youtube.com/watch?v=f6kdp27TYZs)).
18- Convert integers, floats or json correctly and safetly.
19
20<small>\*compose functions independently if they return promises or actual values</small>
21
22## Utilities
23
24### Functional programming
25
26#### Compose
27
28Compose N functions, works with promises too.
29
30```
31const {compose} = require('keyu');
32
33const sum1 = x => x+1;
34const mult2 = x => x*2;
35const dbSum = x => Promise.resolve(x+1);
36
37const sumAndMult = compose(mult2,sum1);
38const sumAndMultDB = compose(dbSum,mult2,sum1);
39
40sumAndMult(1) // 4
41sumAndMultDB(1) // Promise(5)
42```
43
44#### Pipe
45
46Compose N functions in reverse order, works promises too.
47
48```
49const {pipe} = require('keyu');
50
51const sum1 = x => x+1;
52const mult2 = x => x*2;
53const dbSum = x => Promise.resolve(x+1);
54
55const sumAndMult = pipe(sum1,mult2);
56const sumAndMultDB = pipe(sum1, mult2, dbSum);
57
58sumAndMult(1) // 4
59sumAndMultDB(1) // Promise(5)
60```
61
62### Objects
63
64#### Map
65
66Maps an object based on the mapping function passed.
67
68```
69let obj = {a:1,b:2,c:3};
70
71console.log(obj.map(value => value+1)) // {a:2}
72console.log(obj.map((value, key) => `${key}:${value+1}`)) // {a:'a:2'}
73```
74
75functional programmming implementation:
76
77```
78const {map,compose} = require('keyu');
79
80const sum1 = compose(map(x => x+1))
81
82sum1([1]) // [2]
83sum1({a:1}) // {a:2}
84```
85
86#### Filter
87
88Filters an object based on the filter function passed.
89
90```
91let obj = {a:1,b:2,c:3};
92
93console.log(obj.filter(value => value > 2)) // {c:3}
94
95console.log(obj.filter((value, key) => value > 1 && key === 'c')) // {c:3}
96```
97
98functional programmming implementation:
99
100```
101const {filter,compose} = require('keyu');
102
103const greaterThan5 = compose(filter(x => x > 5 ))
104
105greaterThan5([1,2,3,4,5,6,7,8]) // [6,7,8]
106greaterThan5({a:1,b:2,c:3,d:4,e:5,f:6,g:7}) // {f:6,g:7}
107```
108
109#### Reduce
110
111### Concurrency
112
113#### Promise.any
114
115Promise.all executes an array of promises in parallel and returns all of them either they are sucessful or not without failing.
116
117```
118Promise.any([Promise.resolve(1),Promise.reject(2), Promise.resolve(3)]).then(console.log) // [ { value: 1 }, { error: 2 }, { value: 3 } ]
119```
120
121#### Promise.best
122
123Promise.best execute an array of promises in parallel and returns the first one to be sucessful.
124
125```
126Promise.best([Promise.reject(1),Promise.resolve(2), Promise.resolve(3)]).then(console.log) // 2
127
128Promise.best([Promise.reject(1),Promise.reject(2), Promise.reject(3)]).catch(console.log) // [1,2,3]
129```
130
131### Conversions
132
133#### jsonOr
134
135Converts safetly any input into a json, if it fails returns the default function value.
136
137```
138jsonOr(33)('{"a":1}') // -> {a:1}
139jsonOr(33)(null) // -> 33
140jsonOr(value => 33)(null) // -> 33
141```
142
143#### floatOr
144
145Converts safetly any input into a float, if it fails returns the default function value.
146
147```
148floatOr(33)('11.33') // -> 11.33
149floatOr(33)(null) // -> 33
150floatOr(value => 33)(null) // -> 33
151```
152
153#### intOr
154
155Converts safetly any input into an integer, if it fails returns the default function value.
156
157```
158intOr(33)('11') // -> 11
159intOr(33)(null) // -> 33
160intOr(value => 33)(null) // -> 33
161```
162
163#### setPrecision
164
165Set the amount of decimals for any given float safelty or returns cero precision.
166
167```
168setPrecision(3,'1.1234') // -> 1.234
169```
170
171### Logic
172
173#### Either
174
175Is a try/catch in a functional way, it will try to execute the first function and if this one throws an exception will return the value of the second one.
176
177```
178let parser = either(JSON.parse,33);
179
180parser("") // => 33
181parser('{}') // => {}
182
183```
184
185#### fnOrValue
186
187Detects if the value passed is a function or a value and returns the function applied to the value or the value itself.
188
189### Identity
190
191Function that given the same input returns the same output.
192
193### Not
194
195Negates any function that recieves