-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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(formatting): Add memory units adaptive formatter to format bytes #30559
feat(formatting): Add memory units adaptive formatter to format bytes #30559
Conversation
I've created a discussion for this PR as well: #30558 |
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 is a great feature, and one that many have been looking forward to seeing. In general this looks good, but would you be able to add some unit tests?
@villebro , sure thing, let me give it a try :) I never wrote any Javascript before, therefore happy to get some feedback :) |
- Make decimals option a parameter of MemoryFormatter - Handle negative values in MemoryFormatter - Handle values exceeding maximal unit value in MemoryFormatter
@villebro I added unit tests and fixed some bugs on the way :) Let me know what you think! |
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.
LGTM after the linting issue is fixed
expect(formatter(1000)).toBe('1kB'); | ||
expect(formatter(1111)).toBe('1.11kB'); | ||
expect(formatter(1024)).toBe('1.02kB'); | ||
expect(formatter(1337)).toBe('1.34kB'); |
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.
lol
if (value === 0) return '0B'; | ||
|
||
const sign = value > 0 ? '' : '-'; | ||
value = Math.abs(value); |
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.
Just create a new const
and use that to get around the linting error:
value = Math.abs(value); | |
const absValue = Math.abs(value); |
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.
changed as suggested
describe('createMemoryFormatter()', () => { | ||
it('creates an instance of MemoryFormatter', () => { | ||
const formatter = createMemoryFormatter(); | ||
expect(formatter).toBeInstanceOf(NumberFormatter); | ||
}); | ||
it('formats bytes in human readable format with default options', () => { |
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.
Oh, one more thing, we try to steer away from nested tests. So instead of doing describe('foo)
with many it('bar')
and it('baz')
, just do individual test('foo bar')
and test('foo baz')
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.
sure, done
- Avoid function parameter reassignment in MemoryFormatter
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 great, fingers crossed for CI to pass 🤞 Only comment is it'd be great to be able to customize the decimals, but that's outside the scope of this PR. I believe there's a discussion about this lying around somewhere, and would be a great improvement (the idea would be to be able to provide arbitrary config objects to the formatters), but that would require a SIP, so let's leave that for a future improvement.
Thanks @villebro for your help merging this :) |
Add new memory units adaptive formatter to format bytes units into kB, MB, GB, etc.
SUMMARY
Memory unit (bytes) is typically formatted into kB (kilobytes), MB (megabytes), etc.
Superset formatting options don't enable adaptive formatting with proper unit naming in case of bytes values.
There are two most commonly used multi-byte unit types (according to wikipedia), this PR implements them both.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
TESTING INSTRUCTIONS
ADDITIONAL INFORMATION