Comments (6)
See #17
from bignumber.js.
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.
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.
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.
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.
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)
- Using version 9.1.1, run new BigNumber(0).exponentiatedBy('-12345678901234567890123456789012345678901234567890').valueOf() Is return to infinity normal? HOT 1
- Issue toNumber() function round up when number is too big (v9.1.1) HOT 2
- Division on exponential values leads to infinite loop HOT 3
- 负数加减乘除不好用 HOT 1
- [Feature Request]: Can we support `toBigInt`?
- Type error HOT 2
- Big Number (toFixed,dp,decimalPlaces,toFormat) - Chrome Update Issue 115.0.5790.102 HOT 24
- Minus sign in formatted strings HOT 2
- wrong calculation result on multipliedBy/times methods HOT 1
- toNumber does not respect ROUNDING_MODE HOT 11
- Calculation error HOT 1
- Is there any other way to handle new BigNumber(999999999999999999999) without using strings? HOT 2
- Difference between multipliedBy and dividedBy behaviors HOT 2
- Different result between toFixed and toNumber HOT 1
- Can't convert to Base64 HOT 1
- Tree shake commonjs version from mjs version HOT 1
- toFixed error
- [Bug] Return Type of Methods HOT 2
- Logarithms (I know there is another issue about this)
- toString() default base is not 10? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bignumber.js.