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.3.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.4.0
Choose a head ref
  • 5 commits
  • 8 files changed
  • 2 contributors

Commits on Nov 7, 2019

  1. Readme tweaks

    sindresorhus authored Nov 7, 2019
    Copy the full SHA
    00037f6 View commit details

Commits on Jan 11, 2020

  1. Add note about unit localization

    Closes #56
    sindresorhus committed Jan 11, 2020
    Copy the full SHA
    4a2eb76 View commit details

Commits on Aug 29, 2020

  1. Add binary option (#60)

    Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
    nmoinvaz and sindresorhus authored Aug 29, 2020
    Copy the full SHA
    4ef01f4 View commit details
  2. Meta tweaks

    sindresorhus committed Aug 29, 2020
    Copy the full SHA
    bf02326 View commit details
  3. 5.4.0

    sindresorhus committed Aug 29, 2020
    Copy the full SHA
    1000ebe View commit details
Showing with 100 additions and 51 deletions.
  1. +2 −0 .travis.yml
  2. +19 −2 index.d.ts
  3. +16 −4 index.js
  4. +1 −0 index.test-d.ts
  5. +1 −1 license
  6. +4 −4 package.json
  7. +16 −15 readme.md
  8. +41 −25 test.js
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: node_js
node_js:
- '14'
- '12'
- '10'
- '8'
- '6'
21 changes: 19 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -20,9 +20,9 @@ declare namespace prettyBytes {

/**
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');
@@ -31,6 +31,23 @@ declare namespace prettyBytes {
```
*/
readonly bits?: boolean;

/**
Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_Prefix). This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes.
@default false
```
import prettyBytes = require('pretty-bytes');
prettyBytes(1000, {binary: true});
//=> '1000 bit'
prettyBytes(1024, {binary: true});
//=> '1 kiB'
```
*/
readonly binary?: boolean;
}
}

20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -12,6 +12,18 @@ const BYTE_UNITS = [
'YB'
];

const BIBYTE_UNITS = [
'B',
'kiB',
'MiB',
'GiB',
'TiB',
'PiB',
'EiB',
'ZiB',
'YiB'
];

const BIT_UNITS = [
'b',
'kbit',
@@ -46,8 +58,8 @@ module.exports = (number, options) => {
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
}

options = Object.assign({bits: false}, options);
const UNITS = options.bits ? BIT_UNITS : BYTE_UNITS;
options = Object.assign({bits: false, binary: false}, options);
const UNITS = options.bits ? (options.binary ? BIBYTE_UNITS : BIT_UNITS) : BYTE_UNITS;

if (options.signed && number === 0) {
return ' 0 ' + UNITS[0];
@@ -65,9 +77,9 @@ module.exports = (number, options) => {
return prefix + numberString + ' ' + UNITS[0];
}

const exponent = Math.min(Math.floor(Math.log10(number) / 3), UNITS.length - 1);
const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3), UNITS.length - 1);
// eslint-disable-next-line unicorn/prefer-exponentiation-operator
number = Number((number / Math.pow(1000, exponent)).toPrecision(3));
number = Number((number / Math.pow(options.binary ? 1024 : 1000, exponent)).toPrecision(3));
const numberString = toLocaleString(number, options.locale);

