Skip to content

Commit

Permalink
Merge pull request #770 from nextcloud-libraries/fix/default-for-form…
Browse files Browse the repository at this point in the history
…at-size

fix(formatFileSize): Fix default value for `binaryPrefixes`
  • Loading branch information
susnux authored Sep 22, 2023
2 parents 6d59148 + 292c784 commit 99c48b7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
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

0 comments on commit 99c48b7

Please sign in to comment.