Sha256: b8e80938545343a6872b4580e20c350b7c2656fa6c84e774326e9a7e3942bc9f

Contents?: true

Size: 947 Bytes

Versions: 20

Compression:

Stored size: 947 Bytes

Contents

/**
 * Parse byte `size` string.
 *
 * @param {String} size
 * @return {Number}
 * @api public
 */

module.exports = function(size) {
  if ('number' == typeof size) return convert(size);
  var parts = size.match(/^(\d+(?:\.\d+)?) *(kb|mb|gb|tb)$/)
    , n = parseFloat(parts[1])
    , type = parts[2];

  var map = {
      kb: 1 << 10
    , mb: 1 << 20
    , gb: 1 << 30
    , tb: ((1 << 30) * 1024)
  };

  return map[type] * n;
};

/**
 * convert bytes into string.
 *
 * @param {Number} b - bytes to convert
 * @return {String}
 * @api public
 */

function convert (b) {
  var tb = ((1 << 30) * 1024), gb = 1 << 30, mb = 1 << 20, kb = 1 << 10, abs = Math.abs(b);
  if (abs >= tb) return (Math.round(b / tb * 100) / 100) + 'tb';
  if (abs >= gb) return (Math.round(b / gb * 100) / 100) + 'gb';
  if (abs >= mb) return (Math.round(b / mb * 100) / 100) + 'mb';
  if (abs >= kb) return (Math.round(b / kb * 100) / 100) + 'kb';
  return b + 'b';
}

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
hooch-0.4.2 jasmine/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
hooch-0.4.1 jasmine/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
hooch-0.4.0 jasmine/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
gulp_assets-1.0.0.pre.5 template/node_modules/gulp-livereload/node_modules/tiny-lr/node_modules/body-parser/node_modules/bytes/index.js
gulp_assets-1.0.0.pre.4 template/node_modules/gulp-livereload/node_modules/tiny-lr/node_modules/body-parser/node_modules/bytes/index.js
gulp_assets-1.0.0.pre.3 template/node_modules/gulp-livereload/node_modules/tiny-lr/node_modules/body-parser/node_modules/bytes/index.js
hooch-0.3.0 jasmine/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
hooch-0.2.1 jasmine/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
hooch-0.2.0 jasmine/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
hooch-0.1.0 jasmine/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
hooch-0.0.8 jasmine/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
hooch-0.0.7 jasmine/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
hooch-0.0.6 jasmine/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
entangled-0.0.16 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
entangled-0.0.15 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
entangled-0.0.14 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
entangled-0.0.13 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
entangled-0.0.12 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
entangled-0.0.11 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/bytes/index.js
entangled-0.0.10 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/bytes/index.js