@naturalcycles/js-lib 15.29.0 → 15.29.1
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.
- package/dist/unit/size.util.d.ts +2 -0
- package/dist/unit/size.util.js +51 -21
- package/package.json +2 -2
- package/src/unit/size.util.ts +33 -14
package/dist/unit/size.util.d.ts
CHANGED
|
@@ -9,5 +9,7 @@ export declare function _hb(b?: number): string;
|
|
|
9
9
|
* hc stands for "human count", similar to "human bytes" `_hb` function.
|
|
10
10
|
* Helpful to print big numbers, as it adds `K` (kilo), `M` (mega), etc to make
|
|
11
11
|
* them more readable.
|
|
12
|
+
*
|
|
13
|
+
* Implementation rule of thumb: aim to have up to 3 significant digits, cut the rest.
|
|
12
14
|
*/
|
|
13
15
|
export declare function _hc(c?: number): string;
|
package/dist/unit/size.util.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
+
const kb = 1024;
|
|
2
|
+
const mb = 1024 ** 2;
|
|
3
|
+
const gb = 1024 ** 3;
|
|
4
|
+
const tb = 1024 ** 4;
|
|
1
5
|
export function _gb(b) {
|
|
2
|
-
return Math.round(b /
|
|
6
|
+
return Math.round(b / gb);
|
|
3
7
|
}
|
|
4
8
|
export function _mb(b) {
|
|
5
|
-
return Math.round(b /
|
|
9
|
+
return Math.round(b / mb);
|
|
6
10
|
}
|
|
7
11
|
export function _kb(b) {
|
|
8
|
-
return Math.round(b /
|
|
12
|
+
return Math.round(b / kb);
|
|
9
13
|
}
|
|
10
14
|
/**
|
|
11
15
|
* Byte size to Human byte size string
|
|
@@ -13,35 +17,61 @@ export function _kb(b) {
|
|
|
13
17
|
export function _hb(b = 0) {
|
|
14
18
|
if (b < 100)
|
|
15
19
|
return `${Math.round(b)} byte(s)`;
|
|
16
|
-
if (b <
|
|
17
|
-
return `${(b /
|
|
18
|
-
if (b <
|
|
19
|
-
return `${
|
|
20
|
-
if (b <
|
|
21
|
-
return `${Math.round(b /
|
|
22
|
-
if (b <
|
|
23
|
-
return `${(b /
|
|
24
|
-
if (b <
|
|
25
|
-
return `${
|
|
26
|
-
if (b <
|
|
27
|
-
return `${(b /
|
|
28
|
-
|
|
20
|
+
if (b < 10 ** 4)
|
|
21
|
+
return `${(b / kb).toFixed(2)} Kb`;
|
|
22
|
+
if (b < 10 ** 5)
|
|
23
|
+
return `${(b / kb).toFixed(1)} Kb`;
|
|
24
|
+
if (b < 10 ** 6)
|
|
25
|
+
return `${Math.round(b / kb)} Kb`;
|
|
26
|
+
if (b < 10 ** 7)
|
|
27
|
+
return `${(b / mb).toFixed(2)} Mb`;
|
|
28
|
+
if (b < 10 ** 8)
|
|
29
|
+
return `${(b / mb).toFixed(1)} Mb`;
|
|
30
|
+
if (b < 10 ** 9)
|
|
31
|
+
return `${Math.round(b / mb)} Mb`;
|
|
32
|
+
if (b < 10 ** 10)
|
|
33
|
+
return `${(b / gb).toFixed(2)} Gb`;
|
|
34
|
+
if (b < 10 ** 11)
|
|
35
|
+
return `${(b / gb).toFixed(1)} Gb`;
|
|
36
|
+
if (b < 10 ** 12)
|
|
37
|
+
return `${Math.round(b / gb)} Gb`;
|
|
38
|
+
if (b < 10 ** 13)
|
|
39
|
+
return `${(b / tb).toFixed(2)} Tb`;
|
|
40
|
+
if (b < 10 ** 14)
|
|
41
|
+
return `${(b / tb).toFixed(1)} Tb`;
|
|
42
|
+
return `${Math.round(b / tb)} Tb`;
|
|
29
43
|
}
|
|
30
44
|
/**
|
|
31
45
|
* hc stands for "human count", similar to "human bytes" `_hb` function.
|
|
32
46
|
* Helpful to print big numbers, as it adds `K` (kilo), `M` (mega), etc to make
|
|
33
47
|
* them more readable.
|
|
48
|
+
*
|
|
49
|
+
* Implementation rule of thumb: aim to have up to 3 significant digits, cut the rest.
|
|
34
50
|
*/
|
|
35
51
|
export function _hc(c = 0) {
|
|
36
|
-
if (c < 10 **
|
|
52
|
+
if (c < 10 ** 3)
|
|
37
53
|
return String(Math.round(c));
|
|
54
|
+
if (c < 10 ** 4)
|
|
55
|
+
return (c / 10 ** 3).toFixed(2) + ' K';
|
|
56
|
+
if (c < 10 ** 5)
|
|
57
|
+
return (c / 10 ** 3).toFixed(1) + ' K';
|
|
38
58
|
if (c < 10 ** 6)
|
|
39
59
|
return Math.round(c / 10 ** 3) + ' K';
|
|
60
|
+
if (c < 10 ** 7)
|
|
61
|
+
return (c / 10 ** 6).toFixed(2) + ' M';
|
|
62
|
+
if (c < 10 ** 8)
|
|
63
|
+
return (c / 10 ** 6).toFixed(1) + ' M';
|
|
40
64
|
if (c < 10 ** 9)
|
|
41
|
-
return Math.round(c / 10 ** 6) + ' M';
|
|
65
|
+
return Math.round(c / 10 ** 6) + ' M';
|
|
66
|
+
if (c < 10 ** 10)
|
|
67
|
+
return (c / 10 ** 9).toFixed(2) + ' B';
|
|
68
|
+
if (c < 10 ** 11)
|
|
69
|
+
return (c / 10 ** 9).toFixed(1) + ' B';
|
|
42
70
|
if (c < 10 ** 12)
|
|
43
|
-
return Math.round(c / 10 ** 9) + ' B';
|
|
44
|
-
if (c < 10 **
|
|
45
|
-
return
|
|
71
|
+
return Math.round(c / 10 ** 9) + ' B';
|
|
72
|
+
if (c < 10 ** 13)
|
|
73
|
+
return (c / 10 ** 12).toFixed(2) + ' T';
|
|
74
|
+
if (c < 10 ** 14)
|
|
75
|
+
return (c / 10 ** 12).toFixed(1) + ' T';
|
|
46
76
|
return Math.round(c / 10 ** 12) + ' T';
|
|
47
77
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "15.29.
|
|
4
|
+
"version": "15.29.1",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"tslib": "^2",
|
|
7
7
|
"zod": "^4"
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"@types/semver": "^7",
|
|
13
13
|
"crypto-js": "^4",
|
|
14
14
|
"dayjs": "^1",
|
|
15
|
-
"@naturalcycles/dev-lib": "
|
|
15
|
+
"@naturalcycles/dev-lib": "18.4.2"
|
|
16
16
|
},
|
|
17
17
|
"exports": {
|
|
18
18
|
".": "./dist/index.js",
|
package/src/unit/size.util.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
+
const kb = 1024
|
|
2
|
+
const mb = 1024 ** 2
|
|
3
|
+
const gb = 1024 ** 3
|
|
4
|
+
const tb = 1024 ** 4
|
|
5
|
+
|
|
1
6
|
export function _gb(b: number): number {
|
|
2
|
-
return Math.round(b /
|
|
7
|
+
return Math.round(b / gb)
|
|
3
8
|
}
|
|
4
9
|
|
|
5
10
|
export function _mb(b: number): number {
|
|
6
|
-
return Math.round(b /
|
|
11
|
+
return Math.round(b / mb)
|
|
7
12
|
}
|
|
8
13
|
|
|
9
14
|
export function _kb(b: number): number {
|
|
10
|
-
return Math.round(b /
|
|
15
|
+
return Math.round(b / kb)
|
|
11
16
|
}
|
|
12
17
|
|
|
13
18
|
/**
|
|
@@ -15,25 +20,39 @@ export function _kb(b: number): number {
|
|
|
15
20
|
*/
|
|
16
21
|
export function _hb(b = 0): string {
|
|
17
22
|
if (b < 100) return `${Math.round(b)} byte(s)`
|
|
18
|
-
if (b <
|
|
19
|
-
if (b <
|
|
20
|
-
if (b <
|
|
21
|
-
if (b <
|
|
22
|
-
if (b <
|
|
23
|
-
if (b <
|
|
24
|
-
return `${
|
|
23
|
+
if (b < 10 ** 4) return `${(b / kb).toFixed(2)} Kb`
|
|
24
|
+
if (b < 10 ** 5) return `${(b / kb).toFixed(1)} Kb`
|
|
25
|
+
if (b < 10 ** 6) return `${Math.round(b / kb)} Kb`
|
|
26
|
+
if (b < 10 ** 7) return `${(b / mb).toFixed(2)} Mb`
|
|
27
|
+
if (b < 10 ** 8) return `${(b / mb).toFixed(1)} Mb`
|
|
28
|
+
if (b < 10 ** 9) return `${Math.round(b / mb)} Mb`
|
|
29
|
+
if (b < 10 ** 10) return `${(b / gb).toFixed(2)} Gb`
|
|
30
|
+
if (b < 10 ** 11) return `${(b / gb).toFixed(1)} Gb`
|
|
31
|
+
if (b < 10 ** 12) return `${Math.round(b / gb)} Gb`
|
|
32
|
+
if (b < 10 ** 13) return `${(b / tb).toFixed(2)} Tb`
|
|
33
|
+
if (b < 10 ** 14) return `${(b / tb).toFixed(1)} Tb`
|
|
34
|
+
return `${Math.round(b / tb)} Tb`
|
|
25
35
|
}
|
|
26
36
|
|
|
27
37
|
/**
|
|
28
38
|
* hc stands for "human count", similar to "human bytes" `_hb` function.
|
|
29
39
|
* Helpful to print big numbers, as it adds `K` (kilo), `M` (mega), etc to make
|
|
30
40
|
* them more readable.
|
|
41
|
+
*
|
|
42
|
+
* Implementation rule of thumb: aim to have up to 3 significant digits, cut the rest.
|
|
31
43
|
*/
|
|
32
44
|
export function _hc(c = 0): string {
|
|
33
|
-
if (c < 10 **
|
|
45
|
+
if (c < 10 ** 3) return String(Math.round(c))
|
|
46
|
+
if (c < 10 ** 4) return (c / 10 ** 3).toFixed(2) + ' K'
|
|
47
|
+
if (c < 10 ** 5) return (c / 10 ** 3).toFixed(1) + ' K'
|
|
34
48
|
if (c < 10 ** 6) return Math.round(c / 10 ** 3) + ' K'
|
|
35
|
-
if (c < 10 **
|
|
36
|
-
if (c < 10 **
|
|
37
|
-
if (c < 10 **
|
|
49
|
+
if (c < 10 ** 7) return (c / 10 ** 6).toFixed(2) + ' M'
|
|
50
|
+
if (c < 10 ** 8) return (c / 10 ** 6).toFixed(1) + ' M'
|
|
51
|
+
if (c < 10 ** 9) return Math.round(c / 10 ** 6) + ' M'
|
|
52
|
+
if (c < 10 ** 10) return (c / 10 ** 9).toFixed(2) + ' B'
|
|
53
|
+
if (c < 10 ** 11) return (c / 10 ** 9).toFixed(1) + ' B'
|
|
54
|
+
if (c < 10 ** 12) return Math.round(c / 10 ** 9) + ' B'
|
|
55
|
+
if (c < 10 ** 13) return (c / 10 ** 12).toFixed(2) + ' T'
|
|
56
|
+
if (c < 10 ** 14) return (c / 10 ** 12).toFixed(1) + ' T'
|
|
38
57
|
return Math.round(c / 10 ** 12) + ' T'
|
|
39
58
|
}
|