Skip to content

Commit 17863db

Browse files
authored
Add space option (#79)
1 parent 6860747 commit 17863db

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

index.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export interface Options {
5656
5757
@default undefined
5858
59+
@example
5960
```
6061
import prettyBytes from 'pretty-bytes';
6162
@@ -76,6 +77,7 @@ export interface Options {
7677
7778
@default undefined
7879
80+
@example
7981
```
8082
import prettyBytes from 'pretty-bytes';
8183
@@ -88,6 +90,24 @@ export interface Options {
8890
```
8991
*/
9092
readonly maximumFractionDigits?: number;
93+
94+
/**
95+
Put a space between the number and unit.
96+
97+
@default true
98+
99+
@example
100+
```
101+
import prettyBytes from 'pretty-bytes';
102+
103+
prettyBytes(1920, {space: false});
104+
//=> '1.9kB'
105+
106+
prettyBytes(1920);
107+
//=> '1.92 kB'
108+
```
109+
*/
110+
readonly space?: boolean;
91111
}
92112

93113
/**

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,18 @@ export default function prettyBytes(number, options) {
7171
options = {
7272
bits: false,
7373
binary: false,
74+
space: true,
7475
...options,
7576
};
7677

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

82+
const separator = options.space ? ' ' : '';
83+
8184
if (options.signed && number === 0) {
82-
return ` 0 ${UNITS[0]}`;
85+
return ` 0${separator}${UNITS[0]}`;
8386
}
8487

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

102105
if (number < 1) {
103106
const numberString = toLocaleString(number, options.locale, localeOptions);
104-
return prefix + numberString + ' ' + UNITS[0];
107+
return prefix + numberString + separator + UNITS[0];
105108
}
106109

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

116119
const unit = UNITS[exponent];
117120

118-
return prefix + numberString + ' ' + unit;
121+
return prefix + numberString + separator + unit;
119122
}

index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ expectType<string>(prettyBytes(1337, {locale: 'de'}));
77
expectType<string>(prettyBytes(1337, {locale: true}));
88
expectType<string>(prettyBytes(1337, {bits: true}));
99
expectType<string>(prettyBytes(1337, {binary: true}));
10+
expectType<string>(prettyBytes(1337, {space: true}));

readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ prettyBytes(1920);
123123
//=> '1.92 kB'
124124
```
125125

126+
##### space
127+
128+
Type: `boolean`\
129+
Default: `true`
130+
131+
Put a space between the number and unit.
132+
133+
```js
134+
import prettyBytes from 'pretty-bytes';
135+
136+
prettyBytes(1920, {space: false});
137+
//=> '1.9kB'
138+
139+
prettyBytes(1920);
140+
//=> '1.92 kB'
141+
```
142+
126143
## Related
127144

128145
- [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module

test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,14 @@ test('fractional digits options', t => {
146146
t.is(prettyBytes(32_768, {minimumFractionDigits: 2, maximumFractionDigits: 3, binary: true}), '32.00 kiB');
147147
t.is(prettyBytes(65_536, {minimumFractionDigits: 1, maximumFractionDigits: 3, binary: true}), '64.0 kiB');
148148
});
149+
150+
test('space option', t => {
151+
t.is(prettyBytes(0), '0 B');
152+
t.is(prettyBytes(0, {space: false}), '0B');
153+
t.is(prettyBytes(999), '999 B');
154+
t.is(prettyBytes(999, {space: false}), '999B');
155+
t.is(prettyBytes(-13, {signed: true}), '-13 B');
156+
t.is(prettyBytes(-13, {signed: true, space: false}), '-13B');
157+
t.is(prettyBytes(42, {signed: true}), '+42 B');
158+
t.is(prettyBytes(42, {signed: true, space: false}), '+42B');
159+
});

0 commit comments

Comments
 (0)