All files / internal / format.ts

100.00% Branches 19/19
100.00% Lines 43/43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1336
 
x1730
 
x1730
x1730
 
x1730
x1730
x2109
x2109
x2109
x2109
x2109
 
x2109
x2109
x1730
x1730
x1730
 
x1336
x1336
x1351
x1351
 
x1351
x1351
x1351
x1351
x1351
x1351
x5412
x1351
x1362
x1362
x1336
x1336
x1336
x1336
 
 
x1351
x1351
x1370
x1370
x1370
x1370
x1370
 
x1352
x1351





















































































// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.

/** An inspect function conforming to the shape of `Deno.inspect` and `node:util`'s `inspect` */
export type InspectFn = (
  v: unknown,
  options: {
    depth: number;
    sorted: boolean;
    trailingComma: boolean;
    compact: boolean;
    iterableLimit: number;
    getters: boolean;
    strAbbreviateSize: number;
  },
) => string;

/**
 * Converts the input into a string. Objects, Sets and Maps are sorted so as to
 * make tests less flaky.
 *
 * @param v Value to be formatted
 *
 * @returns The formatted string
 *
 * @example Usage
 * ```ts
 * import { format } from "@std/internal/format";
 * import { assertEquals } from "@std/assert";
 *
 * assertEquals(format({ a: 1, b: 2 }), "{\n  a: 1,\n  b: 2,\n}");
 * assertEquals(format(new Set([1, 2])), "Set(2) {\n  1,\n  2,\n}");
 * assertEquals(format(new Map([[1, 2]])), "Map(1) {\n  1 => 2,\n}");
 * ```
 */
export function format(v: unknown): string {
  // deno-lint-ignore no-explicit-any
  const { Deno, process } = globalThis as any;

  const inspect: InspectFn | undefined = Deno?.inspect ??
    process?.getBuiltinModule?.("node:util")?.inspect;

  return typeof inspect === "function"
    ? inspect(v, {
      depth: Infinity,
      sorted: true,
      trailingComma: true,
      compact: false,
      iterableLimit: Infinity,
      // getters should be true in assertEquals.
      getters: true,
      strAbbreviateSize: Infinity,
    })
    : basicInspect(v);
}

const formatters: ((v: unknown) => string | undefined)[] = [
  (v) => {
    if (typeof v === "undefined") return "undefined";
    if (typeof v === "bigint") return `${v}n`;

    if (
      typeof v === "string" ||
      typeof v === "number" ||
      typeof v === "boolean" ||
      v === null ||
      Array.isArray(v) ||
      [null, Object.prototype].includes(Object.getPrototypeOf(v))
    ) {
      return JSON.stringify(v, null, 2);
    }
  },
  (v) => String(v),
  (v) => Object.prototype.toString.call(v),
];

// for environments lacking both `Deno.inspect` and `process.inspect`
function basicInspect(v: unknown): string {
  for (const fmt of formatters) {
    try {
      const result = fmt(v);
      if (typeof result === "string") return result;
    } catch { /* try the next one */ }
  }

  return "[[Unable to format value]]";
}