@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.
Files changed (44) hide show
  1. package/README.md +25 -10
  2. package/dist/browser/jsenv_humanize_browser.js +96 -2
  3. package/dist/node/jsenv_humanize_node.js +1470 -1531
  4. package/package.json +3 -3
  5. package/src/.jsenv/jsenv_tests_output.txt +27 -30
  6. package/src/_tests/byte/file_size.test.js +2 -2
  7. package/src/_tests/byte/memory.test.js +11 -0
  8. package/src/_tests/js_value/array.test.js +14 -14
  9. package/src/_tests/js_value/biginit.test.js +10 -10
  10. package/src/_tests/js_value/boolean.test.js +12 -12
  11. package/src/_tests/js_value/date.test.js +4 -4
  12. package/src/_tests/js_value/error.test.js +8 -8
  13. package/src/_tests/js_value/function.test.js +12 -12
  14. package/src/_tests/js_value/instance.test.js +2 -2
  15. package/src/_tests/js_value/null.test.js +2 -2
  16. package/src/_tests/js_value/number.test.js +54 -54
  17. package/src/_tests/js_value/object.test.js +34 -34
  18. package/src/_tests/js_value/regexp.test.js +4 -4
  19. package/src/_tests/js_value/string.test.js +32 -32
  20. package/src/_tests/js_value/symbol.test.js +6 -6
  21. package/src/_tests/js_value/undefined.test.js +2 -2
  22. package/src/_tests/log/logger.test.mjs +14 -14
  23. package/src/_tests/log/spinner.test.mjs +6 -6
  24. package/src/_tests/percentage/percentage_distribution.test.mjs +2 -2
  25. package/src/_tests/time/decimals.test.js +40 -40
  26. package/src/_tests/time/duration.test.js +2 -2
  27. package/src/_tests/time/ellapsed.test.js +2 -2
  28. package/src/ansi/ansi_browser.js +3 -0
  29. package/src/ansi/ansi_node.js +16 -0
  30. package/src/ansi/ansi_runtime_agnostic.js +33 -0
  31. package/src/byte/byte.js +1 -1
  32. package/src/js_value/humanize.js +1 -1
  33. package/src/js_value/string.js +1 -1
  34. package/src/log/dynamic_log.js +6 -4
  35. package/src/log/spinner.js +1 -1
  36. package/src/log/task_log.js +1 -1
  37. package/src/main_browser.js +3 -5
  38. package/src/main_node.js +9 -13
  39. package/src/unicode/unicode_browser.js +7 -0
  40. package/src/unicode/unicode_node.js +8 -0
  41. package/src/unicode/unicode_runtime_agnostic.js +53 -0
  42. package/src/_tests/byte/memory_usage.test.js +0 -11
  43. package/src/log/ansi.js +0 -37
  44. package/src/log/unicode.js +0 -52
@@ -2,9 +2,9 @@ import { assert } from "@jsenv/assert";
2
2
 
3
3
  import { humanizeEllapsedTime } from "@jsenv/humanize";
4
4
 
