Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b64cee5

Browse files
committed
Fix truncation behavior with fraction digits options
Fixes #71
1 parent b637640 commit b64cee5

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

index.d.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ export type Options = {
5252
/**
5353
The minimum number of fraction digits to display.
5454
55-
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
56-
5755
@default undefined
5856
57+
If neither `minimumFractionDigits` nor `maximumFractionDigits` is set, the default behavior is to round to 3 significant digits.
58+
59+
Note: When `minimumFractionDigits` or `maximumFractionDigits` is specified, values are truncated instead of rounded to provide more intuitive results for file sizes.
60+
5961
@example
6062
```
6163
import prettyBytes from 'pretty-bytes';
@@ -73,10 +75,12 @@ export type Options = {
7375
/**
7476
The maximum number of fraction digits to display.
7577
76-
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
77-
7878
@default undefined
7979
80+
If neither `minimumFractionDigits` nor `maximumFractionDigits` is set, the default behavior is to round to 3 significant digits.
81+
82+
Note: When `minimumFractionDigits` or `maximumFractionDigits` is specified, values are truncated instead of rounded to provide more intuitive results for file sizes.
83+
8084
@example
8185
```
8286
import prettyBytes from 'pretty-bytes';

index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,11 @@ export default function prettyBytes(number, options) {
121121
number = -number;
122122
}
123123

124-
let localeOptions;
125-
126-
if (options.minimumFractionDigits !== undefined) {
127-
localeOptions = {minimumFractionDigits: options.minimumFractionDigits};
128-
}
129-
130-
if (options.maximumFractionDigits !== undefined) {
131-
localeOptions = {maximumFractionDigits: options.maximumFractionDigits, ...localeOptions};
132-
}
124+
const localeOptions = (options.minimumFractionDigits !== undefined || options.maximumFractionDigits !== undefined) ? {
125+
...(options.minimumFractionDigits !== undefined && {minimumFractionDigits: options.minimumFractionDigits}),
126+
...(options.maximumFractionDigits !== undefined && {maximumFractionDigits: options.maximumFractionDigits}),
127+
roundingMode: 'trunc',
128+
} : undefined;
133129

134130
if (number < 1) {
135131
const numberString = toLocaleString(number, options.locale, localeOptions);

readme.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary
7777
Type: `boolean | string`\
7878
Default: `false` *(No localization)*
7979

80-
**Important:** Only the number and decimal separator are localized. The unit title is not and will not be localized.
80+
> [!IMPORTANT]
81+
> Only the number and decimal separator are localized. The unit title is not and will not be localized.
8182
8283
- If `true`: Localize the output using the system/browser locale.
8384
- If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
@@ -90,7 +91,10 @@ Default: `undefined`
9091

9192
The minimum number of fraction digits to display.
9293

93-
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
94+
If neither `minimumFractionDigits` nor `maximumFractionDigits` is set, the default behavior is to round to 3 significant digits.
95+
96+
> [!NOTE]
97+
> When `minimumFractionDigits` or `maximumFractionDigits` is specified, values are truncated instead of rounded to provide more intuitive results for file sizes.
9498
9599
```js
96100
import prettyBytes from 'pretty-bytes';
@@ -110,7 +114,10 @@ Default: `undefined`
110114

111115
The maximum number of fraction digits to display.
112116

113-
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
117+
If neither `minimumFractionDigits` nor `maximumFractionDigits` is set, the default behavior is to round to 3 significant digits.
118+
119+
> [!NOTE]
120+
> When `minimumFractionDigits` or `maximumFractionDigits` is specified, values are truncated instead of rounded to provide more intuitive results for file sizes.
114121
115122
```js
116123
import prettyBytes from 'pretty-bytes';

test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,14 @@ test('fractional digits options', t => {
198198
t.is(prettyBytes(1000n, {minimumFractionDigits: 1, maximumFractionDigits: 3}), '1.0 kB');
199199
t.is(prettyBytes(3942, {minimumFractionDigits: 1, maximumFractionDigits: 2}), '3.94 kB');
200200
t.is(prettyBytes(3942n, {minimumFractionDigits: 1, maximumFractionDigits: 2}), '3.94 kB');
201-
t.is.skip(prettyBytes(59_952_784, {maximumFractionDigits: 1}), '59.9 MB'); // eslint-disable-line ava/no-skip-assert
202-
t.is.skip(prettyBytes(59_952_784, {minimumFractionDigits: 1, maximumFractionDigits: 1}), '59.9 MB'); // eslint-disable-line ava/no-skip-assert
201+
t.is(prettyBytes(59_952_784, {maximumFractionDigits: 1}), '59.9 MB');
202+
t.is(prettyBytes(59_952_784n, {maximumFractionDigits: 1}), '59.9 MB');
203+
t.is(prettyBytes(59_952_784, {minimumFractionDigits: 1, maximumFractionDigits: 1}), '59.9 MB');
204+
t.is(prettyBytes(59_952_784n, {minimumFractionDigits: 1, maximumFractionDigits: 1}), '59.9 MB');
203205
t.is(prettyBytes(4001, {maximumFractionDigits: 3, binary: true}), '3.907 KiB');
204206
t.is(prettyBytes(4001n, {maximumFractionDigits: 3, binary: true}), '3.907 KiB');
205-
t.is(prettyBytes(18_717, {maximumFractionDigits: 2, binary: true}), '18.28 KiB');
206-
t.is(prettyBytes(18_717n, {maximumFractionDigits: 2, binary: true}), '18.28 KiB');
207+
t.is(prettyBytes(18_717, {maximumFractionDigits: 2, binary: true}), '18.27 KiB');
208+
t.is(prettyBytes(18_717n, {maximumFractionDigits: 2, binary: true}), '18.27 KiB');
207209
t.is(prettyBytes(18_717, {maximumFractionDigits: 4, binary: true}), '18.2783 KiB');
208210
t.is(prettyBytes(18_717n, {maximumFractionDigits: 4, binary: true}), '18.2783 KiB');
209211
t.is(prettyBytes(32_768, {minimumFractionDigits: 2, maximumFractionDigits: 3, binary: true}), '32.00 KiB');

0 commit comments

Comments
 (0)