-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c3cc0f
commit 5e3b936
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { helper } from '@ember/component/helper'; | ||
import { isEmpty } from '@ember/utils'; | ||
|
||
export function formatBytes([number]/*, hash*/) { | ||
if (isEmpty(number)) { | ||
return ''; | ||
} | ||
if (number >= Math.pow(1024, 3)) { | ||
return format(number, Math.pow(1024, 3), 'GB'); | ||
} | ||
if (number >= Math.pow(1024, 2)) { | ||
return format(number, Math.pow(1024, 2), 'MB'); | ||
} | ||
if (number >= 1024) { | ||
return format(number, 1024, 'KB'); | ||
} | ||
return `${number} B`; | ||
} | ||
|
||
function format(number, denominator, unit) { | ||
return `${parseFloat((number / denominator).toFixed(2))} ${unit}` | ||
} | ||
|
||
export default helper(formatBytes); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { render } from '@ember/test-helpers'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
module('Integration | Helper | format-bytes', function(hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('it renders nothing if size is null', async function(assert) { | ||
this.set('inputValue', null); | ||
|
||
await render(hbs`{{format-bytes inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), ''); | ||
}); | ||
|
||
test('it renders bytes', async function(assert) { | ||
this.set('inputValue', '12'); | ||
|
||
await render(hbs`{{format-bytes inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), '12 B'); | ||
}); | ||
|
||
test('it renders kilobytes', async function(assert) { | ||
this.set('inputValue', '7785'); | ||
|
||
await render(hbs`{{format-bytes inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), '7.6 KB'); | ||
}); | ||
|
||
test('it renders one kilobyte', async function(assert) { | ||
this.set('inputValue', '1024'); | ||
|
||
await render(hbs`{{format-bytes inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), '1 KB'); | ||
}); | ||
|
||
test('it renders megabytes', async function(assert) { | ||
this.set('inputValue', '2809565'); | ||
|
||
await render(hbs`{{format-bytes inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), '2.68 MB'); | ||
}); | ||
|
||
test('it renders one megabyte', async function(assert) { | ||
this.set('inputValue', '1048576'); | ||
|
||
await render(hbs`{{format-bytes inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), '1 MB'); | ||
}); | ||
|
||
test('it renders gigabytes', async function(assert) { | ||
this.set('inputValue', '1095216660'); | ||
|
||
await render(hbs`{{format-bytes inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), '1.02 GB'); | ||
}); | ||
|
||
test('it renders one gigabyte', async function(assert) { | ||
this.set('inputValue', '1073741824'); | ||
|
||
await render(hbs`{{format-bytes inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), '1 GB'); | ||
}); | ||
}); |