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

Truncate long Set and Map in util.inspect/console.log/REPL, just like long Array #42933

Closed
lydell opened this issue May 1, 2022 · 4 comments
Closed
Labels
feature request Issues that request new features to be added to Node.js. stale util Issues and PRs related to the built-in util module.

Comments

@lydell
Copy link

lydell commented May 1, 2022

What is the problem this feature will solve?

console.log(), util.inspect() and REPL prints can become unreadable if you happen to have a very large Set or Map as part of the data you’re passing. But if you have a very large Array, the print helpfully cuts off after 100 items (adding something like ... 900 more items at the end).

What is the feature you are proposing to solve the problem?

  1. In 2015, support for Set and Map were added to util.inspect: util: support inspecting Map, Set, and Promise #1471
  2. In 2016, the maxArrayLength option (defaulting to 100) was added to util.inspect: util: truncate inspect array and typed array #6334
  3. In 2018, maxArrayLength was extended to also cover WeakSet and WeakMap: util: improve Weak(Map|Set) support #19259

So maxArrayLength covers Array, TypedArray, WeakMap and WeakSet, but not plain Map and Set.

In the last PR I linked to above, there was even a TODO comment at one point suggesting to rename maxArrayLength to maxEntries: #19259 (comment)

I suggest that maxArrayLength keeps it name (renaming would be another issue), but also applies to Map and Set.

What alternatives have you considered?

I could add something like this to my application:

Set.prototype[util.inspect.custom] = function(depth, opts, inspect) {
  return `Set(${this.size}) ${inspect(Array.from(this))}`;
};

But it feels a bit dirty, and I also seem to lose color that way. I also need to do it in every project, and it’s unclear how to get it into the REPL.

@lydell lydell added the feature request Issues that request new features to be added to Node.js. label May 1, 2022
@targos
Copy link
Member

targos commented May 2, 2022

@nodejs/util

@VoltrexKeyva VoltrexKeyva added the util Issues and PRs related to the built-in util module. label May 2, 2022
@ljharb
Copy link
Member

ljharb commented May 2, 2022

Having a max option that applies to entries makes sense, because Record/Tuple are coming - I don't think the name "array" in that option would make sense.

I think a new maxEntries option that, when provided, takes precedence over maxArrayLength, would be a good semver-minor approach.

@lydell
Copy link
Author

lydell commented Jul 14, 2022

Should this be closed now that #43576 was merged?

@github-actions
Copy link
Contributor

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Jan 11, 2023
@lydell lydell closed this as completed Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. stale util Issues and PRs related to the built-in util module.
Projects
Development

No branches or pull requests

4 participants