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

yiming-liao / logry / 19846724186

02 Dec 2025 04:06AM UTC coverage: 98.515% (+2.8%) from 95.764%
19846724186

push

github

yiming-liao
v2.0.2

506 of 530 branches covered (95.47%)

Branch coverage included in aggregate %.

3939 of 3982 relevant lines covered (98.92%)

5.58 hits per line

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

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

1✔
3
export type StringifyFormat = "raw" | "json" | "compact" | "pretty";
1✔
4

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

13✔
23
      case "json": {
13✔
24
        return JSON.stringify(input);
5✔
25
      }
5✔
26

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

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

13✔
54
      default: {
13✔
55
        internalLog({
1✔
56
          type: "warn",
1✔
57
          message: `Unknown style "${style}", using "json" as fallback.`,
1✔
58
        });
1✔
59
        return JSON.stringify(input);
1✔
60
      } // Fallback to single-line JSON
13✔
61
    }
13✔
62
  } catch (error) {
13✔
63
    internalLog({
2✔
64
      type: "error",
2✔
65
      message: `Failed to stringify object. ${error instanceof Error ? error.message : String(error)}`,
2✔
66
    });
2✔
67
    return "";
2✔
68
  }
2✔
69
};
13✔
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