• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yiming-liao / logry / 15760909787

19 Jun 2025 03:01PM UTC coverage: 96.654% (-1.8%) from 98.439%
15760909787

push

github

yiming-liao
refactor: restructure formatter, handler and core log flow

503 of 546 branches covered (92.12%)

Branch coverage included in aggregate %.

543 of 549 new or added lines in 39 files covered. (98.91%)

80 existing lines in 11 files now uncovered.

4668 of 4804 relevant lines covered (97.17%)

3.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/src/modules/formatters/utils/format-object.ts
1
import type { StringifyFormat } from "@/modules/formatters";
1✔
2
import { internalLog } from "@/internal";
1✔
3

1✔
4
/**
1✔
5
 * Converts an object into different string formats.
1✔
6
 *
1✔
7
 * @param input - The object to stringify.
1✔
8
 * @param style - Stringify style: raw, json, compact, or pretty.
1✔
9
 * @returns Formatted string or raw object.
1✔
10
 */
1✔
11
export const formatObject = (
1✔
12
  input: Record<string, unknown>,
12✔
13
  style: StringifyFormat = "pretty",
12✔
14
  indent: number = 0,
12✔
15
): Record<string, unknown> | string => {
12✔
16
  try {
12✔
17
    switch (style) {
12✔
18
      case "raw":
12✔
19
        return input;
5✔
20

12✔
21
      case "json":
12✔
22
        return JSON.stringify(input);
3✔
23

12✔
24
      case "pretty": {
12✔
25
        const indentSpaces = " ".repeat(indent);
1✔
26
        return `\n${JSON.stringify(input, null, 2)
1✔
27
          .split("\n")
1✔
28
          .map((line) => indentSpaces + line)
1✔
29
          .join("\n")}`;
1✔
30
      }
1✔
31

12✔
32
      case "compact":
12✔
33
        if (typeof input !== "object" || input === null) return "";
2✔
34

1✔
35
        return Object.entries(input)
1✔
36
          .map(([key, val]) => {
1✔
37
            let value;
2✔
38
            if (typeof val === "object" && val !== null) {
2!
NEW
39
              value = JSON.stringify(val);
×
40
            } else if (typeof val === "string" && /\s/.test(val)) {
2!
NEW
41
              value = `"${val}"`;
×
42
            } else {
2✔
43
              value = String(val);
2✔
44
            }
2✔
45
            return `${key}=${value}`;
2✔
46
          })
1✔
47
          .join(" ");
1✔
48

12✔
49
      default:
12✔
50
        internalLog({
1✔
51
          type: "warn",
1✔
52
          message: `Unknown style "${style}", using "json" as fallback.`,
1✔
53
        });
1✔
54
        return JSON.stringify(input); // Fallback to single-line JSON
12✔
55
    }
12✔
56
  } catch (error) {
12✔
57
    internalLog({
2✔
58
      type: "error",
2✔
59
      message: `Failed to stringify object. ${error instanceof Error ? error.message : String(error)}`,
2✔
60
    });
2✔
61
    return "";
2✔
62
  }
2✔
63
};
12✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc