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

Add inspector formatting #173

Closed
Darkhogg opened this issue Jul 6, 2021 · 2 comments
Closed

Add inspector formatting #173

Darkhogg opened this issue Jul 6, 2021 · 2 comments

Comments

@Darkhogg
Copy link

Darkhogg commented Jul 6, 2021

Sometimes one needs to print out Big values or objects containing them, and at least in Node the output is considerably ugly and mostly useless for a human:

> new Big('1337.6942')
Big {
  s: 1,
  e: 3,
  c: [
    1, 3, 3, 7,
    6, 9, 4, 2
  ],
  constructor: [Function: Big] {
    DP: 20,
    RM: 1,
    NE: -7,
    PE: 21,
    version: '5.2.2',
    Big: [Circular],
    default: [Circular]
  }
}

I'm adding this to my projects using Big so that output is a bit friendlier:

Big.prototype[Symbol.for('nodejs.util.inspect.custom')] = function(depth, options) {
    const name = options.stylize('Big', 'special');
    const amount = options.stylize(this.toString(), 'number');
    return `${name}(${amount})`;
};

With this, the ouput would be like this:

> new Big('1337.6942')
Big(1337.6942)

which clearly shows that the value being printed is a Big and its human friendly representation


Please consider adding this or something similar to it to the base code so printing Big in node is friendlier to humans.

@MikeMcl
Copy link
Owner

MikeMcl commented Jul 6, 2021

Yes, I did have this in bignumber.js but I took it out because the reference to Symbol caused problems in some legacy browsers. I just added a note to the README instead, and I could do that here also.

Big.prototype[Symbol.for('nodejs.util.inspect.custom')] = Big.prototype.toString;

@MikeMcl
Copy link
Owner

MikeMcl commented Jun 3, 2022

Added to big.mjs in v7.0.0 soon to be published.

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