Ende.inspect
function
function Ende.inspect(value: unknown, options?: InspectOptions): stringConverts the input into a string that has the same format as printed by
console.log().
const obj = {
a: 10,
b: "hello",
};
const objAsString = Ende.inspect(obj); // { a: 10, b: "hello" }
console.log(obj); // prints same value as objAsString, e.g. { a: 10, b: "hello" }
A custom inspect function can be registered on objects, via the symbol
Symbol.for("Ende.customInspect"), to control and customize the output
of inspect() or when using console logging. The function receives
inspect and the current Ende.CustomInspectOptions, including
the traversal depth and indentation level:
class A {
x = 10;
y = "hello";
[Symbol.for("Ende.customInspect")](
inspect: typeof Ende.inspect,
options: Ende.CustomInspectOptions,
) {
return `x=${inspect(this.x, options)}, y=${inspect(this.y, options)}`;
}
}
const inStringFormat = Ende.inspect(new A()); // 'x=10, y="hello"'
console.log(inStringFormat); // prints 'x=10, y="hello"'
Symbol.for("Deno.customInspect") still works. If an object has both,
the Ende one is used.
A depth can be specified by using the depth option:
Ende.inspect({a: {b: {c: {d: 'hello'}}}}, {depth: 2}); // { a: { b: [Object] } }