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

Commits on Aug 30, 2020

  1. Copy the full SHA
    1333349 View commit details
  2. 5.4.1

    sindresorhus committed Aug 30, 2020
    Copy the full SHA
    f5c147c View commit details
Showing with 28 additions and 5 deletions.
  1. +15 −1 index.js
  2. +1 −1 package.json
  3. +12 −3 test.js
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -36,6 +36,18 @@ const BIT_UNITS = [
'Ybit'
];

const BIBIT_UNITS = [
'b',
'kibit',
'Mibit',
'Gibit',
'Tibit',
'Pibit',
'Eibit',
'Zibit',
'Yibit'
];

/*
Formats the given number using `Number#toLocaleString`.
- If locale is a string, the value is expected to be a locale-key (for example: `de`).
@@ -59,7 +71,9 @@ module.exports = (number, options) => {
}

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

if (options.signed && number === 0) {
return ' 0 ' + UNITS[0];
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.4.0",
"version": "5.4.1",
"description": "Convert bytes to a human readable string: 1337 → 1.34 kB",
"license": "MIT",
"repository": "sindresorhus/pretty-bytes",
15 changes: 12 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -93,8 +93,17 @@ test('binary option', t => {
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(1025, {binary: true}), '1 kiB');
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');
t.is(prettyBytes(1e16, {binary: true}), '8.88 PiB');
t.is(prettyBytes(1e30, {binary: true}), '827000 YiB');
});

test('bits and binary option', t => {
t.is(prettyBytes(0, {bits: true, binary: true}), '0 b');
t.is(prettyBytes(4, {bits: true, binary: true}), '4 b');
t.is(prettyBytes(10, {bits: true, binary: true}), '10 b');
t.is(prettyBytes(999, {bits: true, binary: true}), '999 b');
t.is(prettyBytes(1025, {bits: true, binary: true}), '1 kibit');
t.is(prettyBytes(1e6, {bits: true, binary: true}), '977 kibit');
});