arnaucube
arnaucube/comunicationLeapPublic Self-hosted repository
comunicationLeap/node_modules/bytes/index.js
node_modules/bytes/index.jsRaw
 1
 2/**
 3 * Parse byte `size` string.
 4 *
 5 * @param {String} size
 6 * @return {Number}
 7 * @api public
 8 */
 9
10module.exports = function(size) {
11  if ('number' == typeof size) return convert(size);
12  var parts = size.match(/^(\d+(?:\.\d+)?) *(kb|mb|gb)$/)
13    , n = parseFloat(parts[1])
14    , type = parts[2];
15
16  var map = {
17      kb: 1 << 10
18    , mb: 1 << 20
19    , gb: 1 << 30
20  };
21
22  return map[type] * n;
23};
24
25/**
26 * convert bytes into string.
27 *
28 * @param {Number} b - bytes to convert
29 * @return {String}
30 * @api public
31 */
32
33function convert (b) {
34  var gb = 1 << 30, mb = 1 << 20, kb = 1 << 10;
35  if (b >= gb) return (Math.round(b / gb * 100) / 100) + 'gb';
36  if (b >= mb) return (Math.round(b / mb * 100) / 100) + 'mb';
37  if (b >= kb) return (Math.round(b / kb * 100) / 100) + 'kb';
38  return b + 'b';
39}