@node-mf/utils 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of @node-mf/utils might be problematic. Click here for more details.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/disk-size.js +91 -0
- package/duration.js +61 -0
- package/index.js +1 -0
- package/package.json +13 -0
- package/performance.js +33 -0
- package/readable.js +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
GNU License
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2012-2018 by various contributors (see AUTHORS)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
package/disk-size.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// 10ⁿ
|
|
4
|
+
exports.DEC_K = 1e3;
|
|
5
|
+
exports.DEC_M = 1e6;
|
|
6
|
+
exports.DEC_G = 1e9;
|
|
7
|
+
exports.DEC_T = 1e12;
|
|
8
|
+
exports.DEC_P = 1e15;
|
|
9
|
+
// 2ⁿ
|
|
10
|
+
exports.K = 2 ** 10;
|
|
11
|
+
exports.M = 2 ** 20;
|
|
12
|
+
exports.G = 2 ** 30;
|
|
13
|
+
exports.T = 2 ** 40;
|
|
14
|
+
exports.P = 2 ** 50;
|
|
15
|
+
const SI = [
|
|
16
|
+
['P', exports.DEC_P],
|
|
17
|
+
['T', exports.DEC_T],
|
|
18
|
+
['G', exports.DEC_G],
|
|
19
|
+
['M', exports.DEC_M],
|
|
20
|
+
['k', exports.DEC_K],
|
|
21
|
+
];
|
|
22
|
+
const IEC = [
|
|
23
|
+
['Pi', exports.P],
|
|
24
|
+
['Ti', exports.T],
|
|
25
|
+
['Gi', exports.G],
|
|
26
|
+
['Mi', exports.M],
|
|
27
|
+
['Ki', exports.K],
|
|
28
|
+
];
|
|
29
|
+
const JEDEC = [
|
|
30
|
+
['P', exports.P],
|
|
31
|
+
['T', exports.T],
|
|
32
|
+
['G', exports.G],
|
|
33
|
+
['M', exports.M],
|
|
34
|
+
['K', exports.K],
|
|
35
|
+
];
|
|
36
|
+
const Standards = {
|
|
37
|
+
SI,
|
|
38
|
+
IEC,
|
|
39
|
+
JEDEC,
|
|
40
|
+
};
|
|
41
|
+
function isStandardName(std) {
|
|
42
|
+
return std === 'SI' || std === 'IEC' || std === 'JEDEC';
|
|
43
|
+
}
|
|
44
|
+
const one = /^−?1$/;
|
|
45
|
+
const defaultOptions = {
|
|
46
|
+
std: 'SI',
|
|
47
|
+
decimalPlaces: 2,
|
|
48
|
+
keepTrailingZeroes: false,
|
|
49
|
+
allowMultiples: ['K', 'M', 'G', 'T', 'P'],
|
|
50
|
+
render(literal, symbol) {
|
|
51
|
+
if (symbol)
|
|
52
|
+
return `${literal} ${symbol}B`;
|
|
53
|
+
return `${literal} ${one.test(literal) ? 'byte' : 'bytes'}`;
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
const trailingZeroes = /\.?0*$/;
|
|
57
|
+
function toFixed(n, decimalPlaces, keepTrailingZeroes) {
|
|
58
|
+
const a = n.toFixed(decimalPlaces);
|
|
59
|
+
if (keepTrailingZeroes)
|
|
60
|
+
return a;
|
|
61
|
+
return a.replace(trailingZeroes, '');
|
|
62
|
+
}
|
|
63
|
+
function sizeFormatter(options) {
|
|
64
|
+
const opt = Object.assign({}, defaultOptions, options);
|
|
65
|
+
if (!isStandardName(opt.std)) {
|
|
66
|
+
throw Error(`Unknown std '${opt.std}'`);
|
|
67
|
+
}
|
|
68
|
+
const allowMultiples = new Set;
|
|
69
|
+
opt.allowMultiples.forEach(a => {
|
|
70
|
+
allowMultiples.add(a.toUpperCase());
|
|
71
|
+
allowMultiples.add(a.toLowerCase());
|
|
72
|
+
});
|
|
73
|
+
return function (n) {
|
|
74
|
+
let sign = '';
|
|
75
|
+
if (n < 0) {
|
|
76
|
+
sign = '−';
|
|
77
|
+
n = -n;
|
|
78
|
+
}
|
|
79
|
+
let literal = '' + n;
|
|
80
|
+
let symbol = '';
|
|
81
|
+
for (const [a, b] of Standards[opt.std]) {
|
|
82
|
+
if (n >= b && allowMultiples.has(a[0])) {
|
|
83
|
+
literal = toFixed(n / b, opt.decimalPlaces, opt.keepTrailingZeroes);
|
|
84
|
+
symbol = a;
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return opt.render(sign + literal, symbol);
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
exports.sizeFormatter = sizeFormatter;
|
package/duration.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// const ACTUAL_YEAR_DAYS = 365.242199
|
|
4
|
+
const GREGORIAN_YEAR_DAYS = 365.242500;
|
|
5
|
+
exports.DUR_MS = 1;
|
|
6
|
+
exports.DUR_S = 1000 * exports.DUR_MS;
|
|
7
|
+
exports.DUR_M = 60 * exports.DUR_S;
|
|
8
|
+
exports.DUR_H = 60 * exports.DUR_M;
|
|
9
|
+
exports.DUR_D = 24 * exports.DUR_H;
|
|
10
|
+
exports.DUR_W = 7 * exports.DUR_D;
|
|
11
|
+
exports.DUR_MO = GREGORIAN_YEAR_DAYS / 12 * exports.DUR_D;
|
|
12
|
+
exports.DUR_Y = GREGORIAN_YEAR_DAYS * exports.DUR_D;
|
|
13
|
+
const Units = [
|
|
14
|
+
['y', exports.DUR_Y],
|
|
15
|
+
['mo', exports.DUR_MO],
|
|
16
|
+
['w', exports.DUR_W],
|
|
17
|
+
['d', exports.DUR_D],
|
|
18
|
+
['h', exports.DUR_H],
|
|
19
|
+
['m', exports.DUR_M],
|
|
20
|
+
['s', exports.DUR_S],
|
|
21
|
+
['ms', exports.DUR_MS],
|
|
22
|
+
];
|
|
23
|
+
const defaultOptions = {
|
|
24
|
+
allowMultiples: ['y', 'mo', 'd', 'h', 'm', 's'],
|
|
25
|
+
keepNonLeadingZeroes: false,
|
|
26
|
+
render(parts) {
|
|
27
|
+
return parts.map(({ literal, symbol }) => `${literal}${symbol}`).join(' ');
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
function durationFormatter(options) {
|
|
31
|
+
const opt = Object.assign({}, defaultOptions, options);
|
|
32
|
+
const allowMultiples = new Set(opt.allowMultiples.map(a => a.toLowerCase()));
|
|
33
|
+
return function (n) {
|
|
34
|
+
const parts = [];
|
|
35
|
+
const short = { literal: '0', symbol: 'ms' };
|
|
36
|
+
let sign = '';
|
|
37
|
+
if (n < 0) {
|
|
38
|
+
sign = '−';
|
|
39
|
+
n = -n;
|
|
40
|
+
}
|
|
41
|
+
for (const [a, b] of Units) {
|
|
42
|
+
if (allowMultiples.has(a)) {
|
|
43
|
+
if (n >= b) {
|
|
44
|
+
parts.push({ literal: '' + Math.floor(n / b), symbol: a });
|
|
45
|
+
n %= b;
|
|
46
|
+
}
|
|
47
|
+
else if (!parts.length) {
|
|
48
|
+
short.symbol = a;
|
|
49
|
+
}
|
|
50
|
+
else if (opt.keepNonLeadingZeroes) {
|
|
51
|
+
parts.push({ literal: '0', symbol: a });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (sign && parts.length && parts[0].literal != '0') {
|
|
56
|
+
parts[0].literal = sign + parts[0].literal;
|
|
57
|
+
}
|
|
58
|
+
return opt.render(parts.length ? parts : [short]);
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
exports.durationFormatter = durationFormatter;
|