diff --git a/lib/gui/models/flash-state.js b/lib/gui/models/flash-state.js index 65d7e931..56f8106b 100644 --- a/lib/gui/models/flash-state.js +++ b/lib/gui/models/flash-state.js @@ -25,6 +25,7 @@ const _ = require('lodash'); const Store = require('./store'); const MODULE_NAME = 'Etcher.Models.FlashState'; const FlashState = angular.module(MODULE_NAME, []); +const units = require('../../shared/units'); FlashState.service('FlashStateModel', function() { @@ -132,8 +133,8 @@ FlashState.service('FlashStateModel', function() { speed: _.attempt(() => { if (_.isNumber(state.speed) && !_.isNaN(state.speed)) { - // Transform bytes to megabytes preserving only two decimal places - return Math.floor(state.speed / 1e+6 * 100) / 100; + // Preserve only two decimal places + return Math.floor(units.bytesToMegabytes(state.speed) * 100) / 100; } }) diff --git a/lib/gui/utils/byte-size/filters/gigabyte.js b/lib/gui/utils/byte-size/filters/gigabyte.js index ed857251..fd1551c3 100644 --- a/lib/gui/utils/byte-size/filters/gigabyte.js +++ b/lib/gui/utils/byte-size/filters/gigabyte.js @@ -16,6 +16,8 @@ 'use strict'; +const units = require('../../../../shared/units'); + module.exports = () => { /** @@ -29,8 +31,6 @@ module.exports = () => { * @example * {{ 7801405440 | gigabytes }} */ - return (bytes) => { - return bytes / 1000000000; - }; + return units.bytesToGigabytes; }; diff --git a/lib/shared/units.js b/lib/shared/units.js new file mode 100644 index 00000000..eeeae624 --- /dev/null +++ b/lib/shared/units.js @@ -0,0 +1,47 @@ +/* + * Copyright 2016 resin.io + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +/** + * @summary Convert bytes to gigabytes + * @function + * @public + * + * @param {Number} bytes - bytes + * @returns {Number} gigabytes + * + * @example + * const result = units.bytesToGigabytes(7801405440); + */ +exports.bytesToGigabytes = (bytes) => { + return bytes / 1e+9; +}; + +/** + * @summary Convert bytes to megabytes + * @function + * @public + * + * @param {Number} bytes - bytes + * @returns {Number} megabytes + * + * @example + * const result = units.bytesToMegabytes(7801405440); + */ +exports.bytesToMegabytes = (bytes) => { + return bytes / 1e+6; +}; diff --git a/tests/gui/utils/byte-size.spec.js b/tests/gui/utils/byte-size.spec.js index f20571ae..b7ec5fd7 100644 --- a/tests/gui/utils/byte-size.spec.js +++ b/tests/gui/utils/byte-size.spec.js @@ -3,6 +3,7 @@ const m = require('mochainon'); const angular = require('angular'); require('angular-mocks'); +const units = require('../../../lib/shared/units'); describe('Browser: ByteSize', function() { @@ -18,9 +19,8 @@ describe('Browser: ByteSize', function() { gigabyteFilter = _gigabyteFilter_; })); - it('should convert bytes to gigabytes', function() { - m.chai.expect(gigabyteFilter(7801405440)).to.equal(7.80140544); - m.chai.expect(gigabyteFilter(100000000)).to.equal(0.1); + it('should expose lib/shared/units.js bytesToGigabytes()', function() { + m.chai.expect(gigabyteFilter).to.equal(units.bytesToGigabytes); }); }); diff --git a/tests/shared/units.spec.js b/tests/shared/units.spec.js new file mode 100644 index 00000000..452f7a24 --- /dev/null +++ b/tests/shared/units.spec.js @@ -0,0 +1,38 @@ +/* + * Copyright 2016 resin.io + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const m = require('mochainon'); +const units = require('../../lib/shared/units'); + +describe('Shared: Units', function() { + + describe('.bytesToGigabytes()', function() { + + it('should convert bytes to gigabytes', function() { + m.chai.expect(units.bytesToGigabytes(7801405440)).to.equal(7.80140544); + m.chai.expect(units.bytesToGigabytes(100000000)).to.equal(0.1); + }); + + it('should convert bytes to megabytes', function() { + m.chai.expect(units.bytesToMegabytes(1.2e+7)).to.equal(12); + m.chai.expect(units.bytesToMegabytes(332000)).to.equal(0.332); + }); + + }); + +});