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

More readable objects on console #46

Open
arshaw opened this issue Jul 25, 2024 · 2 comments
Open

More readable objects on console #46

arshaw opened this issue Jul 25, 2024 · 2 comments

Comments

@arshaw
Copy link
Member

arshaw commented Jul 25, 2024

For example, when I'm using Google Chrome dev console, and I run this:

const dur = Temporal.Duration.from({ months: 11 });
dur.round({ relativeTo: "2023-05-31", smallestUnit: "days", roundingMode: "ceil" });

Here's what's shown for temporal-polyfill:

> Temporal.Duration {}

But @js-temporal/polyfill does it nicer:

> Duration {_repr_: 'Temporal.Duration <P10M30D>'}

TODO: figure out how to do something like this

@fabon-f
Copy link

fabon-f commented Jul 26, 2024

Apparently it's implemented in tc39/proposal-temporal#712 .

@arshaw
Copy link
Member Author

arshaw commented Jul 26, 2024

Thanks for the pointer @fabon-f

For browsers, seems like the only way to do this is to add an own-property to the object (_repr_). In Node, you can use Symbol.for('nodejs.util.inspect.custom') (link).

@js-temporal/polyfill uses _repr_ for both browser/Node, and allows for conditional removal via injecting a __debug__ global.

I'm sensitive to people not wanting the upfront computation of _repr_ for all created Temporal objects, and I'd prefer not to force them to jump through hoops with their bundler to set __debug__ in order to strip it out.

I thought of a different solution: detect if the currently executing code is minified, and if it is, skip the _repr_ computation and assignment. You can detect like this:

function explicitlyNamedFunction() {}

function isMinified() {
  return explicitlyNamedFunction.name !== 'explicitlyNamedFunction'
}

For Node, we'd want to avoid all this and just use Symbol.for('nodejs.util.inspect.custom'). We might want to detect the environment using subpath imports.

If _repr_ needs to be computed up-front, we'd want to reuse the same value for future toString calls.

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

2 participants