-
Notifications
You must be signed in to change notification settings - Fork 1k
Feat: improve formatBytes function #6427
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
Feat: improve formatBytes function #6427
Conversation
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 think we need to use something besides userAgentData
to get the current operating system
@jcfilben thanks for reviewing this, I'll work to fix it |
@jcfilben just pushed the modifications to fix that. I changed |
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 looks good to me, tested on Chrome, Safari, and FireFox on Mac
@MikeKingdom tagging you for review since you have a windows pc and I'm thinking it would be good to check this on different operating systems |
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'll check it on windows shortly but in the meantime one small comment...
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 tested this on windows and it is correct.
However I must note that windows' file explorer uses units of KB, MB etc even though it's technically KiB or MiB. If you're wanting the numbers to match they still won't because windows shows the Size
detail column always in KB. The numbers won't match between the maxsize message and what windows shows in the detail column because windows will still show sizes in KB even if larger than 1024 KB. For example, windows file explore will show a file as 29,820 KB instead of 29.1 MB, In the property dialog for the file it will show "29.1 MB (30,535,680 bytes)". It's still not saying MiB but it will be technically MiB.
@MikeKingdom Hey, thanks for reviewing this again. I've searching about that issue you related on windows, so I found this: "This cannot be changed. Windows Explorer will display the size column in the smallest unit, “KB” for simplicity. If you select your file and view the Details Pane, you will see the file in its true form, whether it’s MB or GB." Said that, do you think is a good approach create a business rule on the code only for windows conversion? |
If the goal is to make it was easier to know if your file is too big by looking at the file explorer details column (which is what you'll see when you click Browse to choose the file assuming that file dialog is set to the "details" view) then I'd think for windows it should show in KiB (the 1024 version) |
@MikeKingdom I've done some changes, so when you have a chance, could you review? |
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.
Thanks Isaac. I think it needs a couple tweaks on what it does for windows
const num = Math.ceil(size / IEC_CONVERSION_FACTOR); | ||
|
||
function formatNumber(number) { | ||
return number.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.'); |
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 this just trying to format the number with a delimiter every 1000? To do this correctly for the browser locale instead do:
Intl.NumberFormat().format(number)
Note that in en-US this uses a comma as the delimiter instead of period.
return number.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.'); | ||
} | ||
|
||
return `${formatNumber(num)} ${'KB'}`; |
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.
Don't need the ${}:
return `${formatNumber(num)} KB`;
@MikeKingdom I send some updates! |
function formatNumber(number) { | ||
return Intl.NumberFormat().format(number); | ||
} | ||
|
||
return `${formatNumber(num)} KB`; |
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.
Looks good and thanks for making the change. It might be nice to simplify this to
return `${Intl.NumberFormat().format(num)} KB`;
since the formatNumber function is trivial and not used elsewhere.
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.
done!
d15af4f
to
0b990fd
Compare
Hi @MikeKingdom could you take a look again? I sent some updates 😃 |
const windowsFormat = (size) => { | ||
const num = Math.ceil(size / IEC_CONVERSION_FACTOR); | ||
|
||
return Intl.NumberFormat().format(num); |
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.
Didn't we lose the units here? Shouldn't it be
return `${Intl.NumberFormat().format(num)} KiB`;
or it wouldn't bother me to say KB but KiB is techinically correct.
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.
yup, I'll change to
return ${Intl.NumberFormat().format(num)} KB;
It is incorrect (technically speaking), but windows don't show to it's users with KiB
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.
Hey, @MikeKingdom just did the updates!
userAgentData ins't compatible with all browsers
0b990fd
to
6276c98
Compare
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.
Looks good. Thanks!
|
||
const getCurrentOS = () => { | ||
const currentOS = ['Win', 'Linux', 'Mac'].find( | ||
(v) => window.navigator.userAgent.indexOf(v) >= 0, |
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.
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.
Thanks to share it!. I'll work on 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.
Looks like a PR for this has already been filed #6480 to fix this. Appreciate the willingness to work on this though!
What does this PR do?
This PR improve
formatBytes
function by making it more dynamic. Based on OS user is on we change the conversion factorfrom 1000 to 1024.
FYI:
Windows assumes that there are 1024 Bytes in a Kilobyte unit, and 1024 Kilobytes in a Megabyte unit etc.
while Ubuntu and Mac assumes, a 1000 bytes constitute a Kilobyte (KB) unit, 1000 Kilobytes for a Megabyte (MB) and so on.
That is, same file on ubuntu and mac are larger than windows
For example:
So, if you try to attach same file in different OS you're gonna get an error
Where should the reviewer start?
src/js/components/FileInput/FileInput.js
What testing has been done on this PR?
How should this be manually tested?
Do Jest tests follow these best practices?
screen
is used for querying.userEvent
is used in place offireEvent
.asFragment()
is used for snapshot testing.Any background context you want to provide?
What are the relevant issues?
closes #5807
Screenshots (if appropriate)
Do the grommet docs need to be updated?
Should this PR be mentioned in the release notes?
Is this change backwards compatible or is it a breaking change?