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 space option #79

Merged
merged 14 commits into from Feb 4, 2023
42 changes: 42 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,46 @@ export interface Options {
```
*/
readonly maximumFractionDigits?: number;

/**
The number of spaces to put between the number and unit.

If it is not set, the default value is to display 1 space between the number and unit.

If it is set to a negative number, the number of spaces between the number and unit will be 0.
klevente marked this conversation as resolved.
Show resolved Hide resolved

@default 1

@example
```
import prettyBytes from 'pretty-bytes';
prettyBytes(1920, {spaces: 0});
//=> '1.9kB'
klevente marked this conversation as resolved.
Show resolved Hide resolved

prettyBytes(1920);
//=> '1.92 kB'
```
*/
readonly spaces?: number;
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

/**
Display an uppercase K for units with the "kilo" prefix.
klevente marked this conversation as resolved.
Show resolved Hide resolved

If not set, the default behavior is to display a lowercase "k" for units with the "kilo" prefix.

@default undefined

@example
```
import prettyBytes from 'pretty-bytes';
prettyBytes(1920, {uppercaseK: true});
//=> '1.9 KB'

prettyBytes(1920);
//=> '1.92 kB'
```
*/
readonly uppercaseKilo?: boolean;
}

/**
Expand Down
13 changes: 9 additions & 4 deletions index.js
Expand Up @@ -74,12 +74,14 @@ export default function prettyBytes(number, options) {
...options,
};

const separator = ' '.repeat(options.spaces === undefined ? 1 : Math.max(options.spaces, 0));

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]}`;
return ` 0${separator}${UNITS[0]}`;
}

const isNegative = number < 0;
Expand All @@ -101,7 +103,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 @@ -113,7 +115,10 @@ export default function prettyBytes(number, options) {

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

const unit = UNITS[exponent];
let unit = UNITS[exponent];
if (options.uppercaseKilo && exponent === 1) {
unit = 'K' + unit.slice(1);
}

return prefix + numberString + ' ' + unit;
return prefix + numberString + separator + unit;
}
2 changes: 2 additions & 0 deletions index.test-d.ts
Expand Up @@ -7,3 +7,5 @@ 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, {spaces: 0}));
expectType<string>(prettyBytes(1337, {uppercaseKilo: true}));
40 changes: 40 additions & 0 deletions readme.md
Expand Up @@ -123,6 +123,46 @@ prettyBytes(1920);
//=> '1.92 kB'
```

##### spaces

Type: `number`\
Default: `1`

The number of spaces to put between the number and unit.

If it is not set, the default value is to display 1 space between the number and unit.

If it is set to a negative number, the number of spaces between the number and unit will be 0.

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

prettyBytes(1920, {spaces: 0});
//=> '1.9kB'

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

##### uppercaseKilo

Type: `boolean`\
Default: `false`

Display an uppercase K for units with the "kilo" prefix.

If not set, the default behavior is to display a lowercase "k" for units with the "kilo" prefix.

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

prettyBytes(1920, {uppercaseK: true});
//=> '1.9 KB'

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

## Related

- [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module
Expand Down
22 changes: 22 additions & 0 deletions test.js
Expand Up @@ -146,3 +146,25 @@ 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('spaces option', t => {
t.is(prettyBytes(0), '0 B');
t.is(prettyBytes(0, {spaces: 0}), '0B');
t.is(prettyBytes(999, {spaces: 0}), '999B');
t.is(prettyBytes(999, {spaces: 2}), '999 B');
t.is(prettyBytes(-13, {signed: true, spaces: 0}), '-13B');
t.is(prettyBytes(-13, {signed: true, spaces: 3}), '-13 B');
t.is(prettyBytes(42, {signed: true, spaces: 0}), '+42B');
t.is(prettyBytes(42, {signed: true, spaces: 2}), '+42 B');
});

test('uppercaseKilo option', t => {
t.is(prettyBytes(1001), '1 kB');
t.is(prettyBytes(1025, {binary: true}), '1 kiB');
t.is(prettyBytes(1001, {bits: true}), '1 kbit');
t.is(prettyBytes(1025, {bits: true, binary: true}), '1 kibit');
t.is(prettyBytes(1001, {uppercaseKilo: true}), '1 KB');
t.is(prettyBytes(1025, {binary: true, uppercaseKilo: true}), '1 KiB');
t.is(prettyBytes(1001, {bits: true, uppercaseKilo: true}), '1 Kbit');
t.is(prettyBytes(1025, {bits: true, binary: true, uppercaseKilo: true}), '1 Kibit');
});