Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/pretty-bytes
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v5.2.0
Choose a base ref
...
head repository: sindresorhus/pretty-bytes
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v5.3.0
Choose a head ref
  • 3 commits
  • 7 files changed
  • 2 contributors

Commits on May 28, 2019

  1. Create funding.yml

    sindresorhus authored May 28, 2019
    Copy the full SHA
    cdc6ea7 View commit details

Commits on Jul 26, 2019

  1. Add bits option (#53)

    Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
    Monty Anderson and sindresorhus committed Jul 26, 2019
    Copy the full SHA
    cb93e5b View commit details
  2. 5.3.0

    sindresorhus committed Jul 26, 2019
    Copy the full SHA
    50f9fdb View commit details
Showing with 59 additions and 5 deletions.
  1. +3 −0 .github/funding.yml
  2. +14 −0 index.d.ts
  3. +17 −4 index.js
  4. +1 −0 index.test-d.ts
  5. +1 −1 package.json
  6. +10 −0 readme.md
  7. +13 −0 test.js
3 changes: 3 additions & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github: sindresorhus
open_collective: sindresorhus
custom: https://sindresorhus.com/donate
14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -17,6 +17,20 @@ declare namespace prettyBytes {
@default false
*/
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;
}
}

21 changes: 17 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const UNITS = [
const BYTE_UNITS = [
'B',
'kB',
'MB',
@@ -12,6 +12,18 @@ const UNITS = [
'YB'
];

const BIT_UNITS = [
'b',
'kbit',
'Mbit',
'Gbit',
'Tbit',
'Pbit',
'Ebit',
'Zbit',
'Ybit'
];

/*
Formats the given number using `Number#toLocaleString`.
- If locale is a string, the value is expected to be a locale-key (for example: `de`).
@@ -34,10 +46,11 @@ module.exports = (number, options) => {
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
}

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];
}

const isNegative = number < 0;
@@ -49,7 +62,7 @@ module.exports = (number, options) => {

if (number < 1) {
const numberString = toLocaleString(number, options.locale);
return prefix + numberString + ' B';
return prefix + numberString + ' ' + UNITS[0];
}

const exponent = Math.min(Math.floor(Math.log10(number) / 3), UNITS.length - 1);
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -7,3 +7,4 @@ expectType<string>(prettyBytes(1337));
expectType<string>(prettyBytes(42, {signed: true}));
expectType<string>(prettyBytes(1337, {locale: 'de'}));
expectType<string>(prettyBytes(1337, {locale: true}));
expectType<string>(prettyBytes(1337, {bits: true}));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pretty-bytes",
"version": "5.2.0",
"version": "5.3.0",
"description": "Convert bytes to a human readable string: 1337 → 1.34 kB",
"license": "MIT",
"repository": "sindresorhus/pretty-bytes",
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -26,6 +26,10 @@ prettyBytes(1337);
prettyBytes(100);
//=> '100 B'

// Display with units of bits
prettyBytes(1337, {bits: true});
//=> '1.34 kbit'

// Display file size differences
prettyBytes(42, {signed: true});
//=> '+42 B'
@@ -57,6 +61,12 @@ Default: `false`

Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.

##### 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

13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -69,3 +69,16 @@ test('signed option', t => {
t.is(prettyBytes(-13, {signed: true}), '-13 B');
t.is(prettyBytes(0, {signed: true}), ' 0 B');
});

test('bits option', t => {
t.is(prettyBytes(0, {bits: true}), '0 b');
t.is(prettyBytes(0.4, {bits: true}), '0.4 b');
t.is(prettyBytes(0.7, {bits: true}), '0.7 b');
t.is(prettyBytes(10, {bits: true}), '10 b');
t.is(prettyBytes(10.1, {bits: true}), '10.1 b');
t.is(prettyBytes(999, {bits: true}), '999 b');
t.is(prettyBytes(1001, {bits: true}), '1 kbit');
t.is(prettyBytes(1001, {bits: true}), '1 kbit');
t.is(prettyBytes(1e16, {bits: true}), '10 Pbit');
t.is(prettyBytes(1e30, {bits: true}), '1000000 Ybit');
});