Skip to content
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

fix(formatFileSize): Fix default value for binaryPrefixes #770

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions __tests__/humanFileSize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { formatFileSize } from '../lib/humanfilesize'

describe('humanFileSize', () => {
describe('formatFileSize', () => {
it('renders binary sizes by default', () => {
expect(formatFileSize(2048)).toBe('2 KiB')
})

it('renders file sizes with the correct unit', function() {
const dataDecimal = [
[0, '0 B'],
Expand All @@ -30,7 +34,7 @@ describe('humanFileSize', () => {
[128000000000000000.0, '113.7 PiB'],
]
for (let i = 0; i < dataDecimal.length; i++) {
expect(formatFileSize(dataDecimal[i][0])).toEqual(dataDecimal[i][1])
expect(formatFileSize(dataDecimal[i][0], false, false)).toEqual(dataDecimal[i][1])
}
for (let i = 0; i < dataBinary.length; i++) {
expect(formatFileSize(dataBinary[i][0], false, true)).toEqual(dataBinary[i][1])
Expand Down Expand Up @@ -59,7 +63,7 @@ describe('humanFileSize', () => {
[128000000000000000.0, '113.7 PiB'],
]
for (let i = 0; i < dataDecimal.length; i++) {
expect(formatFileSize(dataDecimal[i][0], true)).toEqual(dataDecimal[i][1])
expect(formatFileSize(dataDecimal[i][0], true, false)).toEqual(dataDecimal[i][1])
}
for (let i = 0; i < dataBinary.length; i++) {
expect(formatFileSize(dataBinary[i][0], true, true)).toEqual(dataBinary[i][1])
Expand Down Expand Up @@ -93,7 +97,7 @@ describe('humanFileSize', () => {
[128000000000000000.0, '113,7 PiB'],
]
for (let i = 0; i < dataDecimal.length; i++) {
expect(formatFileSize(dataDecimal[i][0])).toEqual(dataDecimal[i][1])
expect(formatFileSize(dataDecimal[i][0], false, false)).toEqual(dataDecimal[i][1])
}
for (let i = 0; i < dataBinary.length; i++) {
expect(formatFileSize(dataBinary[i][0], false, true)).toEqual(dataBinary[i][1])
Expand Down
4 changes: 2 additions & 2 deletions lib/humanfilesize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ const humanListBinary = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB']
*
* @param size in bytes
* @param skipSmallSizes avoid rendering tiny sizes and return '< 1 KB' instead
* @param binaryPrefixes True if binary prefixes like `KiB` should be used (size base 2)
* @param binaryPrefixes True if size base 2 (and binary prefixes like `KiB`) should be used
*/
export function formatFileSize(size: number|string, skipSmallSizes = false, binaryPrefixes = false): string {
export function formatFileSize(size: number|string, skipSmallSizes = false, binaryPrefixes = true): string {

if (typeof size === 'string') {
size = Number(size)
Expand Down
Loading