-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
50 lines (42 loc) · 961 Bytes
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const dayjs = require('dayjs');
const numeral = require('numeral');
const R = require('ramda');
function calcRatio(open, closed) {
const total = R.add(open, closed);
return open / total;
}
function formatDate(date) {
return dayjs(date).format('YYYY-MM-DD');
}
function formatNumber(x) {
return numeral(x).format('0,0');
}
function formatPercentage(x) {
return numeral(x).format('0.00%');
}
// copied from bundlephobia source
function formatSize(value) {
let unit, size;
if (Math.log10(value) < 3) {
unit = 'B';
size = value;
} else if (Math.log10(value) < 6) {
unit = 'kB';
size = value / 1024;
} else {
unit = 'mB';
size = value / 1024 / 1024;
}
return { unit, size };
}
const fillWith = R.curry((defaultValue, length, list) =>
R.times(idx => R.defaultTo(defaultValue, list[idx]), length),
);
module.exports = {
calcRatio,
fillWith,
formatDate,
formatNumber,
formatPercentage,
formatSize,
};