refactor: move all byte-size conversion logic to lib/shared/units.js (#1021)

This commit extracts the byte-related conversions from the `byte-size`
AngularJS module and the `FlashStateModel` to a re-usable generic
CommonJS module at `lib/shared/units.js`, than can also be used by the
CLI.

Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
This commit is contained in:
Juan Cruz Viotti 2017-01-13 16:11:28 -04:00 committed by GitHub
parent c552494480
commit aea4403a16
5 changed files with 94 additions and 8 deletions

View File

@ -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;
}
})

View File

@ -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;
};

47
lib/shared/units.js Normal file
View File

@ -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;
};

View File

@ -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);
});
});

View File

@ -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);
});
});
});