-
Notifications
You must be signed in to change notification settings - Fork 103
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
Implement binary prefixes #70
Conversation
There are a few considerations:
I tried to remain consistent in style with the rest of the code base, however I’m not used to writing this terse code so pardon my mistakes 😉 Finally, @mbostock thanks for considering this PR. I’m big fan of your work 😎 |
Nice PR; I hope it gets reviewed! @mbostock |
export default function(x, p) { | ||
var binaryExponent = 0; | ||
|
||
while (Math.round(x) >= 1024 && binaryExponent < 80) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a closed form for doing this, rather than needing a loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it looks like this will hang if you try to format Infinity, but I didn’t test it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn’t investigate any closed forms. I wouldn’t be surprised if one existed using the power of 2. But this loop will run at most 8 times as far as I can tell, even for Infinity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a test that confirms that infinity does not hang indefinitely. However if someone can point out a closed form, it would probably be preferable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds right; I was too hasty reading the code. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be possible to use Math.log2
here?
(or the IE-compatible equivalent Math.log(number) / Math.log(2)
)
Closes: d3#33
Co-authored-by: Philippe Rivière <[email protected]>
Co-authored-by: Philippe Rivière <[email protected]>
Co-authored-by: Philippe Rivière <[email protected]>
|
||
export default function(x, p) { | ||
var binaryExponent = 0; | ||
if (x === Infinity) return binaryPrefixExponent = 0, x; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is confusing.
Why not
if (x === Infinity) return Infinity;
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably because it needs to set the exported binaryPrefixExponent (note, not binaryExponent).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aaaahhhhh I see. Side effects. Thanks for pointing that out 🙏
var tape = require("tape"), | ||
format = require("../"); | ||
|
||
tape("format(\"B\") outputs binary-prefix notation with default precision 6", function(test) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great comprehensive tests!
Suggest to close this in favor of #96 |
closed in favor of #96 |
Should this be reopened since #96 is still being discussed? |
It looks like that PR #96 is stalled. It has a ton of changes in it. I don't remember the context for all this, but it seems that the PR has grown too large to ever merge. I feel like we need to take a step back and consider the use case(s) and a smaller subset of changes to consider. @grotlue Is there a particular subset of the changes that interest you? The tests are great - https://github.com/d3/d3-format/pull/96/files - maybe you could identify specific tests that are less ... controversial than all of them together? Then maybe a new smaller PR could be opened that only adds a small part of the feature that would be sensible to add to the library and maintain in the long term, and would be of value to a lot of people. The other alternative is to author a new NPM package separate from mainline D3 that implements the features from that PR. |
I don’t understand why d3-format needs a currency format since const usdFormat = new Intl.NumberFormat(
"en-US",
{
style: "currency",
currency: "USD",
currencyDisplay: "symbol",
notation: "compact",
}
);
usdFormat.format(1_000_000)
// => "$1M" However there is no vanilla way to get binary prefixes (or any SI prefixes) with That said, since this PR was closed, I simply rolled my own way of achieving binary prefixes in my own code. #96 (comment) |
Hi @runarberg, @Fil, @curran, thanks a lot for your replies. I had a look at it again and the changes @runarberg mentioned in #33 (comment) and updated in runarberg@3f2383d would be sufficient for my use case. Anything preventing from merging them? |
Hi @runarberg @Fil @curran, can this be merged? |
Closes: #33