5
- const test = (durationInMs, expected) => {
5
+ const test = (durationInMs, expect) => {
6
6
  const actual = humanizeEllapsedTime(durationInMs);
7
- assert({ actual, expected });
7
+ assert({ actual, expect });
8
8
  };
9
9
 
10
10
  test(2200, "2 seconds");
@@ -0,0 +1,3 @@
1
+ import { createAnsi } from "./ansi_runtime_agnostic.js";
2
+
3
+ export const ANSI = createAnsi({ supported: true });
@@ -0,0 +1,16 @@
1
+ import { createSupportsColor } from "supports-color";
2
+ import { createAnsi } from "./ansi_runtime_agnostic.js";
3
+
4
+ const processSupportsBasicColor = createSupportsColor(process.stdout).hasBasic;
5
+
6
+ export const ANSI = createAnsi({
7
+ supported:
8
+ processSupportsBasicColor ||
9
+ // GitHub workflow does support ANSI but "supports-color" returns false
10
+ // because stream.isTTY returns false, see https://github.com/actions/runner/issues/241
11
+ (process.env.GITHUB_WORKFLOW &&
12
+ // Check on FORCE_COLOR is to ensure it is prio over GitHub workflow check
13
+ // in unit test we use process.env.FORCE_COLOR = 'false' to fake
14
+ // that colors are not supported. Let it have priority
15
+ process.env.FORCE_COLOR !== "false"),
16
+ });
@@ -0,0 +1,33 @@
1
+ // https://github.com/Marak/colors.js/blob/master/lib/styles.js
2
+ // https://stackoverflow.com/a/75985833/2634179
3
+ const RESET = "\x1b[0m";
4
+
5
+ export const createAnsi = ({ supported }) => {
6
+ const ANSI = {
7
+ supported,
8
+
9
+ RED: "\x1b[31m",
10
+ GREEN: "\x1b[32m",
11
+ YELLOW: "\x1b[33m",
12
+ BLUE: "\x1b[34m",
13
+ MAGENTA: "\x1b[35m",
14
+ CYAN: "\x1b[36m",
15
+ GREY: "\x1b[90m",
16
+ color: (text, ANSI_COLOR) => {
17
+ return ANSI.supported && ANSI_COLOR
18
+ ? `${ANSI_COLOR}${text}${RESET}`
19
+ : text;
20
+ },
21
+
22
+ BOLD: "\x1b[1m",
23
+ UNDERLINE: "\x1b[4m",
24
+ STRIKE: "\x1b[9m",
25
+ effect: (text, ANSI_EFFECT) => {
26
+ return ANSI.supported && ANSI_EFFECT
27
+ ? `${ANSI_EFFECT}${text}${RESET}`
28
+ : text;
29
+ },
30
+ };
31
+
32
+ return ANSI;
33
+ };
package/src/byte/byte.js CHANGED
@@ -4,7 +4,7 @@ export const humanizeFileSize = (numberOfBytes, { decimals, short } = {}) => {
4
4
  return inspectBytes(numberOfBytes, { decimals, short });
5
5
  };
6
6
 
7
- export const humanizeMemoryUsage = (metricValue, { decimals, short } = {}) => {
7
+ export const humanizeMemory = (metricValue, { decimals, short } = {}) => {
8
8
  return inspectBytes(metricValue, { decimals, fixedDecimals: true, short });
9
9
  };
10
10
 
@@ -65,7 +65,7 @@ export const humanize = (
65
65
 
66
66
  export const humanizeMethodSymbol = Symbol.for("inspect");
67
67
 
68
- export const humanizeValue = (value, options) => {
68
+ const humanizeValue = (value, options) => {
69
69
  const customHumanize = value && value[humanizeMethodSymbol];
70
70
  if (customHumanize) {
71
71
  return customHumanize(options);
@@ -67,7 +67,7 @@ export const determineQuote = (
67
67
  return quoteDefault;
68
68
  };
69
69
 
70
- export const inspectChar = (char, { quote, preserveLineBreaks }) => {
70
+ const inspectChar = (char, { quote, preserveLineBreaks }) => {
71
71
  const point = char.charCodeAt(0);
72
72
  if (preserveLineBreaks && (char === "\n" || char === "\r")) {
73
73
  return char;
@@ -88,20 +88,22 @@ export const createDynamicLog = ({
88
88
  clearAttemptResult = undefined;
89
89
  };
90
90
 
91
- const clearDuringFunctionCall = (callback) => {
91
+ const clearDuringFunctionCall = (
92
+ callback,
93
+ ouputAfterCallback = lastOutput,
94
+ ) => {
92
95
  // 1. Erase the current log
93
- // 2. Call callback (expected to write something on stdout)
96
+ // 2. Call callback (expect to write something on stdout)
94
97
  // 3. Restore the current log
95
98
  // During step 2. we expect a "write from outside" so we uninstall
96
99
  // the stream spy during function call
97
- const currentOutput = lastOutput;
98
100
  update("");
99
101
 
100
102
  writing = true;
101
103
  callback();
102
104
  writing = false;
103
105
 
104
- update(currentOutput);
106
+ update(ouputAfterCallback);
105
107
  };
106
108
 
107
109
  const writeFromOutsideEffect = (value) => {
@@ -1,4 +1,4 @@
1
- import { ANSI } from "./ansi.js";
1
+ import { ANSI } from "../ansi/ansi_node.js";
2
2
 
3
3
  export const startSpinner = ({
4
4
  dynamicLog,
@@ -1,5 +1,5 @@
1
1
  import { humanizeDuration } from "../time/time.js";
2
- import { UNICODE } from "./unicode.js";
2
+ import { UNICODE } from "../unicode/unicode_node.js";
3
3
  import { createDynamicLog } from "./dynamic_log.js";
4
4
  import { startSpinner } from "./spinner.js";
5
5
 
@@ -1,10 +1,8 @@
1
1
  export { createDetailedMessage } from "./log/detailed_message.js";
2
-
2
+ export { ANSI } from "./ansi/ansi_browser.js";
3
+ export { UNICODE } from "./unicode/unicode_browser.js";
3
4
  export { humanize, humanizeMethodSymbol } from "./js_value/humanize.js";
4
- export { determineQuote, inspectChar } from "./js_value/string.js";
5
-
6
5
  export { humanizeDuration, humanizeEllapsedTime } from "./time/time.js";
7
- export { humanizeFileSize, humanizeMemoryUsage } from "./byte/byte.js";
6
+ export { humanizeFileSize, humanizeMemory } from "./byte/byte.js";
8
7
  export { distributePercentages } from "./percentage/distribute_percentages.js";
9
-
10
8
  export { generateContentFrame } from "./content_frame/content_frame.js";
package/src/main_node.js CHANGED
@@ -1,18 +1,14 @@
1
- // node only
2
- export { createLogger } from "./log/logger.js";
3
- export { createDynamicLog } from "./log/dynamic_log.js";
4
- export { startSpinner } from "./log/spinner.js";
5
- export { createTaskLog } from "./log/task_log.js";
6
1
  export { createDetailedMessage } from "./log/detailed_message.js";
7
-
8
- export { ANSI } from "./log/ansi.js";
9
- export { UNICODE } from "./log/unicode.js";
10
-
2
+ export { ANSI } from "./ansi/ansi_node.js";
3
+ export { UNICODE } from "./unicode/unicode_node.js";
11
4
  export { humanize, humanizeMethodSymbol } from "./js_value/humanize.js";
12
- export { determineQuote, inspectChar } from "./js_value/string.js";
13
-
14
5
  export { humanizeDuration, humanizeEllapsedTime } from "./time/time.js";
15
- export { humanizeFileSize, humanizeMemoryUsage } from "./byte/byte.js";
6
+ export { humanizeFileSize, humanizeMemory } from "./byte/byte.js";
16
7
  export { distributePercentages } from "./percentage/distribute_percentages.js";
17
-
18
8
  export { generateContentFrame } from "./content_frame/content_frame.js";
9
+
10
+ // node only
11
+ export { createLogger } from "./log/logger.js";
12
+ export { createDynamicLog } from "./log/dynamic_log.js";
13
+ export { startSpinner } from "./log/spinner.js";
14
+ export { createTaskLog } from "./log/task_log.js";
@@ -0,0 +1,7 @@
1
+ import { createUnicode } from "./unicode_runtime_agnostic.js";
2
+ import { ANSI } from "../ansi/ansi_browser.js";
3
+
4
+ export const UNICODE = createUnicode({
5
+ supported: true,
6
+ ANSI,
7
+ });
@@ -0,0 +1,8 @@
1
+ import isUnicodeSupported from "is-unicode-supported";
2
+ import { createUnicode } from "./unicode_runtime_agnostic.js";
3
+ import { ANSI } from "../ansi/ansi_node.js";
4
+
5
+ export const UNICODE = createUnicode({
6
+ supported: isUnicodeSupported(),
7
+ ANSI,
8
+ });
@@ -0,0 +1,53 @@
1
+ // see also https://github.com/sindresorhus/figures
2
+
3
+ export const createUnicode = ({ supported, ANSI }) => {
4
+ const UNICODE = {
5
+ supported,
6
+ get COMMAND_RAW() {
7
+ return UNICODE.supported ? `❯` : `>`;
8
+ },
9
+ get OK_RAW() {
10
+ return UNICODE.supported ? `✔` : `√`;
11
+ },
12
+ get FAILURE_RAW() {
13
+ return UNICODE.supported ? `✖` : `×`;
14
+ },
15
+ get DEBUG_RAW() {
16
+ return UNICODE.supported ? `◆` : `♦`;
17
+ },
18
+ get INFO_RAW() {
19
+ return UNICODE.supported ? `ℹ` : `i`;
20
+ },
21
+ get WARNING_RAW() {
22
+ return UNICODE.supported ? `⚠` : `‼`;
23
+ },
24
+ get CIRCLE_CROSS_RAW() {
25
+ return UNICODE.supported ? `ⓧ` : `(×)`;
26
+ },
27
+ get COMMAND() {
28
+ return ANSI.color(UNICODE.COMMAND_RAW, ANSI.GREY); // ANSI_MAGENTA)
29
+ },
30
+ get OK() {
31
+ return ANSI.color(UNICODE.OK_RAW, ANSI.GREEN);
32
+ },
33
+ get FAILURE() {
34
+ return ANSI.color(UNICODE.FAILURE_RAW, ANSI.RED);
35
+ },
36
+ get DEBUG() {
37
+ return ANSI.color(UNICODE.DEBUG_RAW, ANSI.GREY);
38
+ },
39
+ get INFO() {
40
+ return ANSI.color(UNICODE.INFO_RAW, ANSI.BLUE);
41
+ },
42
+ get WARNING() {
43
+ return ANSI.color(UNICODE.WARNING_RAW, ANSI.YELLOW);
44
+ },
45
+ get CIRCLE_CROSS() {
46
+ return ANSI.color(UNICODE.CIRCLE_CROSS_RAW, ANSI.RED);
47
+ },
48
+ get ELLIPSIS() {
49
+ return UNICODE.supported ? `…` : `...`;
50
+ },
51
+ };
52
+ return UNICODE;
53
+ };
@@ -1,11 +0,0 @@
1
- import { assert } from "@jsenv/assert";
2
-
3
- import { humanizeMemoryUsage } from "@jsenv/humanize";
4
-
5
- const test = (memoryUsageInBytes, expected) => {
6
- const actual = humanizeMemoryUsage(memoryUsageInBytes);
7
- assert({ actual, expected });
8
- };
9
-
10
- test(1000, "1.0 kB");
11
- test(1100, "1.1 kB");
package/src/log/ansi.js DELETED
@@ -1,37 +0,0 @@
1
- import { createSupportsColor } from "supports-color";
2
-
3
- const processSupportsBasicColor = createSupportsColor(process.stdout).hasBasic;
4
- // https://github.com/Marak/colors.js/blob/master/lib/styles.js
5
- // https://stackoverflow.com/a/75985833/2634179
6
- const RESET = "\x1b[0m";
7
-
8
- export const ANSI = {
9
- supported: processSupportsBasicColor,
10
-
11
- RED: "\x1b[31m",
12
- GREEN: "\x1b[32m",
13
- YELLOW: "\x1b[33m",
14
- BLUE: "\x1b[34m",
15
- MAGENTA: "\x1b[35m",
16
- GREY: "\x1b[90m",
17
- color: (text, ANSI_COLOR) => {
18
- return ANSI.supported ? `${ANSI_COLOR}${text}${RESET}` : text;
19
- },
20
-
21
- BOLD: "\x1b[1m",
22
- effect: (text, ANSI_EFFECT) => {
23
- return ANSI.supported ? `${ANSI_EFFECT}${text}${RESET}` : text;
24
- },
25
- };
26
-
27
- // GitHub workflow does support ANSI but "supports-color" returns false
28
- // because stream.isTTY returns false, see https://github.com/actions/runner/issues/241
29
- if (
30
- process.env.GITHUB_WORKFLOW &&
31
- // Check on FORCE_COLOR is to ensure it is prio over GitHub workflow check
32
- // in unit test we use process.env.FORCE_COLOR = 'false' to fake
33
- // that colors are not supported. Let it have priority
34
- process.env.FORCE_COLOR !== "false"
35
- ) {
36
- ANSI.supported = true;
37
- }
@@ -1,52 +0,0 @@
1
- // see also https://github.com/sindresorhus/figures
2
-
3
- import isUnicodeSupported from "is-unicode-supported";
4
-
5
- import { ANSI } from "./ansi.js";
6
-
7
- export const UNICODE = {
8
- supported: isUnicodeSupported(),
9
-
10
- get COMMAND_RAW() {
11
- return UNICODE.supported ? `❯` : `>`;
12
- },
13
- get OK_RAW() {
14
- return UNICODE.supported ? `✔` : `√`;
15
- },
16
- get FAILURE_RAW() {
17
- return UNICODE.supported ? `✖` : `×`;
18
- },
19
- get DEBUG_RAW() {
20
- return UNICODE.supported ? `◆` : `♦`;
21
- },
22
- get INFO_RAW() {
23
- return UNICODE.supported ? `ℹ` : `i`;
24
- },
25
- get WARNING_RAW() {
26
- return UNICODE.supported ? `⚠` : `‼`;
27
- },
28
- get CIRCLE_CROSS_RAW() {
29
- return UNICODE.supported ? `ⓧ` : `(×)`;
30
- },
31
- get COMMAND() {
32
- return ANSI.color(UNICODE.COMMAND_RAW, ANSI.GREY); // ANSI_MAGENTA)
33
- },
34
- get OK() {
35
- return ANSI.color(UNICODE.OK_RAW, ANSI.GREEN);
36
- },
37
- get FAILURE() {
38
- return ANSI.color(UNICODE.FAILURE_RAW, ANSI.RED);
39
- },
40
- get DEBUG() {
41
- return ANSI.color(UNICODE.DEBUG_RAW, ANSI.GREY);
42
- },
43
- get INFO() {
44
- return ANSI.color(UNICODE.INFO_RAW, ANSI.BLUE);
45
- },
46
- get WARNING() {
47
- return ANSI.color(UNICODE.WARNING_RAW, ANSI.YELLOW);
48
- },
49
- get CIRCLE_CROSS() {
50
- return ANSI.color(UNICODE.CIRCLE_CROSS_RAW, ANSI.RED);
51
- },
52
- };