Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cAttte opened this issue Feb 6, 2025 · 0 comments
Open

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

cAttte opened this issue Feb 6, 2025 · 0 comments

Comments

@cAttte
Copy link

cAttte commented Feb 6, 2025

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.

@cAttte cAttte changed the title Better Node.js inspect() support Better Node.js inspect() support [PR-ready] Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant