Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add space option (#79)
  • Loading branch information
klevente committed Feb 4, 2023
1 parent 6860747 commit 17863db
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
20 changes: 20 additions & 0 deletions index.d.ts
Expand Up @@ -56,6 +56,7 @@ export interface Options {
@default undefined
@example
```
import prettyBytes from 'pretty-bytes';
Expand All @@ -76,6 +77,7 @@ export interface Options {
@default undefined
@example
```
import prettyBytes from 'pretty-bytes';
Expand All @@ -88,6 +90,24 @@ export interface Options {
```
*/
readonly maximumFractionDigits?: number;

/**
Put a space between the number and unit.
@default true
@example
```
import prettyBytes from 'pretty-bytes';
prettyBytes(1920, {space: false});
//=> '1.9kB'
prettyBytes(1920);
//=> '1.92 kB'
```
*/
readonly space?: boolean;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions index.js
Expand Up @@ -71,15 +71,18 @@ export default function prettyBytes(number, options) {
options = {
bits: false,
binary: false,
space: true,
...options,
};

const UNITS = options.bits
? (options.binary ? BIBIT_UNITS : BIT_UNITS)
: (options.binary ? BIBYTE_UNITS : BYTE_UNITS);

const separator = options.space ? ' ' : '';

if (options.signed && number === 0) {
return ` 0 ${UNITS[0]}`;
return ` 0${separator}${UNITS[0]}`;
}

const isNegative = number < 0;
Expand All @@ -101,7 +104,7 @@ export default function prettyBytes(number, options) {

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

const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3), UNITS.length - 1);
Expand All @@ -115,5 +118,5 @@ export default function prettyBytes(number, options) {

const unit = UNITS[exponent];

return prefix + numberString + ' ' + unit;
return prefix + numberString + separator + unit;
}
1 change: 1 addition & 0 deletions index.test-d.ts
Expand Up @@ -7,3 +7,4 @@ expectType<string>(prettyBytes(1337, {locale: 'de'}));
expectType<string>(prettyBytes(1337, {locale: true}));
expectType<string>(prettyBytes(1337, {bits: true}));
expectType<string>(prettyBytes(1337, {binary: true}));
expectType<string>(prettyBytes(1337, {space: true}));
17 changes: 17 additions & 0 deletions readme.md
Expand Up @@ -123,6 +123,23 @@ prettyBytes(1920);
//=> '1.92 kB'
```

##### space

Type: `boolean`\
Default: `true`

Put a space between the number and unit.

```js
import prettyBytes from 'pretty-bytes';

prettyBytes(1920, {space: false});
//=> '1.9kB'

prettyBytes(1920);
//=> '1.92 kB'
```

## Related

- [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module
Expand Down
11 changes: 11 additions & 0 deletions test.js
Expand Up @@ -146,3 +146,14 @@ test('fractional digits options', t => {
t.is(prettyBytes(32_768, {minimumFractionDigits: 2, maximumFractionDigits: 3, binary: true}), '32.00 kiB');
t.is(prettyBytes(65_536, {minimumFractionDigits: 1, maximumFractionDigits: 3, binary: true}), '64.0 kiB');
});

test('space option', t => {
t.is(prettyBytes(0), '0 B');
t.is(prettyBytes(0, {space: false}), '0B');
t.is(prettyBytes(999), '999 B');
t.is(prettyBytes(999, {space: false}), '999B');
t.is(prettyBytes(-13, {signed: true}), '-13 B');
t.is(prettyBytes(-13, {signed: true, space: false}), '-13B');
t.is(prettyBytes(42, {signed: true}), '+42 B');
t.is(prettyBytes(42, {signed: true, space: false}), '+42B');
});

0 comments on commit 17863db

Please sign in to comment.