const unit = UNITS[exponent];
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -8,3 +8,4 @@ expectType<string>(prettyBytes(42, {signed: true}));
expectType<string>(prettyBytes(1337, {locale: 'de'}));
expectType<string>(prettyBytes(1337, {locale: true}));
expectType<string>(prettyBytes(1337, {bits: true}));
expectType<string>(prettyBytes(1337, {binary: true}));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"name": "pretty-bytes",
"version": "5.3.0",
"version": "5.4.0",
"description": "Convert bytes to a human readable string: 1337 → 1.34 kB",
"license": "MIT",
"repository": "sindresorhus/pretty-bytes",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && NODE_ICU_DATA=node_modules/full-icu ava && tsd"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
@@ -37,7 +38,6 @@
],
"devDependencies": {
"ava": "^1.4.1",
"full-icu": "^1.2.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
31 changes: 16 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pretty-bytes [![Build Status](https://travis-ci.org/sindresorhus/pretty-bytes.svg?branch=master)](https://travis-ci.org/sindresorhus/pretty-bytes)
# pretty-bytes [![Build Status](https://travis-ci.com/sindresorhus/pretty-bytes.svg?branch=master)](https://travis-ci.com/github/sindresorhus/pretty-bytes)

> Convert bytes to a human readable string: `1337``1.34 kB`
@@ -7,14 +7,12 @@ Useful for displaying file sizes for humans.
*Note that it uses base-10 (e.g. kilobyte).
[Read about the difference between kilobyte and kibibyte.](https://web.archive.org/web/20150324153922/https://pacoup.com/2009/05/26/kb-kb-kib-whats-up-with-that/)*


## Install

```
$ npm install pretty-bytes
```


## Usage

```js
@@ -39,10 +37,9 @@ prettyBytes(1337, {locale: 'de'});
//=> '1,34 kB'
```


## API

### prettyBytes(number, [options])
### prettyBytes(number, options?)

#### number

@@ -56,34 +53,38 @@ Type: `object`

##### signed

Type: `boolean`<br>
Type: `boolean`\
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>
Type: `boolean`\
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).

##### binary

Type: `boolean`\
Default: `false`

Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_Prefix). This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes.

##### locale

Type: `boolean` `string`<br>
Type: `boolean | string`\
Default: `false` *(No localization)*

**Important:** Only the number and decimal separator are localized. The unit title is not and will not be localized.

- If `true`: Localize the output using the system/browser locale.
- If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)

**Note:** Localization should generally work in browsers. Node.js needs to be [built](https://github.com/nodejs/node/wiki/Intl) with `full-icu` or `system-icu`. Alternatively, the [`full-icu`](https://github.com/unicode-org/full-icu-npm) module can be used to provide support at runtime.

**Note:** Localization should generally work in browsers. Node.js needs to be [built](https://github.com/nodejs/node/wiki/Intl) with `full-icu` or `system-icu`. Alternatively, the [`full-icu`](https://github.com/unicode-org/full-icu-npm) module can be used to provide support at runtime. [Node.js 13](https://nodejs.org/en/blog/release/v13.0.0/) and later ships with ICU by default.

## Related

- [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
- [pretty-ms](https://github.com/sindresorhus/pretty-ms) - Convert milliseconds to a human readable string
66 changes: 41 additions & 25 deletions test.js
Original file line number Diff line number Diff line change
@@ -33,35 +33,39 @@ test('supports negative number', t => {
});

test('locale option', t => {
t.is(prettyBytes(-0.4, {locale: 'de'}), '-0,4 B');
t.is(prettyBytes(0.4, {locale: 'de'}), '0,4 B');
t.is(prettyBytes(1001, {locale: 'de'}), '1 kB');
t.is(prettyBytes(10.1, {locale: 'de'}), '10,1 B');
t.is(prettyBytes(1e30, {locale: 'de'}), '1.000.000 YB');
if (Number(process.version[0]) >= 14) {
t.is(prettyBytes(-0.4, {locale: 'de'}), '-0,4 B');
t.is(prettyBytes(0.4, {locale: 'de'}), '0,4 B');
t.is(prettyBytes(1001, {locale: 'de'}), '1 kB');
t.is(prettyBytes(10.1, {locale: 'de'}), '10,1 B');
t.is(prettyBytes(1e30, {locale: 'de'}), '1.000.000 YB');

t.is(prettyBytes(-0.4, {locale: 'en'}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: 'en'}), '0.4 B');
t.is(prettyBytes(1001, {locale: 'en'}), '1 kB');
t.is(prettyBytes(10.1, {locale: 'en'}), '10.1 B');
t.is(prettyBytes(1e30, {locale: 'en'}), '1,000,000 YB');
t.is(prettyBytes(-0.4, {locale: 'en'}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: 'en'}), '0.4 B');
t.is(prettyBytes(1001, {locale: 'en'}), '1 kB');
t.is(prettyBytes(10.1, {locale: 'en'}), '10.1 B');
t.is(prettyBytes(1e30, {locale: 'en'}), '1,000,000 YB');

t.is(prettyBytes(-0.4, {locale: true}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: true}), '0.4 B');
t.is(prettyBytes(1001, {locale: true}), '1 kB');
t.is(prettyBytes(10.1, {locale: true}), '10.1 B');
t.is(prettyBytes(1e30, {locale: true}), '1,000,000 YB');
t.is(prettyBytes(-0.4, {locale: true}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: true}), '0.4 B');
t.is(prettyBytes(1001, {locale: true}), '1 kB');
t.is(prettyBytes(10.1, {locale: true}), '10.1 B');
t.is(prettyBytes(1e30, {locale: true}), '1,000,000 YB');

t.is(prettyBytes(-0.4, {locale: false}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: false}), '0.4 B');
t.is(prettyBytes(1001, {locale: false}), '1 kB');
t.is(prettyBytes(10.1, {locale: false}), '10.1 B');
t.is(prettyBytes(1e30, {locale: false}), '1000000 YB');
t.is(prettyBytes(-0.4, {locale: false}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: false}), '0.4 B');
t.is(prettyBytes(1001, {locale: false}), '1 kB');
t.is(prettyBytes(10.1, {locale: false}), '10.1 B');
t.is(prettyBytes(1e30, {locale: false}), '1000000 YB');

t.is(prettyBytes(-0.4, {locale: undefined}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: undefined}), '0.4 B');
t.is(prettyBytes(1001, {locale: undefined}), '1 kB');
t.is(prettyBytes(10.1, {locale: undefined}), '10.1 B');
t.is(prettyBytes(1e30, {locale: undefined}), '1000000 YB');
t.is(prettyBytes(-0.4, {locale: undefined}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: undefined}), '0.4 B');
t.is(prettyBytes(1001, {locale: undefined}), '1 kB');
t.is(prettyBytes(10.1, {locale: undefined}), '10.1 B');
t.is(prettyBytes(1e30, {locale: undefined}), '1000000 YB');
} else {
t.pass();
}
});

test('signed option', t => {
@@ -82,3 +86,15 @@ test('bits option', t => {
t.is(prettyBytes(1e16, {bits: true}), '10 Pbit');
t.is(prettyBytes(1e30, {bits: true}), '1000000 Ybit');
});

test('binary option', t => {
t.is(prettyBytes(0, {binary: true}), '0 B');
t.is(prettyBytes(4, {binary: true}), '4 B');
t.is(prettyBytes(10, {binary: true}), '10 B');
t.is(prettyBytes(10.1, {binary: true}), '10.1 B');
t.is(prettyBytes(999, {binary: true}), '999 B');
t.is(prettyBytes(1025, {binary: true}), '1 kB');
t.is(prettyBytes(1001, {binary: true}), '1000 B');
t.is(prettyBytes(1e16, {binary: true}), '8.88 PB');
t.is(prettyBytes(1e30, {binary: true}), '827000 YB');
});