Does Deno have a utility function that dumps a Object or primitive to a string like Node.js util.inspect
?
For example in Node.js I create an json object and want to dump out its contents in Node:
> m = {k1:'v1', k2:'v2'}
{ k1: 'v1', k2: 'v2' }
> util.inspect(m)
'{ k1: \'v1\', k2: \'v2\' }'
Does Deno have a utility function that dumps a Object or primitive to a string like Node.js util.inspect
?
For example in Node.js I create an json object and want to dump out its contents in Node:
> m = {k1:'v1', k2:'v2'}
{ k1: 'v1', k2: 'v2' }
> util.inspect(m)
'{ k1: \'v1\', k2: \'v2\' }'
Share
Improve this question
edited Apr 20, 2022 at 22:46
aef
4,6987 gold badges28 silver badges46 bronze badges
asked May 11, 2020 at 3:49
Timothy C. QuinnTimothy C. Quinn
4,4951 gold badge44 silver badges53 bronze badges
3 Answers
Reset to default 14Deno's equivalent of Node's util.inspect
is Deno.inspect
.
For example in the deno REPL:
> m = {k1:'v1', k2:'v2'}
{ k1: "v1", k2: "v2" }
> Deno.inspect(m)
{ k1: "v1", k2: "v2" }
Additionally, if you want to override to return your own results from Deno.inspect
, you can add a Deno.customInspect
method to your classes like so:
class someClass {
// code ...
[Symbol.for("Deno.customInspect")]() {
return 'any ol string';
}
}
Here's the documentation for Deno.inspect
and Deno.customInspect
Generally use Deno.inspect(someObject)
.
In case you want to customize the string representation of Deno.inspect
for a specific class, since Deno 1.19, you are supposed to implement something like the following:
class MyClass {
// …
[Symbol.for("Deno.customInspect")]() {
return "custom string representation;
}
// …
}
Objects displayed using console.log(someObject)
will also be affected by this.
Documentation can be found here.
Note that the version shown by @Lukalot is deprecated since Deno 1.19 and might therefore stop to work in future versions.