index.js
function format(value, options) {
  if (!Number.isFinite(value)) {
    return null;
  }

  var mag = Math.abs(value);
  var thousandsSeparator = (options && options.thousandsSeparator) || '';
  var unitSeparator = (options && options.unitSeparator) || '';
  var decimalPlaces = (options && options.decimalPlaces !== undefined) ? options.decimalPlaces : 2;
  var fixedDecimals = Boolean(options && options.fixedDecimals);
  var unit = (options && options.unit) || '';

  if (!unit || !map[unit.toLowerCase()]) {
    if (mag >= map.pb) {
      unit = 'PB';
    } else if (mag >= map.tb) {
      unit = 'TB';
    } else if (mag >= map.gb) {
      unit = 'GB';
    } else if (mag >= map.mb) {
      unit = 'MB';
    } else if (mag >= map.kb) {
      unit = 'KB';
    } else {
      unit = 'B';
    }
  }

  var val = value / map[unit.toLowerCase()];
  var str = val.toFixed(decimalPlaces);

  if (!fixedDecimals) {
    str = str.replace(formatDecimalsRegExp, '$1');
  }

  if (thousandsSeparator) {
    str = str.replace(formatThousandsRegExp, thousandsSeparator);
  }

  return str + unitSeparator + unit;
}
function format(value, options) {
  if (!Number.isFinite(value)) {
    return null;
  }

  var mag = Math.abs(value);
  var thousandsSeparator = (options && options.thousandsSeparator) || '';
  var unitSeparator = (options && options.unitSeparator) || '';
  var decimalPlaces = (options && options.decimalPlaces !== undefined) ? options.decimalPlaces : 2;
  var fixedDecimals = Boolean(options && options.fixedDecimals);
  var unit = (options && options.unit) || '';

  if (!unit || !map[unit.toLowerCase()]) {
    if (mag >= map.pb) {
      unit = 'PB';
    } else if (mag >= map.tb) {
      unit = 'TB';
    } else if (mag >= map.gb) {
      unit = 'GB';
    } else if (mag >= map.mb) {
      unit = 'MB';
    } else if (mag >= map.kb) {
      unit = 'KB';
    } else {
      unit = 'B';
    }
  }

  var val = value / map[unit.toLowerCase()];
  var str = val.toFixed(decimalPlaces);

  if (!fixedDecimals) {
    str = str.replace(formatDecimalsRegExp, '$1');
  }

  if (thousandsSeparator) {
    str = str.replace(formatThousandsRegExp, thousandsSeparator);
  }

  return str + unitSeparator + unit;
}
function format ( value , options ) { if ( ! Number . isFinite ( value ) ) { return null ; } var mag = Math . abs ( value ) ; var thousandsSeparator = ( options && options . thousandsSeparator ) || '' ; var unitSeparator = ( options && options . unitSeparator ) || '' ; var decimalPlaces = ( options && options . decimalPlaces !== undefined ) ? options . decimalPlaces : 2 ; var fixedDecimals = Boolean ( options && options . fixedDecimals ) ; var unit = ( options && options . unit ) || '' ; if ( ! unit || ! map [ unit . toLowerCase ( ) ] ) { if ( mag >= map . pb ) { unit = 'PB' ; } else if ( mag >= map . tb ) { unit = 'TB' ; } else if ( mag >= map . gb ) { unit = 'GB' ; } else if ( mag >= map . mb ) { unit = 'MB' ; } else if ( mag >= map . kb ) { unit = 'KB' ; } else { unit = 'B' ; } } var val = value / map [ unit . toLowerCase ( ) ] ; var str = val . toFixed ( decimalPlaces ) ; if ( ! fixedDecimals ) { str = str . replace ( formatDecimalsRegExp , '$1' ) ; } if ( thousandsSeparator ) { str = str . replace ( formatThousandsRegExp , thousandsSeparator ) ; } return str + unitSeparator + unit ; }
Format the given value in bytes into a string.

If the value is negative, it is kept as such. If it is a float,
it is rounded.

@param {number} value
@param {object} [options]
@param {number} [options.decimalPlaces=2]
@param {number} [options.fixedDecimals=false]
@param {string} [options.thousandsSeparator=]
@param {string} [options.unit=]
@param {string} [options.unitSeparator=]

@returns {string|null}
@public
Format the given value in bytes into a string .