-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Feature Request: Add formatting options to util.inspect()
#47744
Comments
I think you can set opt util.inspect(obj, {
compact: false,
}); I tried it and it showed something like this:
|
@HinataKah0 Ah wasn't aware of that, thanks! That solves one of the suggestions 😄 |
I think it's possible to have an opt for modifying indentation In But let's see the collaborators' thoughts if we want to have it or not 😄 |
Thank's awesome! Thanks for already looking into it 😃 |
As the documentation says,
Sure, we could add more and more options to allow fine-grained control over the output format, but why? Why should we encourage very specific format options (such as trailing commas) for a function whose output format developers were never supposed to rely on? |
@tniessen Well, unfortunately there aren't many other options to get a cleanly formatted string representation without using e.g. So why not improve the only built-in method we currently have for that? Or, as mentioned in my suggestion above, implement a helper method for such formatting if it's not in the scope of |
Could you clarify your use case? What do you want to use the output for? Why do you need trailing commas, for example? |
@tniessen My current use case would be automatically writing an object to a new JS file without breaking our linting. This use case is rather specific but those options for "consistent code style" probably find other applications (such as generating documentations) as well. I also do see potential for use cases such as tooling / logging. For instance printing objects in some "smart warnings" while adhering to previous logging styles. I understand that the method is meant for debugging, but in practice developers do use the output of (Added this reasoning to the original issue) |
This implies that the formatting function's options must be powerful enough to adhere to whatever linting you apply, which implies a high level of complexity. Also, the output of |
That's true. As I said my personal use case is rather specific. The objects in question aren't deeply nested / complex. But I do see some other use cases that mainly involve printing and not using it as-is in code. And of course adding options to cover all linting cases is completely overkill, which is why I only proposed indentation and quotes. Trailing comma is not necessary and line breaks are already covered |
Miss at least indentation option. console.log(
util.inspect(obj, {colors: 1}).replace(
/^ +/gm, a => '\t'.repeat(a.length / 2)
)
); to get tab indentation. (Find two space indentation way to heavy/straining to read in the long run, and very hard to see on bigger objects). |
You should really use |
I believe the consensus here is "no" (and I agree for the reasons Tobias outlined) so I'll go ahead and close this out. |
Rel: #14638
I wanted to propose a feature request regarding util.inspect():
Add options to control
Currently:
Results in:
Proposal:
Which would result in:
Which almost resembles the original.
If this isn't in the scope for
util.inspect()
maybe a different formatting helper function?Addendum: Rationalé / Use Cases (from #47744 (comment))
The text was updated successfully, but these errors were encountered: