Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bits option #53

Merged
merged 10 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add tests and docs
  • Loading branch information
montyanderson committed Jul 10, 2019
commit e7f9925fea186aceb1407741dea5e46936901af4
12 changes: 12 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ declare namespace prettyBytes {
@default false
*/
readonly locale?: boolean | string;

/**
Display proper units for bits.

@default false
*/

readonly bits?: boolean;
}
}

Expand All @@ -42,6 +50,10 @@ prettyBytes(42, {signed: true});
// Localized output using German locale
prettyBytes(1337, {locale: 'de'});
//=> '1,34 kB'

// Display with units of bits
prettyBytes(1337, {bits: true});
// => '1,34 kbit'
```
*/
declare function prettyBytes(
Expand Down
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ module.exports = (number, options) => {
}

options = Object.assign({}, options);
montyanderson marked this conversation as resolved.
Show resolved Hide resolved
const UNITS = options.bits === true ? BIT_UNITS : BYTE_UNITS;
montyanderson marked this conversation as resolved.
Show resolved Hide resolved

if (options.signed && number === 0) {
return ' 0 B';
return ' 0 ' + UNITS[0];
}

const UNITS = options.bits !== true ? BYTE_UNITS : BIT_UNITS;

const isNegative = number < 0;
const prefix = isNegative ? '-' : (options.signed ? '+' : '');

Expand All @@ -63,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);
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}));
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ prettyBytes(42, {signed: true});
// Localized output using German locale
prettyBytes(1337, {locale: 'de'});
//=> '1,34 kB'

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


Expand All @@ -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`

Treat input as bits and use proper units respectively.
montyanderson marked this conversation as resolved.
Show resolved Hide resolved

##### locale

Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});