@jsenv/humanize 1.0.0 → 1.1.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/README.md +25 -10
- package/dist/browser/jsenv_humanize_browser.js +96 -2
- package/dist/node/jsenv_humanize_node.js +1470 -1531
- package/package.json +3 -3
- package/src/.jsenv/jsenv_tests_output.txt +27 -30
- package/src/_tests/byte/file_size.test.js +2 -2
- package/src/_tests/byte/memory.test.js +11 -0
- package/src/_tests/js_value/array.test.js +14 -14
- package/src/_tests/js_value/biginit.test.js +10 -10
- package/src/_tests/js_value/boolean.test.js +12 -12
- package/src/_tests/js_value/date.test.js +4 -4
- package/src/_tests/js_value/error.test.js +8 -8
- package/src/_tests/js_value/function.test.js +12 -12
- package/src/_tests/js_value/instance.test.js +2 -2
- package/src/_tests/js_value/null.test.js +2 -2
- package/src/_tests/js_value/number.test.js +54 -54
- package/src/_tests/js_value/object.test.js +34 -34
- package/src/_tests/js_value/regexp.test.js +4 -4
- package/src/_tests/js_value/string.test.js +32 -32
- package/src/_tests/js_value/symbol.test.js +6 -6
- package/src/_tests/js_value/undefined.test.js +2 -2
- package/src/_tests/log/logger.test.mjs +14 -14
- package/src/_tests/log/spinner.test.mjs +6 -6
- package/src/_tests/percentage/percentage_distribution.test.mjs +2 -2
- package/src/_tests/time/decimals.test.js +40 -40
- package/src/_tests/time/duration.test.js +2 -2
- package/src/_tests/time/ellapsed.test.js +2 -2
- package/src/ansi/ansi_browser.js +3 -0
- package/src/ansi/ansi_node.js +16 -0
- package/src/ansi/ansi_runtime_agnostic.js +33 -0
- package/src/byte/byte.js +1 -1
- package/src/js_value/humanize.js +1 -1
- package/src/js_value/string.js +1 -1
- package/src/log/dynamic_log.js +6 -4
- package/src/log/spinner.js +1 -1
- package/src/log/task_log.js +1 -1
- package/src/main_browser.js +3 -5
- package/src/main_node.js +9 -13
- package/src/unicode/unicode_browser.js +7 -0
- package/src/unicode/unicode_node.js +8 -0
- package/src/unicode/unicode_runtime_agnostic.js +53 -0
- package/src/_tests/byte/memory_usage.test.js +0 -11
- package/src/log/ansi.js +0 -37
- package/src/log/unicode.js +0 -52
package/README.md
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
# humanize [](https://www.npmjs.com/package/@jsenv/humanize)
|
|
2
2
|
|
|
3
|
-
`@jsenv/humanize`
|
|
3
|
+
`@jsenv/humanize` helps to generate messages meant to be read by humans
|
|
4
|
+
|
|
5
|
+
It is written in ES6 and compatible with browsers and Node.js.
|
|
6
|
+
|
|
7
|
+
# API
|
|
8
|
+
|
|
9
|
+
## humanize(jsValue)
|
|
10
|
+
|
|
11
|
+
Turns a JavaScript value into a string meant to be read by a human.
|
|
4
12
|
|
|
5
13
|
```js
|
|
6
14
|
import { humanize } from "@jsenv/humanize";
|
|
@@ -15,7 +23,6 @@ console.log(
|
|
|
15
23
|
```
|
|
16
24
|
|
|
17
25
|
```console
|
|
18
|
-
❯ node ./docs/demo.mjs
|
|
19
26
|
{
|
|
20
27
|
"boolean": true,
|
|
21
28
|
"number": 10,
|
|
@@ -23,13 +30,21 @@ console.log(
|
|
|
23
30
|
}
|
|
24
31
|
```
|
|
25
32
|
|
|
26
|
-
|
|
33
|
+
### Comparison with JSON.stringify
|
|
34
|
+
|
|
35
|
+
Table comparing `JSON.stringify` and `humanize()` to demonstrates how inspect focuses on readability and accuracy.
|
|
27
36
|
|
|
28
|
-
|
|
37
|
+
| value | JSON.stringify | humanize |
|
|
38
|
+
| --------- | -------------- | ----------- |
|
|
39
|
+
| 123456789 | "123456789" | 123_456_789 |
|
|
40
|
+
| Infinity | "null" | Infinity |
|
|
41
|
+
| -0 | "0" | -0 |
|
|
42
|
+
| '"' | '"\\""' | '"' |
|
|
29
43
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
44
|
+
## humanizeDuration(ms, options)
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
import { humanizeDuration } from "@jsenv/humanize";
|
|
48
|
+
|
|
49
|
+
humanizeDuration(61_421); // "1 minute and 1 second"
|
|
50
|
+
```
|
|
@@ -7,6 +7,100 @@ const createDetailedMessage = (message, details = {}) => {
|
|
|
7
7
|
return string;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
+
// https://github.com/Marak/colors.js/blob/master/lib/styles.js
|
|
11
|
+
// https://stackoverflow.com/a/75985833/2634179
|
|
12
|
+
const RESET = "\x1b[0m";
|
|
13
|
+
const createAnsi = ({
|
|
14
|
+
supported
|
|
15
|
+
}) => {
|
|
16
|
+
const ANSI = {
|
|
17
|
+
supported,
|
|
18
|
+
RED: "\x1b[31m",
|
|
19
|
+
GREEN: "\x1b[32m",
|
|
20
|
+
YELLOW: "\x1b[33m",
|
|
21
|
+
BLUE: "\x1b[34m",
|
|
22
|
+
MAGENTA: "\x1b[35m",
|
|
23
|
+
CYAN: "\x1b[36m",
|
|
24
|
+
GREY: "\x1b[90m",
|
|
25
|
+
color: (text, ANSI_COLOR) => {
|
|
26
|
+
return ANSI.supported && ANSI_COLOR ? "".concat(ANSI_COLOR).concat(text).concat(RESET) : text;
|
|
27
|
+
},
|
|
28
|
+
BOLD: "\x1b[1m",
|
|
29
|
+
UNDERLINE: "\x1b[4m",
|
|
30
|
+
STRIKE: "\x1b[9m",
|
|
31
|
+
effect: (text, ANSI_EFFECT) => {
|
|
32
|
+
return ANSI.supported && ANSI_EFFECT ? "".concat(ANSI_EFFECT).concat(text).concat(RESET) : text;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
return ANSI;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const ANSI = createAnsi({
|
|
39
|
+
supported: true
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// see also https://github.com/sindresorhus/figures
|
|
43
|
+
|
|
44
|
+
const createUnicode = ({
|
|
45
|
+
supported,
|
|
46
|
+
ANSI
|
|
47
|
+
}) => {
|
|
48
|
+
const UNICODE = {
|
|
49
|
+
supported,
|
|
50
|
+
get COMMAND_RAW() {
|
|
51
|
+
return UNICODE.supported ? "\u276F" : ">";
|
|
52
|
+
},
|
|
53
|
+
get OK_RAW() {
|
|
54
|
+
return UNICODE.supported ? "\u2714" : "\u221A";
|
|
55
|
+
},
|
|
56
|
+
get FAILURE_RAW() {
|
|
57
|
+
return UNICODE.supported ? "\u2716" : "\xD7";
|
|
58
|
+
},
|
|
59
|
+
get DEBUG_RAW() {
|
|
60
|
+
return UNICODE.supported ? "\u25C6" : "\u2666";
|
|
61
|
+
},
|
|
62
|
+
get INFO_RAW() {
|
|
63
|
+
return UNICODE.supported ? "\u2139" : "i";
|
|
64
|
+
},
|
|
65
|
+
get WARNING_RAW() {
|
|
66
|
+
return UNICODE.supported ? "\u26A0" : "\u203C";
|
|
67
|
+
},
|
|
68
|
+
get CIRCLE_CROSS_RAW() {
|
|
69
|
+
return UNICODE.supported ? "\u24E7" : "(\xD7)";
|
|
70
|
+
},
|
|
71
|
+
get COMMAND() {
|
|
72
|
+
return ANSI.color(UNICODE.COMMAND_RAW, ANSI.GREY); // ANSI_MAGENTA)
|
|
73
|
+
},
|
|
74
|
+
get OK() {
|
|
75
|
+
return ANSI.color(UNICODE.OK_RAW, ANSI.GREEN);
|
|
76
|
+
},
|
|
77
|
+
get FAILURE() {
|
|
78
|
+
return ANSI.color(UNICODE.FAILURE_RAW, ANSI.RED);
|
|
79
|
+
},
|
|
80
|
+
get DEBUG() {
|
|
81
|
+
return ANSI.color(UNICODE.DEBUG_RAW, ANSI.GREY);
|
|
82
|
+
},
|
|
83
|
+
get INFO() {
|
|
84
|
+
return ANSI.color(UNICODE.INFO_RAW, ANSI.BLUE);
|
|
85
|
+
},
|
|
86
|
+
get WARNING() {
|
|
87
|
+
return ANSI.color(UNICODE.WARNING_RAW, ANSI.YELLOW);
|
|
88
|
+
},
|
|
89
|
+
get CIRCLE_CROSS() {
|
|
90
|
+
return ANSI.color(UNICODE.CIRCLE_CROSS_RAW, ANSI.RED);
|
|
91
|
+
},
|
|
92
|
+
get ELLIPSIS() {
|
|
93
|
+
return UNICODE.supported ? "\u2026" : "...";
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
return UNICODE;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const UNICODE = createUnicode({
|
|
100
|
+
supported: true,
|
|
101
|
+
ANSI
|
|
102
|
+
});
|
|
103
|
+
|
|
10
104
|
const inspectBoolean = value => value.toString();
|
|
11
105
|
|
|
12
106
|
const inspectNull = () => "null";
|
|
@@ -914,7 +1008,7 @@ const humanizeFileSize = (numberOfBytes, {
|
|
|
914
1008
|
short
|
|
915
1009
|
});
|
|
916
1010
|
};
|
|
917
|
-
const
|
|
1011
|
+
const humanizeMemory = (metricValue, {
|
|
918
1012
|
decimals,
|
|
919
1013
|
short
|
|
920
1014
|
} = {}) => {
|
|
@@ -1154,4 +1248,4 @@ const fillLeft = (value, biggestValue, char = " ") => {
|
|
|
1154
1248
|
return padded;
|
|
1155
1249
|
};
|
|
1156
1250
|
|
|
1157
|
-
export {
|
|
1251
|
+
export { ANSI, UNICODE, createDetailedMessage, distributePercentages, generateContentFrame, humanize, humanizeDuration, humanizeEllapsedTime, humanizeFileSize, humanizeMemory, humanizeMethodSymbol };
|