最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Equivalent of `util.inspect` in Deno - Stack Overflow

programmeradmin0浏览0评论

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
Add a comment  | 

3 Answers 3

Reset to default 14

Deno'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.

发布评论

评论列表(0)

  1. 暂无评论