Giter VIP home page Giter VIP logo

Comments (6)

Xotic750 avatar Xotic750 commented on June 3, 2024

See #17

from bignumber.js.

MikeMcl avatar MikeMcl commented on June 3, 2024

Okay guys, I'll implement this and test it out.

I've been reluctant to do it so far as

i) the configuration properties will need to be stored as public properties of each BigNumber constructor rather than as private variables as currently, which means it will be possible to write to them directly without the checking that is peformed by the config method - and this makes the library less robust

ii) there will be a small performance cost and some unavoidable code bloat

iii) it will be time-consuming to implement as it means a lot of small changes everywhere, and tests and documentation additions will be required

iv) users who want this functionality may be able to use big.js or decimal.js instead, as both include it

v) it is unnecessary: configuration is usually a one-time operation per project and config can easily be wrapped/overwritten to prevent changes if required. (If some code wants to use a different decimal places or rounding mode, for example, it can just use round).

from bignumber.js.

clark800 avatar clark800 commented on June 3, 2024

i) the configuration properties will need to be stored as public properties of each BigNumber constructor

I think you should be able to keep them private by using a closure. Here is an example of what I was thinking of: https://gist.github.com/clark800/26b62ec05fe15be51652

v) it is unnecessary

I don't think there is a safe way to fix this issue outside of the library itself, except maybe making a new npm package that wraps it. If you just make a wrapper class or overwrite the config function, there is nothing stopping someone from requiring bignumber.js directly and changing the global config from there.

from bignumber.js.

MikeMcl avatar MikeMcl commented on June 3, 2024

Yes, I shouldn't have written need to. It's just how I decided to implement it in decimal.js, many things considered, including the option of storing the config settings within the closure along with a method to retrieve them. The issue was more whether to, than how to.

If you just make a wrapper class or overwrite the config function, there is nothing stopping someone from requiring bignumber.js directly and changing the global config from there.

I'm not sure what you mean here. If the config method is overwritten, then there is no way for other code to change the global config.

Even with the multiple constructor functionality, other code can still alter any BigNumber constructor it has a reference to, in the same way that it can alter other globals, wihch is why I haven't thought it is necessary or much of an issue. If other code is sharing the same global, then it doesn't seem to be too much to ask that it is mindful of the state of that global.

But yes, I can see that it would be useful on larger projects, so I've implemented it, but I'm in the process of making other changes/additions so, with the documentation updates etc., I expect it will be ready in about a week.

from bignumber.js.

clark800 avatar clark800 commented on June 3, 2024

I'm not sure what you mean here. If the config method is overwritten, then there is no way for other code to change the global config.

Sorry, I take back the part about overwriting the config function. That could work if you actually want to freeze the config, but it doesn't help if you need different configs in different parts of your code. I'm working on a NodeJS project that breaks if DECIMAL_PLACES is set less than about 40 and also has to switch rounding modes for certain functions (and not all methods in the library that use rounding take a rounding parameter).

other code can still alter any BigNumber constructor it has a reference to

In NodeJS we would have a separate BigNumber constructor in each module, which is fine. But in a project with hundreds of modules and many developers, it is risky assume that nobody else will ever change the configuration in a new module and we also don't want to force other modules to use the same configuration that we are using in our module. Different modules may use bignumber.js for very different applications.

Anyway, I've implemented it

Awesome, thanks!!

from bignumber.js.

MikeMcl avatar MikeMcl commented on June 3, 2024

Added an another method in v2.0.1, so multiple independent BigNumber constructors can be created.

A bit later than predicted, but one change seemed to lead to another.

More methods now accept rounding modes also.

from bignumber.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.