etcher/lib/shared/units.js
Juan Cruz Viotti 6c8bc117ab chore: revise ESLint built-in configuration (#1149)
There are a lot of new rules since the last time I revised the ESLint
rules documentation.

I've updated the main `.eslintrc.yml` to include some newer additions,
plus I added another ESLint configuration file inside `tests`, so we can
add some stricted rules to the production code while relaxing them for
the test suite (due to the fact that Mocha is not very ES6 friendly and
Angular tests require a bit of dark magic to setup).

This is a summary of the most important changes:

- Disallow "magic numbers"

These should now be extracted to constants, which forces us to think of
a good name for them, and thus make the code more self-documenting (I
had to Google up the meaning of some existing magic numbers, so I guess
this will be great for readability purposes).

- Require consistent `return` statements

Some functions relied on JavaScript relaxed casting mechanism to work,
which now have explicit return values. This flag also helped me detect
some promises that were not being returned, and therefore risked not
being caught by the exception handlers in case of errors.

- Disallow redefining function arguments

Immutability makes functions easier to reason about.

- Enforce JavaScript string templates instead of string concatenation

We were heavily mixing boths across the codebase.

There are some extra rules that I tweaked, however most of codebase
changes in this commit are related to the rules mentioned above.

Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
2017-03-07 23:46:44 -04:00

96 lines
1.9 KiB
JavaScript

/*
* 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 Gigabyte to byte ratio
* @constant
* @private
* @type {Number}
*
* @description
* 1 GB = 1e+9 B
*/
const GIGABYTE_TO_BYTE_RATIO = 1e+9;
/**
* @summary Megabyte to byte ratio
* @constant
* @private
* @type {Number}
*
* @description
* 1 MB = 1e+6 B
*/
const MEGABYTE_TO_BYTE_RATIO = 1e+6;
/**
* @summary Milliseconds in a day
* @constant
* @private
* @type {Number}
*
* @description
* From 24 * 60 * 60 * 1000
*/
const MILLISECONDS_IN_A_DAY = 86400000;
/**
* @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 / GIGABYTE_TO_BYTE_RATIO;
};
/**
* @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 / MEGABYTE_TO_BYTE_RATIO;
};
/**
* @summary Convert days to milliseconds
* @function
* @public
*
* @param {Number} days - days
* @returns {Number} milliseconds
*
* @example
* const result = units.daysToMilliseconds(2);
*/
exports.daysToMilliseconds = (days) => {
return days * MILLISECONDS_IN_A_DAY;
};