-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.js
46 lines (33 loc) · 1.49 KB
/
format.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
var util = require('util');
/**
* This function will convert any given number of bytes into a human readable string
* @param {[int]} bytes Any number of bytes
* @param {bool} abbr True if you want to use abbreviations (default) false for full name
* @return {[string]} A human readable string of byte size
*/
exports.bytes = function (bytes, abbr) {
abbr = typeof abbr === 'undefined' ? true : false;
var sizes = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
if (!abbr) sizes = [
'bytes', 'kilobytes', 'megabytes', 'gigabytes',
'terabytes', 'petabytes', 'exabytes', 'zettabytes', 'yottabytes'
];
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
if (i > sizes.length - 1) i = sizes.length - 1; // Larger than yottabytes? yikes.
return util.format('%s %s', (bytes / Math.pow(1024, i)).toFixed(2), sizes[i]);
};
/**
* This function will convert any given frequenty (in megahertz) to a human readable format
* @return {[megahertz]} Any frequency in megahertz
*/
exports.frequency = function (megahertz, abbr) {
abbr = typeof abbr === 'undefined' ? true : false;
var frequencies = ['MHz', 'GHz', 'THz', 'PHz'];
if (!abbr) frequencies = [
'megahertz', 'gigahertz', 'terahertz', 'petahertz'
];
var i = parseInt(Math.floor(Math.log(megahertz) / Math.log(1000)));
if (i > frequencies.length - 1) i = frequencies.length - 1;
return util.format('%s %s', (megahertz / Math.pow(1000, i)).toFixed(2), frequencies[i]);
};