UNPKG

4.11 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### Concurrency
110
111#### Promise.any
112
113Promise.all executes an array of promises in parallel and returns all of them either they are sucessful or not without failing.
114
115```
116Promise.any([Promise.resolve(1),Promise.reject(2), Promise.resolve(3)]).then(console.log) // [ { value: 1 }, { error: 2 }, { value: 3 } ]
117```
118
119#### Promise.best
120
121Promise.best execute an array of promises in parallel and returns the first one to be sucessful.
122
123```
124Promise.best([Promise.reject(1),Promise.resolve(2), Promise.resolve(3)]).then(console.log) // 2
125
126Promise.best([Promise.reject(1),Promise.reject(2), Promise.reject(3)]).catch(console.log) // [1,2,3]
127```
128
129### Conversions
130
131#### jsonOr
132
133Converts safetly any input into a json, if it fails returns the default function value.
134
135```
136jsonOr(33)('{"a":1}') // -> {a:1}
137jsonOr(33)(null) // -> 33
138jsonOr(value => 33)(null) // -> 33
139```
140
141#### floatOr
142
143Converts safetly any input into a float, if it fails returns the default function value.
144
145```
146floatOr(33)('11.33') // -> 11.33
147floatOr(33)(null) // -> 33
148floatOr(value => 33)(null) // -> 33
149```
150
151#### intOr
152
153Converts safetly any input into an integer, if it fails returns the default function value.
154
155```
156intOr(33)('11') // -> 11
157intOr(33)(null) // -> 33
158intOr(value => 33)(null) // -> 33
159```
160
161#### setPrecision
162
163Set the amount of decimals for any given float safelty or returns cero precision.
164
165```
166setPrecision(3,'1.1234') // -> 1.234
167```