@jsenv/humanize 1.5.1 → 1.6.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.
@@ -1447,6 +1447,58 @@ const escapeHtml = (string) => {
1447
1447
  .replace(/'/g, "'");
1448
1448
  };
1449
1449
 
1450
+ const formatError = (error) => {
1451
+ let text = "";
1452
+ text += error.stack;
1453
+ const { cause } = error;
1454
+ if (cause) {
1455
+ const formatCause = (cause, depth) => {
1456
+ let causeText = prefixFirstAndIndentRemainingLines({
1457
+ prefix: " [cause]:",
1458
+ indentation: " ".repeat(depth + 1),
1459
+ text: cause.stack,
1460
+ });
1461
+ const nestedCause = cause.cause;
1462
+ if (nestedCause) {
1463
+ const nestedCauseText = formatCause(nestedCause, depth + 1);
1464
+ causeText += `\n${nestedCauseText}`;
1465
+ }
1466
+ return causeText;
1467
+ };
1468
+ const causeText = formatCause(cause, 0);
1469
+ text += `\n${causeText}`;
1470
+ }
1471
+ return text;
1472
+ };
1473
+
1474
+ const prefixFirstAndIndentRemainingLines = ({
1475
+ prefix,
1476
+ indentation,
1477
+ text,
1478
+ trimLines,
1479
+ trimLastLine,
1480
+ }) => {
1481
+ const lines = text.split(/\r?\n/);
1482
+ const firstLine = lines.shift();
1483
+ if (indentation === undefined) {
1484
+ {
1485
+ indentation = " "; // prefix + space
1486
+ }
1487
+ }
1488
+ let result = `${prefix} ${firstLine}` ;
1489
+ let i = 0;
1490
+ while (i < lines.length) {
1491
+ const line = trimLines ? lines[i].trim() : lines[i];
1492
+ i++;
1493
+ result += line.length
1494
+ ? `\n${indentation}${line}`
1495
+ : trimLastLine && i === lines.length
1496
+ ? ""
1497
+ : `\n`;
1498
+ }
1499
+ return result;
1500
+ };
1501
+
1450
1502
  const renderBigSection = (params) => {
1451
1503
  return renderSection({
1452
1504
  width: 45,
@@ -2004,4 +2056,4 @@ const createTaskLog = (
2004
2056
  };
2005
2057
  };
2006
2058
 
2007
- export { ANSI, UNICODE, createCallOrderer, createDetailedMessage, createDynamicLog, createLogger, createTaskLog, distributePercentages, errorToHTML, errorToMarkdown, generateContentFrame, humanize, humanizeDuration, humanizeEllapsedTime, humanizeFileSize, humanizeMemory, humanizeMethodSymbol, renderBigSection, renderDetails, renderNamedSections, renderSection, startSpinner };
2059
+ export { ANSI, UNICODE, createCallOrderer, createDetailedMessage, createDynamicLog, createLogger, createTaskLog, distributePercentages, errorToHTML, errorToMarkdown, formatError, generateContentFrame, humanize, humanizeDuration, humanizeEllapsedTime, humanizeFileSize, humanizeMemory, humanizeMethodSymbol, renderBigSection, renderDetails, renderNamedSections, renderSection, startSpinner };
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@jsenv/humanize",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "description": "Generate messages meant to read by humans",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/jsenv/core",
9
- "directory": "packages/independent/tooling/humanize"
9
+ "directory": "packages/tooling/humanize"
10
10
  },
11
11
  "publishConfig": {
12
12
  "access": "public"
@@ -18,11 +18,11 @@
18
18
  "exports": {
19
19
  ".": {
20
20
  "browser": {
21
- "development": "./src/main_browser.js",
21
+ "dev:jsenv": "./src/main_browser.js",
22
22
  "default": "./dist/browser/jsenv_humanize_browser.js"
23
23
  },
24
24
  "node": {
25
- "development": "./src/main_node.js",
25
+ "dev:jsenv": "./src/main_node.js",
26
26
  "default": "./dist/node/jsenv_humanize_node.js"
27
27
  }
28
28
  },
@@ -45,6 +45,10 @@
45
45
  },
46
46
  "devDependencies": {
47
47
  "is-unicode-supported": "2.1.0",
48
- "supports-color": "10.0.0"
48
+ "supports-color": "10.0.0",
49
+ "@jsenv/core": "../..",
50
+ "@jsenv/snapshot": "../snapshot",
51
+ "@jsenv/assert": "../assert",
52
+ "@jsenv/terminal-text-size": "../terminal-text-size"
49
53
  }
50
54
  }
@@ -1,8 +1,7 @@
1
+ import { generateContentFrame } from "@jsenv/humanize";
1
2
  import { takeFileSnapshot } from "@jsenv/snapshot";
2
3
  import { writeFileSync } from "node:fs";
3
4
 
4
- import { generateContentFrame } from "@jsenv/humanize";
5
-
6
5
  let number = 1;
7
6
  const test = (name, params) => {
8
7
  const snapshotFileUrl = new URL(
@@ -0,0 +1,53 @@
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
+ };
package/src/main_node.js CHANGED
@@ -14,6 +14,7 @@ export { generateContentFrame } from "./content_frame/content_frame.js";
14
14
  export { createCallOrderer } from "./log/call_orderer.js";
15
15
  export { errorToMarkdown } from "./error/error_to_markdown.js";
16
16
  export { errorToHTML } from "./error/error_to_html.js";
17
+ export { formatError } from "./error/format_error.js";
17
18
 
18
19
  // node only
19
20
  export { renderSection, renderBigSection } from "./log/section.js";