@jsenv/humanize 1.6.0 → 1.7.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 (91) hide show
  1. package/dist/browser/jsenv_humanize_browser.js +539 -482
  2. package/dist/browser/jsenv_humanize_browser.js.map +76 -0
  3. package/dist/node/jsenv_humanize_node.js +36 -37
  4. package/dist/node/jsenv_humanize_node.js.map +97 -0
  5. package/dist/node/jsenv_humanize_node_modules.js +23 -15
  6. package/dist/node/jsenv_humanize_node_modules.js.map +21 -0
  7. package/package.json +18 -19
  8. package/src/_tests/byte/file_size.test.js +0 -18
  9. package/src/_tests/byte/memory.test.js +0 -11
  10. package/src/_tests/content_frame/content_frame.test.js +0 -155
  11. package/src/_tests/content_frame/snapshots/10_line_and_column_are_zero.txt +0 -2
  12. package/src/_tests/content_frame/snapshots/11_line_is_last.txt +0 -3
  13. package/src/_tests/content_frame/snapshots/1_basic.txt +0 -4
  14. package/src/_tests/content_frame/snapshots/2_basic_2.txt +0 -6
  15. package/src/_tests/content_frame/snapshots/3_empty_last_line.txt +0 -7
  16. package/src/_tests/content_frame/snapshots/4_no_column.txt +0 -3
  17. package/src/_tests/content_frame/snapshots/5_line_1_and_2_digits.txt +0 -3
  18. package/src/_tests/content_frame/snapshots/6_line_too_long_column_undefined.txt +0 -3
  19. package/src/_tests/content_frame/snapshots/7_line_too_long_column_near_start.txt +0 -4
  20. package/src/_tests/content_frame/snapshots/8_line_too_long_column_near_middle.txt +0 -4
  21. package/src/_tests/content_frame/snapshots/9_line_too_long_column_near_end.txt +0 -4
  22. package/src/_tests/js_value/_inspect.test.html +0 -22
  23. package/src/_tests/js_value/array.test.js +0 -72
  24. package/src/_tests/js_value/biginit.test.js +0 -35
  25. package/src/_tests/js_value/boolean.test.js +0 -45
  26. package/src/_tests/js_value/date.test.js +0 -15
  27. package/src/_tests/js_value/error.test.js +0 -33
  28. package/src/_tests/js_value/function.test.js +0 -51
  29. package/src/_tests/js_value/instance.test.js +0 -12
  30. package/src/_tests/js_value/null.test.js +0 -6
  31. package/src/_tests/js_value/number.test.js +0 -182
  32. package/src/_tests/js_value/object.test.js +0 -173
  33. package/src/_tests/js_value/regexp.test.js +0 -15
  34. package/src/_tests/js_value/string.test.js +0 -106
  35. package/src/_tests/js_value/symbol.test.js +0 -20
  36. package/src/_tests/js_value/undefined.test.js +0 -6
  37. package/src/_tests/log/log_long.test_manual.mjs +0 -39
  38. package/src/_tests/log/log_modified.test_manual.mjs +0 -7
  39. package/src/_tests/log/logger.test.mjs +0 -90
  40. package/src/_tests/log/spinner.duration.test_manual.mjs +0 -25
  41. package/src/_tests/log/spinner.test.mjs +0 -33
  42. package/src/_tests/log/spinner.test_manual.mjs +0 -15
  43. package/src/_tests/percentage/percentage_distribution.test.mjs +0 -89
  44. package/src/_tests/time/decimals.test.js +0 -119
  45. package/src/_tests/time/duration.test.js +0 -20
  46. package/src/_tests/time/ellapsed.test.js +0 -10
  47. package/src/ansi/ansi_browser.js +0 -3
  48. package/src/ansi/ansi_node.js +0 -13
  49. package/src/ansi/ansi_runtime_agnostic.js +0 -107
  50. package/src/byte/byte.js +0 -44
  51. package/src/content_frame/content_frame.js +0 -157
  52. package/src/error/error_to_html.js +0 -22
  53. package/src/error/error_to_markdown.js +0 -14
  54. package/src/error/format_error.js +0 -53
  55. package/src/js_value/array.js +0 -58
  56. package/src/js_value/bigint.js +0 -3
  57. package/src/js_value/bigint_object.js +0 -5
  58. package/src/js_value/boolean.js +0 -1
  59. package/src/js_value/boolean_object.js +0 -12
  60. package/src/js_value/constructor.js +0 -10
  61. package/src/js_value/date.js +0 -8
  62. package/src/js_value/error.js +0 -34
  63. package/src/js_value/function.js +0 -20
  64. package/src/js_value/humanize.js +0 -142
  65. package/src/js_value/null.js +0 -1
  66. package/src/js_value/number.js +0 -101
  67. package/src/js_value/number_object.js +0 -9
  68. package/src/js_value/object.js +0 -78
  69. package/src/js_value/regexp.js +0 -1
  70. package/src/js_value/string.js +0 -141
  71. package/src/js_value/string_object.js +0 -10
  72. package/src/js_value/symbol.js +0 -24
  73. package/src/js_value/undefined.js +0 -1
  74. package/src/log/LOG_LEVELS.js +0 -9
  75. package/src/log/call_orderer.js +0 -39
  76. package/src/log/detailed_message.js +0 -29
  77. package/src/log/details.js +0 -26
  78. package/src/log/dynamic_log.js +0 -186
  79. package/src/log/logger.js +0 -85
  80. package/src/log/section.js +0 -50
  81. package/src/log/spinner.js +0 -72
  82. package/src/log/task_log.js +0 -49
  83. package/src/main_browser.js +0 -13
  84. package/src/main_node.js +0 -25
  85. package/src/percentage/distribute_percentages.js +0 -53
  86. package/src/time/time.js +0 -151
  87. package/src/unicode/unicode_browser.js +0 -7
  88. package/src/unicode/unicode_node.js +0 -8
  89. package/src/unicode/unicode_runtime_agnostic.js +0 -56
  90. package/src/utils/decimals.js +0 -95
  91. package/src/utils/indentation.js +0 -38
