-
Notifications
You must be signed in to change notification settings - Fork 417
Support plus sign before numbers #193
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
Conversation
|
@MikeMcl Have you had a chance to look at the changes yet? I use big.js to parse numbers from CSS stylesheets, but the CSS spec explicitly allows a positive sign. If that would be supported I can use it to directly parse the numbers from my stylesheets. |
|
Although I don't have a direct need for this capability, it makes sense. The |
|
It's a reasonable suggestion, but I find it a little strange that you have gone straight to a PR when support for a leading plus sign is not likely to have been something I hadn't thought of previously but rather something that was deliberately not included. I am reluctant to accept this because it's another One possibility is a |
|
Sorry, I hadn't looked through the closed issues. Should have done that before opening the PR. I assumed that a leading plus sign just wasn't a feature that many people required and nobody was missing it. I totally agree to keep the number parsing scope of big.js to a minimum, after all it's a library for calculating numbers and not for parsing numbers. I think a well defined scope would be to support any decimal number string in a format that is also supported by the Number constructor, but nothing more. That is probably also what most users expect. It would also define that NumericLiteralSeparator and spaces are invalid, so there is no need to add support for those. The performance impacts of adding support for a leading plus sign are basically zero. I did some benchmarking and could not find any significant performance difference between supporting plus or not. Detailed benchmark resultsI tested the performance of the Big function with the following test function: const test = (value) => {
const a = new Date()
for(var i = 0; i < 1e8; ++i) { Big(value) }
const b = new Date()
return b.getTime() - a.getTime()
}I tested my fork against the current master with three different values
I think there are enough users who want a Big constructor that is more analogous to the Number constructor, to warrant a single line of code more. |
I agree with your assumption that it isn't a feature that many people require.
Hmmm... this seems to contradict your above assumption somewhat and your PR involves more than adding a single line.
Well, we can have, for example Number(' \n +2 \t\n ') // 2so I suppose you mean whatever is supported by Other arguments for are that a leading plus sign is supported by sister libraries decimal.js and bignumber.js, and also by Java's BigDecimal and Python's Decimal, and that this library already supports the similarly redundant plus sign for exponents. Big('1e+2').toString(); // '100'It's a borderline decision (that has already been made and remade previously) and in such cases here I usually follow the maxim: 'If in doubt, leave it out'. Anyway, thanks for your efforts. I may include this but I'm not deciding the matter today. |
|
Closing this PR, because it probably does not fit into big.js. I have published this as Thanks for the creating this library, I really enjoy using it. |
This adds support for parsing positive signed numbers like
+1.