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 |
|
12 | With 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 |
|
28 | Compose N functions, works with promises too.
|
29 |
|
30 | ```
|
31 | const {compose} = 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 = compose(mult2,sum1);
|
38 | const sumAndMultDB = compose(dbSum,mult2,sum1);
|
39 |
|
40 | sumAndMult(1) // 4
|
41 | sumAndMultDB(1) // Promise(5)
|
42 | ```
|
43 |
|
44 | #### Pipe
|
45 |
|
46 | Compose N functions in reverse order, works promises too.
|
47 |
|
48 | ```
|
49 | const {pipe} = require('keyu');
|
50 |
|
51 | const sum1 = x => x+1;
|
52 | const mult2 = x => x*2;
|
53 | const dbSum = x => Promise.resolve(x+1);
|
54 |
|
55 | const sumAndMult = pipe(sum1,mult2);
|
56 | const sumAndMultDB = pipe(sum1, mult2, dbSum);
|
57 |
|
58 | sumAndMult(1) // 4
|
59 | sumAndMultDB(1) // Promise(5)
|
60 | ```
|
61 |
|
62 | ### Objects
|
63 |
|
64 | #### Map
|
65 |
|
66 | Maps an object based on the mapping function passed.
|
67 |
|
68 | ```
|
69 | let obj = {a:1,b:2,c:3};
|
70 |
|
71 | console.log(obj.map(value => value+1)) // {a:2}
|
72 | console.log(obj.map((value, key) => `${key}:${value+1}`)) // {a:'a:2'}
|
73 | ```
|
74 |
|
75 | functional programmming implementation:
|
76 |
|
77 | ```
|
78 | const {map,compose} = require('keyu');
|
79 |
|
80 | const sum1 = compose(map(x => x+1))
|
81 |
|
82 | sum1([1]) // [2]
|
83 | sum1({a:1}) // {a:2}
|
84 | ```
|
85 |
|
86 | #### Filter
|
87 |
|
88 | Filters an object based on the filter function passed.
|
89 |
|
90 | ```
|
91 | let obj = {a:1,b:2,c:3};
|
92 |
|
93 | console.log(obj.filter(value => value > 2)) // {c:3}
|
94 |
|
95 | console.log(obj.filter((value, key) => value > 1 && key === 'c')) // {c:3}
|
96 | ```
|
97 |
|
98 | functional programmming implementation:
|
99 |
|
100 | ```
|
101 | const {filter,compose} = require('keyu');
|
102 |
|
103 | const greaterThan5 = compose(filter(x => x > 5 ))
|
104 |
|
105 | greaterThan5([1,2,3,4,5,6,7,8]) // [6,7,8]
|
106 | greaterThan5({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 |
|
113 | Promise.all executes an array of promises in parallel and returns all of them either they are sucessful or not without failing.
|
114 |
|
115 | ```
|
116 | Promise.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 |
|
121 | Promise.best execute an array of promises in parallel and returns the first one to be sucessful.
|
122 |
|
123 | ```
|
124 | Promise.best([Promise.reject(1),Promise.resolve(2), Promise.resolve(3)]).then(console.log) // 2
|
125 |
|
126 | Promise.best([Promise.reject(1),Promise.reject(2), Promise.reject(3)]).catch(console.log) // [1,2,3]
|
127 | ```
|
128 |
|
129 | ### Conversions
|
130 |
|
131 | #### jsonOr
|
132 |
|
133 | Converts safetly any input into a json, if it fails returns the default function value.
|
134 |
|
135 | ```
|
136 | jsonOr(33)('{"a":1}') // -> {a:1}
|
137 | jsonOr(33)(null) // -> 33
|
138 | jsonOr(value => 33)(null) // -> 33
|
139 | ```
|
140 |
|
141 | #### floatOr
|
142 |
|
143 | Converts safetly any input into a float, if it fails returns the default function value.
|
144 |
|
145 | ```
|
146 | floatOr(33)('11.33') // -> 11.33
|
147 | floatOr(33)(null) // -> 33
|
148 | floatOr(value => 33)(null) // -> 33
|
149 | ```
|
150 |
|
151 | #### intOr
|
152 |
|
153 | Converts safetly any input into an integer, if it fails returns the default function value.
|
154 |
|
155 | ```
|
156 | intOr(33)('11') // -> 11
|
157 | intOr(33)(null) // -> 33
|
158 | intOr(value => 33)(null) // -> 33
|
159 | ```
|
160 |
|
161 | #### setPrecision
|
162 |
|
163 | Set the amount of decimals for any given float safelty or returns cero precision.
|
164 |
|
165 | ```
|
166 | setPrecision(3,'1.1234') // -> 1.234
|
167 | ```
|