@@ -1,107 +0,0 @@
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
- const RED = "red";
6
- const GREEN = "green";
7
- const YELLOW = "yellow";
8
- const BLUE = "blue";
9
- const MAGENTA = "magenta";
10
- const CYAN = "cyan";
11
- const GREY = "grey";
12
- const WHITE = "white";
13
- const BLACK = "black";
14
-
15
- const TEXT_COLOR_ANSI_CODES = {
16
- [RED]: "\x1b[31m",
17
- [GREEN]: "\x1b[32m",
18
- [YELLOW]: "\x1b[33m",
19
- [BLUE]: "\x1b[34m",
20
- [MAGENTA]: "\x1b[35m",
21
- [CYAN]: "\x1b[36m",
22
- [GREY]: "\x1b[90m",
23
- [WHITE]: "\x1b[37m",
24
- [BLACK]: "\x1b[30m",
25
- };
26
- const BACKGROUND_COLOR_ANSI_CODES = {
27
- [RED]: "\x1b[41m",
28
- [GREEN]: "\x1b[42m",
29
- [YELLOW]: "\x1b[43m",
30
- [BLUE]: "\x1b[44m",
31
- [MAGENTA]: "\x1b[45m",
32
- [CYAN]: "\x1b[46m",
33
- [GREY]: "\x1b[100m",
34
- [WHITE]: "\x1b[47m",
35
- [BLACK]: "\x1b[40m",
36
- };
37
-
38
- export const createAnsi = ({ supported }) => {
39
- const ANSI = {
40
- supported,
41
-
42
- RED,
43
- GREEN,
44
- YELLOW,
45
- BLUE,
46
- MAGENTA,
47
- CYAN,
48
- GREY,
49
- WHITE,
50
- BLACK,
51
- color: (text, color) => {
52
- if (!ANSI.supported) {
53
- return text;
54
- }
55
- if (!color) {
56
- return text;
57
- }
58
- if (typeof text === "string" && text.trim() === "") {
59
- // cannot set color of blank chars
60
- return text;
61
- }
62
- const ansiEscapeCodeForTextColor = TEXT_COLOR_ANSI_CODES[color];
63
- if (!ansiEscapeCodeForTextColor) {
64
- return text;
65
- }
66
- return `${ansiEscapeCodeForTextColor}${text}${RESET}`;
67
- },
68
- backgroundColor: (text, color) => {
69
- if (!ANSI.supported) {
70
- return text;
71
- }
72
- if (!color) {
73
- return text;
74
- }
75
- if (typeof text === "string" && text.trim() === "") {
76
- // cannot set background color of blank chars
77
- return text;
78
- }
79
- const ansiEscapeCodeForBackgroundColor =
80
- BACKGROUND_COLOR_ANSI_CODES[color];
81
- if (!ansiEscapeCodeForBackgroundColor) {
82
- return text;
83
- }
84
- return `${ansiEscapeCodeForBackgroundColor}${text}${RESET}`;
85
- },
86
-
87
- BOLD: "\x1b[1m",
88
- UNDERLINE: "\x1b[4m",
89
- STRIKE: "\x1b[9m",
90
- effect: (text, effect) => {
91
- if (!ANSI.supported) {
92
- return text;
93
- }
94
- if (!effect) {
95
- return text;
96
- }
97
- // cannot add effect to empty string
98
- if (text === "") {
99
- return text;
100
- }
101
- const ansiEscapeCodeForEffect = effect;
102
- return `${ansiEscapeCodeForEffect}${text}${RESET}`;
103
- },
104
- };
105
-
106
- return ANSI;
107
- };
package/src/byte/byte.js DELETED
@@ -1,44 +0,0 @@
1
- import { setRoundedPrecision } from "../utils/decimals.js";
2
-
3
- export const humanizeFileSize = (numberOfBytes, { decimals, short } = {}) => {
4
- return inspectBytes(numberOfBytes, { decimals, short });
5
- };
6
-
7
- export const humanizeMemory = (metricValue, { decimals, short } = {}) => {
8
- return inspectBytes(metricValue, { decimals, fixedDecimals: true, short });
9
- };
10
-
11
- const inspectBytes = (
12
- number,
13
- { fixedDecimals = false, decimals, short } = {},
14
- ) => {
15
- if (number === 0) {
16
- return `0 B`;
17
- }
18
- const exponent = Math.min(
19
- Math.floor(Math.log10(number) / 3),
20
- BYTE_UNITS.length - 1,
21
- );
22
- const unitNumber = number / Math.pow(1000, exponent);
23
- const unitName = BYTE_UNITS[exponent];
24
- if (decimals === undefined) {
25
- if (unitNumber < 100) {
26
- decimals = 1;
27
- } else {
28
- decimals = 0;
29
- }
30
- }
31
- const unitNumberRounded = setRoundedPrecision(unitNumber, {
32
- decimals,
33
- decimalsWhenSmall: 1,
34
- });
35
- const value = fixedDecimals
36
- ? unitNumberRounded.toFixed(decimals)
37
- : unitNumberRounded;
38
- if (short) {
39
- return `${value}${unitName}`;
40
- }
41
- return `${value} ${unitName}`;
42
- };
43
-
44
- const BYTE_UNITS = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
@@ -1,157 +0,0 @@
1
- const formatDefault = (v) => v;
2
-
3
- export const generateContentFrame = ({
4
- content,
5
- line,
6
- column,
7
-
8
- linesAbove = 3,
9
- linesBelow = 0,
10
- lineMaxWidth = 120,
11
- lineNumbersOnTheLeft = true,
12
- lineMarker = true,
13
- columnMarker = true,
14
- format = formatDefault,
15
- } = {}) => {
16
- const lineStrings = content.split(/\r?\n/);
17
- if (line === 0) line = 1;
18
- if (column === undefined) {
19
- columnMarker = false;
20
- column = 1;
21
- }
22
- if (column === 0) column = 1;
23
-
24
- let lineStartIndex = line - 1 - linesAbove;
25
- if (lineStartIndex < 0) {
26
- lineStartIndex = 0;
27
- }
28
- let lineEndIndex = line - 1 + linesBelow;
29
- if (lineEndIndex > lineStrings.length - 1) {
30
- lineEndIndex = lineStrings.length - 1;
31
- }
32
- if (columnMarker) {
33
- // human reader deduce the line when there is a column marker
34
- lineMarker = false;
35
- }
36
- if (line - 1 === lineEndIndex) {
37
- lineMarker = false; // useless because last line
38
- }
39
- let lineIndex = lineStartIndex;
40
-
41
- let columnsBefore;
42
- let columnsAfter;
43
- if (column > lineMaxWidth) {
44
- columnsBefore = column - Math.ceil(lineMaxWidth / 2);
45
- columnsAfter = column + Math.floor(lineMaxWidth / 2);
46
- } else {
47
- columnsBefore = 0;
48
- columnsAfter = lineMaxWidth;
49
- }
50
- let columnMarkerIndex = column - 1 - columnsBefore;
51
-
52
- let source = "";
53
- while (lineIndex <= lineEndIndex) {
54
- const lineString = lineStrings[lineIndex];
55
- const lineNumber = lineIndex + 1;
56
- const isLastLine = lineIndex === lineEndIndex;
57
- const isMainLine = lineNumber === line;
58
- lineIndex++;
59
-
60
- write_aside: {
61
- if (lineMarker) {
62
- if (isMainLine) {
63
- source += `${format(">", "marker_line")} `;
64
- } else {
65
- source += " ";
66
- }
67
- }
68
- if (lineNumbersOnTheLeft) {
69
- // fill with spaces to ensure if line moves from 7,8,9 to 10 the display is still great
70
- const asideSource = `${fillLeft(lineNumber, lineEndIndex + 1)} |`;
71
- source += `${format(asideSource, "line_number_aside")} `;
72
- }
73
- }
74
- write_line: {
75
- source += truncateLine(lineString, {
76
- start: columnsBefore,
77
- end: columnsAfter,
78
- prefix: "…",
79
- suffix: "…",
80
- format,
81
- });
82
- }
83
- write_column_marker: {
84
- if (columnMarker && isMainLine) {
85
- source += `\n`;
86
- if (lineMarker) {
87
- source += " ";
88
- }
89
- if (lineNumbersOnTheLeft) {
90
- const asideSpaces = `${fillLeft(lineNumber, lineEndIndex + 1)} | `
91
- .length;
92
- source += " ".repeat(asideSpaces);
93
- }
94
- source += " ".repeat(columnMarkerIndex);
95
- source += format("^", "marker_column");
96
- }
97
- }
98
- if (!isLastLine) {
99
- source += "\n";
100
- }
101
- }
102
- return source;
103
- };
104
-
105
- const truncateLine = (line, { start, end, prefix, suffix, format }) => {
106
- const lastIndex = line.length;
107
-
108
- if (line.length === 0) {
109
- // don't show any ellipsis if the line is empty
110
- // because it's not truncated in that case
111
- return "";
112
- }
113
-
114
- const startTruncated = start > 0;
115
- const endTruncated = lastIndex > end;
116
-
117
- let from = startTruncated ? start + prefix.length : start;
118
- let to = endTruncated ? end - suffix.length : end;
119
- if (to > lastIndex) to = lastIndex;
120
-
121
- if (start >= lastIndex || from === to) {
122
- return "";
123
- }
124
- let result = "";
125
- while (from < to) {
126
- result += format(line[from], "char");
127
- from++;
128
- }
129
- if (result.length === 0) {
130
- return "";
131
- }
132
- if (startTruncated && endTruncated) {
133
- return `${format(prefix, "marker_overflow_left")}${result}${format(
134
- suffix,
135
- "marker_overflow_right",
136
- )}`;
137
- }
138
- if (startTruncated) {
139
- return `${format(prefix, "marker_overflow_left")}${result}`;
140
- }
141
- if (endTruncated) {
142
- return `${result}${format(suffix, "marker_overflow_right")}`;
143
- }
144
- return result;
145
- };
146
-
147
- const fillLeft = (value, biggestValue, char = " ") => {
148
- const width = String(value).length;
149
- const biggestWidth = String(biggestValue).length;
150
- let missingWidth = biggestWidth - width;
151
- let padded = "";
152
- while (missingWidth--) {
153
- padded += char;
154
- }
155
- padded += value;
156
- return padded;
157
- };
@@ -1,22 +0,0 @@
1
- export const errorToHTML = (error) => {
2
- const errorIsAPrimitive =
3
- error === null ||
4
- (typeof error !== "object" && typeof error !== "function");
5
-
6
- if (errorIsAPrimitive) {
7
- if (typeof error === "string") {
8
- return `<pre>${escapeHtml(error)}</pre>`;
9
- }
10
- return `<pre>${JSON.stringify(error, null, " ")}</pre>`;
11
- }
12
- return `<pre>${escapeHtml(error.stack)}</pre>`;
13
- };
14
-
15
- const escapeHtml = (string) => {
16
- return string
17
- .replace(/&/g, "&amp;")
18
- .replace(/</g, "&lt;")
19
- .replace(/>/g, "&gt;")
20
- .replace(/"/g, "&quot;")
21
- .replace(/'/g, "&#039;");
22
- };
@@ -1,14 +0,0 @@
1
- export const errorToMarkdown = (error) => {
2
- const errorIsAPrimitive =
3
- error === null ||
4
- (typeof error !== "object" && typeof error !== "function");
5
-
6
- if (errorIsAPrimitive) {
7
- return `\`\`\`js
8
- ${error}
9
- \`\`\``;
10
- }
11
- return `\`\`\`
12
- ${error.stack}
13
- \`\`\``;
14
- };
@@ -1,53 +0,0 @@
1
- export const formatError = (error) => {
2
- let text = "";
3
- text += error.stack;
4
- const { cause } = error;
5
- if (cause) {
6
- const formatCause = (cause, depth) => {
7
- let causeText = prefixFirstAndIndentRemainingLines({
8
- prefix: " [cause]:",
9
- indentation: " ".repeat(depth + 1),
10
- text: cause.stack,
11
- });
12
- const nestedCause = cause.cause;
13
- if (nestedCause) {
14
- const nestedCauseText = formatCause(nestedCause, depth + 1);
15
- causeText += `\n${nestedCauseText}`;
16
- }
17
- return causeText;
18
- };
19
- const causeText = formatCause(cause, 0);
20
- text += `\n${causeText}`;
21
- }
22
- return text;
23
- };
24
-
25
- const prefixFirstAndIndentRemainingLines = ({
26
- prefix,
27
- indentation,
28
- text,
29
- trimLines,
30
- trimLastLine,
31
- }) => {
32
- const lines = text.split(/\r?\n/);
33
- const firstLine = lines.shift();
34
- if (indentation === undefined) {
35
- if (prefix) {
36
- indentation = " "; // prefix + space
37
- } else {
38
- indentation = "";
39
- }
40
- }
41
- let result = prefix ? `${prefix} ${firstLine}` : firstLine;
42
- let i = 0;
43
- while (i < lines.length) {
44
- const line = trimLines ? lines[i].trim() : lines[i];
45
- i++;
46
- result += line.length
47
- ? `\n${indentation}${line}`
48
- : trimLastLine && i === lines.length
49
- ? ""
50
- : `\n`;
51
- }
52
- return result;
53
- };
@@ -1,58 +0,0 @@
1
- import {
2
- preNewLineAndIndentation,
3
- wrapNewLineAndIndentation,
4
- } from "../utils/indentation.js";
5
- import { inspectConstructor } from "./constructor.js";
6
-
7
- export const inspectArray = (
8
- value,
9
- {
10
- seen = [],
11
- nestedHumanize,
12
- depth,
13
- indentUsingTab,
14
- indentSize,
15
- parenthesis,
16
- useNew,
17
- },
18
- ) => {
19
- if (seen.indexOf(value) > -1) {
20
- return "Symbol.for('circular')";
21
- }
22
- seen.push(value);
23
-
24
- let valuesSource = "";
25
- let i = 0;
26
- const j = value.length;
27
-
28
- while (i < j) {
29
- const valueSource = value.hasOwnProperty(i)
30
- ? nestedHumanize(value[i], { seen })
31
- : "";
32
- if (i === 0) {
33
- valuesSource += valueSource;
34
- } else {
35
- valuesSource += `,${preNewLineAndIndentation(valueSource, {
36
- depth,
37
- indentUsingTab,
38
- indentSize,
39
- })}`;
40
- }
41
- i++;
42
- }
43
-
44
- let arraySource;
45
- if (valuesSource.length) {
46
- arraySource = wrapNewLineAndIndentation(valuesSource, {
47
- depth,
48
- indentUsingTab,
49
- indentSize,
50
- });
51
- } else {
52
- arraySource = "";
53
- }
54
-
55
- arraySource = `[${arraySource}]`;
56
-
57
- return inspectConstructor(arraySource, { parenthesis, useNew });
58
- };
@@ -1,3 +0,0 @@
1
- export const inspectBigInt = (value) => {
2
- return `${value}n`;
3
- };
@@ -1,5 +0,0 @@
1
- export const inspectBigIntObject = (value, { nestedHumanize }) => {
2
- const bigIntSource = nestedHumanize(value.valueOf());
3
-
4
- return `BigInt(${bigIntSource})`;
5
- };
@@ -1 +0,0 @@
1
- export const inspectBoolean = (value) => value.toString();
@@ -1,12 +0,0 @@
1
- import { inspectConstructor } from "./constructor.js";
2
-
3
- export const inspectBooleanObject = (
4
- value,
5
- { nestedHumanize, useNew, parenthesis },
6
- ) => {
7
- const booleanSource = nestedHumanize(value.valueOf());
8
- return inspectConstructor(`Boolean(${booleanSource})`, {
9
- useNew,
10
- parenthesis,
11
- });
12
- };
@@ -1,10 +0,0 @@
1
- export const inspectConstructor = (value, { parenthesis, useNew }) => {
2
- let formattedString = value;
3
- if (parenthesis) {
4
- formattedString = `(${value})`;
5
- }
6
- if (useNew) {
7
- formattedString = `new ${formattedString}`;
8
- }
9
- return formattedString;
10
- };
@@ -1,8 +0,0 @@
1
- import { inspectConstructor } from "./constructor.js";
2
-
3
- export const inspectDate = (value, { nestedHumanize, useNew, parenthesis }) => {
4
- const dateSource = nestedHumanize(value.valueOf(), {
5
- numericSeparator: false,
6
- });
7
- return inspectConstructor(`Date(${dateSource})`, { useNew, parenthesis });
8
- };
@@ -1,34 +0,0 @@
1
- import { inspectConstructor } from "./constructor.js";
2
-
3
- export const inspectError = (
4
- error,
5
- { nestedHumanize, useNew, parenthesis },
6
- ) => {
7
- const messageSource = nestedHumanize(error.message);
8
-
9
- const errorSource = inspectConstructor(
10
- `${errorToConstructorName(error)}(${messageSource})`,
11
- {
12
- useNew,
13
- parenthesis,
14
- },
15
- );
16
- return errorSource;
17
- };
18
-
19
- const errorToConstructorName = ({ name }) => {
20
- if (derivedErrorNameArray.includes(name)) {
21
- return name;
22
- }
23
- return "Error";
24
- };
25
-
26
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types
27
- const derivedErrorNameArray = [
28
- "EvalError",
29
- "RangeError",
30
- "ReferenceError",
31
- "SyntaxError",
32
- "TypeError",
33
- "URIError",
34
- ];
@@ -1,20 +0,0 @@
1
- export const inspectFunction = (
2
- value,
3
- { showFunctionBody, parenthesis, depth },
4
- ) => {
5
- let functionSource;
6
- if (showFunctionBody) {
7
- functionSource = value.toString();
8
- } else {
9
- const isArrowFunction = value.prototype === undefined;
10
- const head = isArrowFunction
11
- ? "() =>"
12
- : `function ${depth === 0 ? value.name : ""}()`;
13
- functionSource = `${head} {/* hidden */}`;
14
- }
15
-
16
- if (parenthesis) {
17
- return `(${functionSource})`;
18
- }
19
- return functionSource;
20
- };