From 17863dbbf0cf8de10f83e269f6e707b6c226f879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Sat, 4 Feb 2023 08:19:58 +0100 Subject: [PATCH] Add `space` option (#79) --- index.d.ts | 20 ++++++++++++++++++++ index.js | 9 ++++++--- index.test-d.ts | 1 + readme.md | 17 +++++++++++++++++ test.js | 11 +++++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index efcfd14..e7283cb 100644 --- a/index.d.ts +++ b/index.d.ts @@ -56,6 +56,7 @@ export interface Options { @default undefined + @example ``` import prettyBytes from 'pretty-bytes'; @@ -76,6 +77,7 @@ export interface Options { @default undefined + @example ``` import prettyBytes from 'pretty-bytes'; @@ -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; } /** diff --git a/index.js b/index.js index ca4fd3f..ae6f4d8 100644 --- a/index.js +++ b/index.js @@ -71,6 +71,7 @@ export default function prettyBytes(number, options) { options = { bits: false, binary: false, + space: true, ...options, }; @@ -78,8 +79,10 @@ export default function prettyBytes(number, options) { ? (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; @@ -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); @@ -115,5 +118,5 @@ export default function prettyBytes(number, options) { const unit = UNITS[exponent]; - return prefix + numberString + ' ' + unit; + return prefix + numberString + separator + unit; } diff --git a/index.test-d.ts b/index.test-d.ts index b98f89a..7f56f9c 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -7,3 +7,4 @@ expectType(prettyBytes(1337, {locale: 'de'})); expectType(prettyBytes(1337, {locale: true})); expectType(prettyBytes(1337, {bits: true})); expectType(prettyBytes(1337, {binary: true})); +expectType(prettyBytes(1337, {space: true})); diff --git a/readme.md b/readme.md index 133c592..e9cba34 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test.js b/test.js index f263d98..bd27450 100644 --- a/test.js +++ b/test.js @@ -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'); +});