pretty-bytes
Advanced tools
Comparing version 5.2.0 to 5.3.0
@@ -20,2 +20,16 @@ declare namespace prettyBytes { | ||
readonly locale?: boolean | string; | ||
/** | ||
Format the number as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte). This can be useful when, for example, referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate). | ||
@default false | ||
``` | ||
import prettyBytes = require('pretty-bytes'); | ||
prettyBytes(1337, {bits: true}); | ||
//=> '1.34 kbit' | ||
``` | ||
*/ | ||
readonly bits?: boolean; | ||
} | ||
@@ -22,0 +36,0 @@ } |
21
index.js
'use strict'; | ||
const UNITS = [ | ||
const BYTE_UNITS = [ | ||
'B', | ||
@@ -15,2 +15,14 @@ 'kB', | ||
const BIT_UNITS = [ | ||
'b', | ||
'kbit', | ||
'Mbit', | ||
'Gbit', | ||
'Tbit', | ||
'Pbit', | ||
'Ebit', | ||
'Zbit', | ||
'Ybit' | ||
]; | ||
/* | ||
@@ -38,6 +50,7 @@ Formats the given number using `Number#toLocaleString`. | ||
options = Object.assign({}, options); | ||
options = Object.assign({bits: false}, options); | ||
const UNITS = options.bits ? BIT_UNITS : BYTE_UNITS; | ||
if (options.signed && number === 0) { | ||
return ' 0 B'; | ||
return ' 0 ' + UNITS[0]; | ||
} | ||
@@ -54,3 +67,3 @@ | ||
const numberString = toLocaleString(number, options.locale); | ||
return prefix + numberString + ' B'; | ||
return prefix + numberString + ' ' + UNITS[0]; | ||
} | ||
@@ -57,0 +70,0 @@ |
{ | ||
"name": "pretty-bytes", | ||
"version": "5.2.0", | ||
"version": "5.3.0", | ||
"description": "Convert bytes to a human readable string: 1337 → 1.34 kB", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -29,2 +29,6 @@ # pretty-bytes [![Build Status](https://travis-ci.org/sindresorhus/pretty-bytes.svg?branch=master)](https://travis-ci.org/sindresorhus/pretty-bytes) | ||
// Display with units of bits | ||
prettyBytes(1337, {bits: true}); | ||
//=> '1.34 kbit' | ||
// Display file size differences | ||
@@ -61,3 +65,9 @@ prettyBytes(42, {signed: true}); | ||
##### bits | ||
Type: `boolean`<br> | ||
Default: `false` | ||
Format the number as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte). This can be useful when, for example, referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate). | ||
##### locale | ||
@@ -64,0 +74,0 @@ |
7584
114
90