Skip to content

Commit

Permalink
Add helper to format sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelyghost committed Apr 12, 2020
1 parent 2c3cc0f commit 5e3b936
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/helpers/format-bytes.js
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);
72 changes: 72 additions & 0 deletions tests/integration/helpers/format-bytes-test.js
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');
});
});

0 comments on commit 5e3b936

Please sign in to comment.