Skip to content

Commit 6a821c2

Browse files
committed
feat(internal): improve format support in node and bun
1 parent 096f0be commit 6a821c2

File tree

2 files changed

+83
-12
lines changed

2 files changed

+83
-12
lines changed

internal/format.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@
2121
*/
2222
export function format(v: unknown): string {
2323
// deno-lint-ignore no-explicit-any
24-
const { Deno } = globalThis as any;
24+
const { Deno, process } = globalThis as any;
25+
const options = {
26+
depth: Infinity,
27+
sorted: true,
28+
trailingComma: true,
29+
compact: false,
30+
iterableLimit: Infinity,
31+
// getters should be true in assertEquals.
32+
getters: true,
33+
strAbbreviateSize: Infinity,
34+
};
35+
2536
return typeof Deno?.inspect === "function"
26-
? Deno.inspect(v, {
27-
depth: Infinity,
28-
sorted: true,
29-
trailingComma: true,
30-
compact: false,
31-
iterableLimit: Infinity,
32-
// getters should be true in assertEquals.
33-
getters: true,
34-
strAbbreviateSize: Infinity,
35-
})
37+
? Deno.inspect(v, options)
38+
: typeof process?.getBuiltinModule === "function"
39+
? process.getBuiltinModule("node:util").inspect(v, options)
3640
: `"${String(v).replace(/(?=["\\])/g, "\\")}"`;
3741
}

internal/format_test.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,82 @@ Deno.test("format() doesn't truncate long strings in object", () => {
103103
);
104104
});
105105

106-
Deno.test("format() has fallback to String if Deno.inspect is not available", () => {
106+
Deno.test("format() has fallback to util.inspect if Deno.inspect is not available", async (t) => {
107107
// Simulates the environment where Deno.inspect is not available
108108
const inspect = Deno.inspect;
109109
// deno-lint-ignore no-explicit-any
110110
delete (Deno as any).inspect;
111+
try {
112+
assertEquals(format([..."abcd"]), `[\n 'a',\n 'b',\n 'c',\n 'd',\n]`);
113+
assertEquals(format({ a: 1, b: 2 }), `{\n a: 1,\n b: 2,\n}`);
114+
await t.step("format() sorts properties", () =>
115+
assertEquals(
116+
format({ b: 2, a: 1 }),
117+
format({ a: 1, b: 2 }),
118+
));
119+
120+
await t.step("format() wraps Object with getters", () =>
121+
assertEquals(
122+
format(Object.defineProperty({}, "a", {
123+
enumerable: true,
124+
get() {
125+
return 1;
126+
},
127+
})),
128+
`{
129+
a: [Getter: 1],
130+
}`,
131+
));
132+
133+
await t.step("format() wraps nested small objects", () =>
134+
assertEquals(
135+
stripAnsiCode(format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])),
136+
`[
137+
{
138+
x: {
139+
a: 1,
140+
b: 2,
141+
},
142+
y: [
143+
'a',
144+
'b',
145+
],
146+
},
147+
]`,
148+
));
149+
150+
// Grouping is disabled.
151+
await t.step("format() disables grouping", () =>
152+
assertEquals(
153+
stripAnsiCode(format(["i", "i", "i", "i", "i", "i", "i"])),
154+
`[
155+
'i',
156+
'i',
157+
'i',
158+
'i',
159+
'i',
160+
'i',
161+
'i',
162+
]`,
163+
));
164+
} finally {
165+
Deno.inspect = inspect;
166+
}
167+
});
168+
169+
Deno.test("format() has fallback to String if util.inspect and Deno.inspect are not available", () => {
170+
// Simulates the environment where Deno.inspect and util.inspect are not available
171+
const inspect = Deno.inspect;
172+
// deno-lint-ignore no-explicit-any
173+
delete (Deno as any).inspect;
174+
// deno-lint-ignore no-explicit-any
175+
const { process } = globalThis as any;
176+
const getBuiltinModule = process.getBuiltinModule;
111177
try {
112178
assertEquals(format([..."abcd"]), `"a,b,c,d"`);
113179
assertEquals(format({ a: 1, b: 2 }), `"[object Object]"`);
114180
} finally {
115181
Deno.inspect = inspect;
182+
process.getBuiltinModule = getBuiltinModule;
116183
}
117184
});

0 commit comments

Comments
 (0)