UNPKG

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