Skip to content

Better Node.js inspect() support [PR-ready] #744

Closed
@cAttte

Description

@cAttte

currently, the classes in this library expose a method called inspect(). this is fine, but it's not very useful because the thing that it's modeled after, node:util's inspect(), doesn't know how to use it. so if you plainly console.log(something) it will look very ugly. the way to fix this is by defining another method, [Symbol.for("nodejs.util.inspect.custom")]—see this example for Just:

class Just {
    [Symbol.for("nodejs.util.inspect.custom")](_, opts, inspect) {
        return `Just(${inspect(this.__value, opts)})`;
    }
}

as you can see, this method also harnesses the passed-down inspect function to get the sub-value(s) recursively pretty-printed inside the parentheses, which avoids boring white output and [object Object]. having defined this method, it's as simple as e.g. console.log(Just(new Date)) and you get pretty output for free!

this would only affect Node.js behavior; this symbol is not globally registered in the browser so nothing will happen there at all. i'm down to open a PR so just let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions