Skip to content

Commit dd2a777

Browse files
committed
Meta tweaks
1 parent 19e7a3c commit dd2a777

File tree

7 files changed

+85
-74
lines changed

7 files changed

+85
-74
lines changed

.github/funding.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

index.d.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ export interface Options {
1212
- If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
1313
- If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
1414
15-
__Note:__ Localization should generally work in browsers. Node.js needs to be [built](https://github.com/nodejs/node/wiki/Intl) with `full-icu` or `system-icu`. Alternatively, the [`full-icu`](https://github.com/unicode-org/full-icu-npm) module can be used to provide support at runtime.
16-
1715
@default false
1816
*/
1917
readonly locale?: boolean | string | readonly string[];
@@ -25,7 +23,7 @@ export interface Options {
2523
2624
@example
2725
```
28-
import { prettyBytes } from 'pretty-bytes';
26+
import prettyBytes from 'pretty-bytes';
2927
3028
prettyBytes(1337, {bits: true});
3129
//=> '1.34 kbit'
@@ -40,7 +38,7 @@ export interface Options {
4038
4139
@example
4240
```
43-
import { prettyBytes } from 'pretty-bytes';
41+
import prettyBytes from 'pretty-bytes';
4442
4543
prettyBytes(1000, {binary: true});
4644
//=> '1000 bit'
@@ -59,7 +57,7 @@ export interface Options {
5957
@default undefined
6058
6159
```
62-
import { prettyBytes } from 'pretty-bytes';
60+
import prettyBytes from 'pretty-bytes';
6361
6462
// Show the number with at least 3 fractional digits
6563
prettyBytes(1900, {minimumFractionDigits: 3});
@@ -79,7 +77,7 @@ export interface Options {
7977
@default undefined
8078
8179
```
82-
import { prettyBytes } from 'pretty-bytes';
80+
import prettyBytes from 'pretty-bytes';
8381
8482
// Show the number with at most 1 fractional digit
8583
prettyBytes(1920, {maximumFractionDigits: 1});
@@ -99,7 +97,7 @@ Convert bytes to a human readable string: `1337` → `1.34 kB`.
9997
10098
@example
10199
```
102-
import { prettyBytes } from 'pretty-bytes';
100+
import prettyBytes from 'pretty-bytes';
103101
104102
prettyBytes(1337);
105103
//=> '1.34 kB'
@@ -116,7 +114,7 @@ prettyBytes(1337, {locale: 'de'});
116114
//=> '1,34 kB'
117115
```
118116
*/
119-
export function prettyBytes(
117+
export default function prettyBytes(
120118
number: number,
121119
options?: Options
122120
): string;

index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ const toLocaleString = (number, locale, options) => {
6363
return result;
6464
};
6565

66-
export const prettyBytes = (number, options) => {
66+
export default function prettyBytes(number, options) {
6767
if (!Number.isFinite(number)) {
6868
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
6969
}
7070

71-
options = {bits: false, binary: false, ...options};
71+
options = {
72+
bits: false,
73+
binary: false,
74+
...options,
75+
};
7276

7377
const UNITS = options.bits
7478
? (options.binary ? BIBIT_UNITS : BIT_UNITS)
@@ -101,8 +105,7 @@ export const prettyBytes = (number, options) => {
101105
}
102106

103107
const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3), UNITS.length - 1);
104-
// eslint-disable-next-line prefer-exponentiation-operator
105-
number /= Math.pow(options.binary ? 1024 : 1000, exponent);
108+
number /= (options.binary ? 1024 : 1000) ** exponent;
106109

107110
if (!localeOptions) {
108111
number = number.toPrecision(3);
@@ -113,4 +116,4 @@ export const prettyBytes = (number, options) => {
113116
const unit = UNITS[exponent];
114117

115118
return prefix + numberString + ' ' + unit;
116-
};
119+
}

index.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expectType} from 'tsd';
2-
import {prettyBytes} from '.';
2+
import prettyBytes from './index.js';
33

44
expectType<string>(prettyBytes(1337));
55
expectType<string>(prettyBytes(42, {signed: true}));

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"exports": "./index.js",
1515
"types": "./index.d.ts",
1616
"engines": {
17-
"node": ">=12"
17+
"node": "^14.13.1 || >=16.0.0"
1818
},
1919
"scripts": {
2020
"test": "xo && ava && tsd"
@@ -42,6 +42,6 @@
4242
"devDependencies": {
4343
"ava": "^4.0.1",
4444
"tsd": "^0.19.1",
45-
"xo": "^0.47.0"
45+
"xo": "^0.48.0"
4646
}
4747
}

readme.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ Useful for displaying file sizes for humans.
99

1010
## Install
1111

12-
```
13-
$ npm install pretty-bytes
12+
```sh
13+
npm install pretty-bytes
1414
```
1515

1616
## Usage
1717

1818
```js
19-
import { prettyBytes } from 'pretty-bytes';
19+
import prettyBytes from 'pretty-bytes';
2020

2121
prettyBytes(1337);
2222
//=> '1.34 kB'
@@ -83,8 +83,6 @@ Default: `false` *(No localization)*
8383
- If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
8484
- If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
8585

86-
**Note:** Localization should generally work in browsers. Node.js needs to be [built](https://github.com/nodejs/node/wiki/Intl) with `full-icu` or `system-icu`. Alternatively, the [`full-icu`](https://github.com/unicode-org/full-icu-npm) module can be used to provide support at runtime. [Node.js 13](https://nodejs.org/en/blog/release/v13.0.0/) and later ships with ICU by default.
87-
8886
##### minimumFractionDigits
8987

9088
Type: `number`\
@@ -95,7 +93,7 @@ The minimum number of fraction digits to display.
9593
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
9694

9795
```js
98-
import { prettyBytes } from 'pretty-bytes';
96+
import prettyBytes from 'pretty-bytes';
9997

10098
// Show the number with at least 3 fractional digits
10199
prettyBytes(1900, {minimumFractionDigits: 3});
@@ -115,7 +113,7 @@ The maximum number of fraction digits to display.
115113
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
116114

117115
```js
118-
import { prettyBytes } from 'pretty-bytes';
116+
import prettyBytes from 'pretty-bytes';
119117

120118
// Show the number with at most 1 fractional digit
121119
prettyBytes(1920, {maximumFractionDigits: 1});

test.js

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1-
import process from 'node:process';
21
import test from 'ava';
3-
import {prettyBytes} from './index.js';
2+
import prettyBytes from './index.js';
43

54
test('throws on invalid input', t => {
6-
t.throws(() => prettyBytes(''));
7-
t.throws(() => prettyBytes('1'));
8-
t.throws(() => prettyBytes(Number.NaN));
9-
t.throws(() => prettyBytes(true));
10-
t.throws(() => prettyBytes(Number.POSITIVE_INFINITY));
11-
t.throws(() => prettyBytes(Number.NEGATIVE_INFINITY));
12-
t.throws(() => prettyBytes(null));
5+
t.throws(() => {
6+
prettyBytes('');
7+
});
8+
9+
t.throws(() => {
10+
prettyBytes('1');
11+
});
12+
13+
t.throws(() => {
14+
prettyBytes(Number.NaN);
15+
});
16+
17+
t.throws(() => {
18+
prettyBytes(true);
19+
});
20+
21+
t.throws(() => {
22+
prettyBytes(Number.POSITIVE_INFINITY);
23+
});
24+
25+
t.throws(() => {
26+
prettyBytes(Number.NEGATIVE_INFINITY);
27+
});
28+
29+
t.throws(() => {
30+
prettyBytes(null);
31+
});
1332
});
1433

1534
test('converts bytes to human readable strings', t => {
@@ -33,45 +52,41 @@ test('supports negative number', t => {
3352
});
3453

3554
test('locale option', t => {
36-
if (Number(process.version[0]) >= 14) {
37-
t.is(prettyBytes(-0.4, {locale: 'de'}), '-0,4 B');
38-
t.is(prettyBytes(0.4, {locale: 'de'}), '0,4 B');
39-
t.is(prettyBytes(1001, {locale: 'de'}), '1 kB');
40-
t.is(prettyBytes(10.1, {locale: 'de'}), '10,1 B');
41-
t.is(prettyBytes(1e30, {locale: 'de'}), '1.000.000 YB');
42-
43-
t.is(prettyBytes(-0.4, {locale: 'en'}), '-0.4 B');
44-
t.is(prettyBytes(0.4, {locale: 'en'}), '0.4 B');
45-
t.is(prettyBytes(1001, {locale: 'en'}), '1 kB');
46-
t.is(prettyBytes(10.1, {locale: 'en'}), '10.1 B');
47-
t.is(prettyBytes(1e30, {locale: 'en'}), '1,000,000 YB');
48-
49-
t.is(prettyBytes(-0.4, {locale: ['unknown', 'de', 'en']}), '-0,4 B');
50-
t.is(prettyBytes(0.4, {locale: ['unknown', 'de', 'en']}), '0,4 B');
51-
t.is(prettyBytes(1001, {locale: ['unknown', 'de', 'en']}), '1 kB');
52-
t.is(prettyBytes(10.1, {locale: ['unknown', 'de', 'en']}), '10,1 B');
53-
t.is(prettyBytes(1e30, {locale: ['unknown', 'de', 'en']}), '1.000.000 YB');
54-
55-
t.is(prettyBytes(-0.4, {locale: true}), '-0.4 B');
56-
t.is(prettyBytes(0.4, {locale: true}), '0.4 B');
57-
t.is(prettyBytes(1001, {locale: true}), '1 kB');
58-
t.is(prettyBytes(10.1, {locale: true}), '10.1 B');
59-
t.is(prettyBytes(1e30, {locale: true}), '1,000,000 YB');
60-
61-
t.is(prettyBytes(-0.4, {locale: false}), '-0.4 B');
62-
t.is(prettyBytes(0.4, {locale: false}), '0.4 B');
63-
t.is(prettyBytes(1001, {locale: false}), '1 kB');
64-
t.is(prettyBytes(10.1, {locale: false}), '10.1 B');
65-
t.is(prettyBytes(1e30, {locale: false}), '1000000 YB');
66-
67-
t.is(prettyBytes(-0.4, {locale: undefined}), '-0.4 B');
68-
t.is(prettyBytes(0.4, {locale: undefined}), '0.4 B');
69-
t.is(prettyBytes(1001, {locale: undefined}), '1 kB');
70-
t.is(prettyBytes(10.1, {locale: undefined}), '10.1 B');
71-
t.is(prettyBytes(1e30, {locale: undefined}), '1000000 YB');
72-
} else {
73-
t.pass();
74-
}
55+
t.is(prettyBytes(-0.4, {locale: 'de'}), '-0,4 B');
56+
t.is(prettyBytes(0.4, {locale: 'de'}), '0,4 B');
57+
t.is(prettyBytes(1001, {locale: 'de'}), '1 kB');
58+
t.is(prettyBytes(10.1, {locale: 'de'}), '10,1 B');
59+
t.is(prettyBytes(1e30, {locale: 'de'}), '1.000.000 YB');
60+
61+
t.is(prettyBytes(-0.4, {locale: 'en'}), '-0.4 B');
62+
t.is(prettyBytes(0.4, {locale: 'en'}), '0.4 B');
63+
t.is(prettyBytes(1001, {locale: 'en'}), '1 kB');
64+
t.is(prettyBytes(10.1, {locale: 'en'}), '10.1 B');
65+
t.is(prettyBytes(1e30, {locale: 'en'}), '1,000,000 YB');
66+
67+
t.is(prettyBytes(-0.4, {locale: ['unknown', 'de', 'en']}), '-0,4 B');
68+
t.is(prettyBytes(0.4, {locale: ['unknown', 'de', 'en']}), '0,4 B');
69+
t.is(prettyBytes(1001, {locale: ['unknown', 'de', 'en']}), '1 kB');
70+
t.is(prettyBytes(10.1, {locale: ['unknown', 'de', 'en']}), '10,1 B');
71+
t.is(prettyBytes(1e30, {locale: ['unknown', 'de', 'en']}), '1.000.000 YB');
72+
73+
t.is(prettyBytes(-0.4, {locale: true}), '-0.4 B');
74+
t.is(prettyBytes(0.4, {locale: true}), '0.4 B');
75+
t.is(prettyBytes(1001, {locale: true}), '1 kB');
76+
t.is(prettyBytes(10.1, {locale: true}), '10.1 B');
77+
t.is(prettyBytes(1e30, {locale: true}), '1,000,000 YB');
78+
79+
t.is(prettyBytes(-0.4, {locale: false}), '-0.4 B');
80+
t.is(prettyBytes(0.4, {locale: false}), '0.4 B');
81+
t.is(prettyBytes(1001, {locale: false}), '1 kB');
82+
t.is(prettyBytes(10.1, {locale: false}), '10.1 B');
83+
t.is(prettyBytes(1e30, {locale: false}), '1000000 YB');
84+
85+
t.is(prettyBytes(-0.4, {locale: undefined}), '-0.4 B');
86+
t.is(prettyBytes(0.4, {locale: undefined}), '0.4 B');
87+
t.is(prettyBytes(1001, {locale: undefined}), '1 kB');
88+
t.is(prettyBytes(10.1, {locale: undefined}), '10.1 B');
89+
t.is(prettyBytes(1e30, {locale: undefined}), '1000000 YB');
7590
});
7691

7792
test('signed option', t => {

0 commit comments

Comments
 (0)