Skip to content

Commit

Permalink
Added digits and fixed options.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmoinvaz committed Aug 30, 2020
1 parent 1000ebe commit 22b92c7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
28 changes: 28 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ declare namespace prettyBytes {
```
*/
readonly binary?: boolean;

/**
Number of significant digits to display when calculating bytes or bits.
@default 3
```
import prettyBytes = require('pretty-bytes');
prettyBytes(1019, {digits: 3});
//=> '1.02 B'
```
*/
readonly digits?: number;

/**
Format the number using fixed-point notation, rounded up.
@default false
```
import prettyBytes = require('pretty-bytes');
prettyBytes(1019, {digits: 3, fixed: true});
//=> '1.019 B'
```
*/
readonly fixed?: boolean;
}
}

Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ module.exports = (number, options) => {

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(options.binary ? 1024 : 1000, exponent)).toPrecision(3));
const numberString = toLocaleString(number, options.locale);
number /= Math.pow(options.binary ? 1024 : 1000, exponent);
const digits = options.digits || 3;
number = options.fixed ? number.toFixed(digits) : number.toPrecision(digits);

const numberString = toLocaleString(Number(number), options.locale);

const unit = UNITS[exponent];

Expand Down
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ 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.

##### digits

Type: `number`\
Default: `3`

Number of significant digits to display when calculating bytes or bits.

##### fixed

Type: `boolean`\
Default: `false`

Format the number using fixed-point notation, rounded up.

##### locale

Type: `boolean | string`\
Expand Down
18 changes: 18 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,21 @@ test('binary option', t => {
t.is(prettyBytes(1e16, {binary: true}), '8.88 PB');
t.is(prettyBytes(1e30, {binary: true}), '827000 YB');
});

test('digits option', t => {
t.is(prettyBytes(1001), '1 kB');
t.is(prettyBytes(1111), '1.11 kB');
t.is(prettyBytes(1900, {digits: 1}), '2 kB');
t.is(prettyBytes(1111, {digits: 2}), '1.1 kB');
t.is(prettyBytes(1019, {digits: 3}), '1.02 kB');
t.is(prettyBytes(1001, {digits: 4}), '1.001 kB');
});

test('fixed option', t => {
t.is(prettyBytes(1001, {fixed: true}), '1.001 kB');
t.is(prettyBytes(1111, {fixed: true}), '1.111 kB');
t.is(prettyBytes(1900, {digits: 1, fixed: true}), '1.9 kB');
t.is(prettyBytes(1111, {digits: 2, fixed: true}), '1.11 kB');
t.is(prettyBytes(1019, {digits: 3, fixed: true}), '1.019 kB');
t.is(prettyBytes(1001, {digits: 4, fixed: true}), '1.001 kB');
});

0 comments on commit 22b92c7

Please sign in to comment.