Comparison and inspection APIs: Bun.peek, Bun.deepEquals, Bun.deepMatch, Bun.inspect
Bun provides comparison and inspection utilities: Bun.peek() for inspecting values without logging, Bun.deepEquals() for deep equality checking, Bun.deepMatch for pattern matching, and Bun.inspect() for value inspection.
console.depth configuration in bunfig.toml
The depth to which console.log() prints nested objects can be configured by setting console.depth in bunfig.toml to persist the setting across runs.
Default console.log() inspection depth
By default, console.log() inspects nested objects to a depth of 2 levels. Deeper levels are truncated and shown as [Object ...].
console as AsyncIterable for reading stdin
In Bun, the console object is an AsyncIterable that reads from process.stdin line by line. You can use for await (const line of console) to read user input interactively.
--console-depth CLI flag
Use the --console-depth <number> CLI flag to set how deeply console.log() prints nested objects for a single run. This flag takes precedence over the console.depth configuration in bunfig.toml.
Interactive stdin reading example with console
Example of reading stdin with console as AsyncIterable:
```ts
for await (const line of console) {
console.log(line);
}
```
This reads process.stdin line by line and logs each line.
Interactive calculator example using console AsyncIterable
Example of an interactive addition calculator:
```ts
console.log(`Let's add some numbers!`);
console.write(`Count: 0\n> `);
let count = 0;
for await (const line of console) {
count += Number(line);
console.write(`Count: ${count}\n> `);
}
```
This demonstrates reading stdin interactively and maintaining state between inputs.
console object browser and Node.js compatibility
Bun provides a browser- and Node.js-compatible console global that follows the standard console API.
console.write() method for output without newline
The console.write() method outputs text without automatically adding a newline, allowing control over formatting in interactive programs.
Bun.semver.order() - compare two versions
Bun.semver.order(versionA: string, versionB: string) returns 0 if versionA and versionB are equal, 1 if versionA is greater than versionB, and -1 if versionA is less than versionB. Returns type is 0 | 1 | -1. This function can be used as a comparator for Array.sort().
Bun.semver.satisfies() - check version matches range
Bun.semver.satisfies(version: string, range: string) returns true if version satisfies range, otherwise false. The version and range parameters are strings compatible with node-semver. If range or version is invalid, it returns false.
Bun.semver performance
Bun.semver is approximately 20x faster than node-semver.
Bun.semver.satisfies() examples
Example usage of Bun.semver.satisfies():
import { semver } from "bun";
semver.satisfies("1.0.0", "^1.0.0"); // true
semver.satisfies("1.0.0", "^1.0.1"); // false
semver.satisfies("1.0.0", "~1.0.0"); // true
semver.satisfies("1.0.0", "~1.0.1"); // false
semver.satisfies("1.0.0", "1.0.0"); // true
semver.satisfies("1.0.0", "1.0.1"); // false
semver.satisfies("1.0.1", "1.0.0"); // false
semver.satisfies("1.0.0", "1.0.x"); // true
semver.satisfies("1.0.0", "1.x.x"); // true
semver.satisfies("1.0.0", "x.x.x"); // true
semver.satisfies("1.0.0", "1.0.0 - 2.0.0"); // true
semver.satisfies("1.0.0", "1.0.0 - 1.0.1"); // true
Bun.semver.order() examples
Example usage of Bun.semver.order():
import { semver } from "bun";
semver.order("1.0.0", "1.0.0"); // 0
semver.order("1.0.0", "1.0.1"); // -1
semver.order("1.0.1", "1.0.0"); // 1
const unsorted = ["1.0.0", "1.0.1", "1.0.0-alpha", "1.0.0-beta", "1.0.0-rc"];
unsorted.sort(semver.order); // ["1.0.0-alpha", "1.0.0-beta", "1.0.0-rc", "1.0.0", "1.0.1"]
console.log(unsorted);
Bun.semver compatibility with node-semver
Bun.semver versions and ranges are designed to be compatible with node-semver, which npm clients use.
Example: Bun.deepEquals strict mode
const a = { entries: [1, 2] };
const b = { entries: [1, 2], extra: undefined };
Bun.deepEquals(a, b); // => true
Bun.deepEquals(a, b, true); // => false (strict mode)
Bun.deepEquals() recursively compares objects
Bun.deepEquals(a: any, b: any, strict?: boolean) recursively checks if two objects are equivalent. Pass a third boolean parameter true to enable strict mode, which treats undefined values, undefined in arrays, sparse arrays, and object literals versus instances as unequal.
Bun.inspect() serializes object to string
Bun.inspect(obj: any): string serializes an object to a string exactly as it would be printed by console.log.
Bun.inspect.custom symbol for custom printing
Bun.inspect.custom is a symbol that Bun uses to implement Bun.inspect. Override it on objects to customize how they are printed. It is identical to util.inspect.custom in Node.js.
Bun.inspect.table() formats tabular data as string
Bun.inspect.table(tabularData: any[], properties?: string[], options?: {colors?: boolean}): string formats tabular data into a string like console.table but returns a string instead of printing. Pass an array of property names to display only those columns. Pass {colors: true} to enable ANSI colors.