Skip to content

Commit 19e7a3c

Browse files
authored
Move to ESM (#73)
1 parent 000ccd1 commit 19e7a3c

File tree

7 files changed

+105
-112
lines changed

7 files changed

+105
-112
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 16
1314
- 14
1415
- 12
15-
- 10
16-
- 8
17-
- 6
1816
steps:
1917
- uses: actions/checkout@v2
20-
- uses: actions/setup-node@v1
18+
- uses: actions/setup-node@v2
2119
with:
2220
node-version: ${{ matrix.node-version }}
2321
- run: npm install

index.d.ts

Lines changed: 70 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,95 @@
1-
declare namespace prettyBytes {
2-
interface Options {
3-
/**
4-
Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.
1+
export interface Options {
2+
/**
3+
Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.
54
6-
@default false
7-
*/
8-
readonly signed?: boolean;
5+
@default false
6+
*/
7+
readonly signed?: boolean;
98

10-
/**
11-
- If `false`: Output won't be localized.
12-
- If `true`: Localize the output using the system/browser locale.
13-
- If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
14-
- If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
9+
/**
10+
- If `false`: Output won't be localized.
11+
- If `true`: Localize the output using the system/browser locale.
12+
- If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
13+
- If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
1514
16-
__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.
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.
1716
18-
@default false
19-
*/
20-
readonly locale?: boolean | string | readonly string[];
17+
@default false
18+
*/
19+
readonly locale?: boolean | string | readonly string[];
2120

22-
/**
23-
Format the number as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte). This can be useful when, for example, referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate).
21+
/**
22+
Format the number as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte). This can be useful when, for example, referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate).
2423
25-
@default false
24+
@default false
2625
27-
@example
28-
```
29-
import prettyBytes = require('pretty-bytes');
26+
@example
27+
```
28+
import { prettyBytes } from 'pretty-bytes';
3029
31-
prettyBytes(1337, {bits: true});
32-
//=> '1.34 kbit'
33-
```
34-
*/
35-
readonly bits?: boolean;
30+
prettyBytes(1337, {bits: true});
31+
//=> '1.34 kbit'
32+
```
33+
*/
34+
readonly bits?: boolean;
3635

37-
/**
38-
Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_prefix). This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes.
36+
/**
37+
Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_prefix). This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes.
3938
40-
@default false
39+
@default false
4140
42-
@example
43-
```
44-
import prettyBytes = require('pretty-bytes');
41+
@example
42+
```
43+
import { prettyBytes } from 'pretty-bytes';
4544
46-
prettyBytes(1000, {binary: true});
47-
//=> '1000 bit'
45+
prettyBytes(1000, {binary: true});
46+
//=> '1000 bit'
4847
49-
prettyBytes(1024, {binary: true});
50-
//=> '1 kiB'
51-
```
52-
*/
53-
readonly binary?: boolean;
48+
prettyBytes(1024, {binary: true});
49+
//=> '1 kiB'
50+
```
51+
*/
52+
readonly binary?: boolean;
5453

55-
/**
56-
The minimum number of fraction digits to display.
54+
/**
55+
The minimum number of fraction digits to display.
5756
58-
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
57+
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
5958
60-
@default undefined
59+
@default undefined
6160
62-
```
63-
import prettyBytes = require('pretty-bytes');
61+
```
62+
import { prettyBytes } from 'pretty-bytes';
6463
65-
// Show the number with at least 3 fractional digits
66-
prettyBytes(1900, {minimumFractionDigits: 3});
67-
//=> '1.900 kB'
64+
// Show the number with at least 3 fractional digits
65+
prettyBytes(1900, {minimumFractionDigits: 3});
66+
//=> '1.900 kB'
6867
69-
prettyBytes(1900);
70-
//=> '1.9 kB'
71-
```
72-
*/
73-
readonly minimumFractionDigits?: number;
68+
prettyBytes(1900);
69+
//=> '1.9 kB'
70+
```
71+
*/
72+
readonly minimumFractionDigits?: number;
7473

74+
/**
75+
The maximum number of fraction digits to display.
7576
76-
/**
77-
The maximum number of fraction digits to display.
77+
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
7878
79-
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
79+
@default undefined
8080
81-
@default undefined
81+
```
82+
import { prettyBytes } from 'pretty-bytes';
8283
83-
```
84-
import prettyBytes = require('pretty-bytes');
84+
// Show the number with at most 1 fractional digit
85+
prettyBytes(1920, {maximumFractionDigits: 1});
86+
//=> '1.9 kB'
8587
86-
// Show the number with at most 1 fractional digit
87-
prettyBytes(1920, {maximumFractionDigits: 1});
88-
//=> '1.9 kB'
89-
90-
prettyBytes(1920);
91-
//=> '1.92 kB'
92-
```
93-
*/
94-
readonly maximumFractionDigits?: number;
95-
}
88+
prettyBytes(1920);
89+
//=> '1.92 kB'
90+
```
91+
*/
92+
readonly maximumFractionDigits?: number;
9693
}
9794

9895
/**
@@ -102,7 +99,7 @@ Convert bytes to a human readable string: `1337` → `1.34 kB`.
10299
103100
@example
104101
```
105-
import prettyBytes = require('pretty-bytes');
102+
import { prettyBytes } from 'pretty-bytes';
106103
107104
prettyBytes(1337);
108105
//=> '1.34 kB'
@@ -119,9 +116,7 @@ prettyBytes(1337, {locale: 'de'});
119116
//=> '1,34 kB'
120117
```
121118
*/
122-
declare function prettyBytes(
119+
export function prettyBytes(
123120
number: number,
124-
options?: prettyBytes.Options
121+
options?: Options
125122
): string;
126-
127-
export = prettyBytes;

index.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
const BYTE_UNITS = [
42
'B',
53
'kB',
@@ -9,7 +7,7 @@ const BYTE_UNITS = [
97
'PB',
108
'EB',
119
'ZB',
12-
'YB'
10+
'YB',
1311
];
1412

1513
const BIBYTE_UNITS = [
@@ -21,7 +19,7 @@ const BIBYTE_UNITS = [
2119
'PiB',
2220
'EiB',
2321
'ZiB',
24-
'YiB'
22+
'YiB',
2523
];
2624

2725
const BIT_UNITS = [
@@ -33,7 +31,7 @@ const BIT_UNITS = [
3331
'Pbit',
3432
'Ebit',
3533
'Zbit',
36-
'Ybit'
34+
'Ybit',
3735
];
3836

3937
const BIBIT_UNITS = [
@@ -45,7 +43,7 @@ const BIBIT_UNITS = [
4543
'Pibit',
4644
'Eibit',
4745
'Zibit',
48-
'Yibit'
46+
'Yibit',
4947
];
5048

5149
/*
@@ -65,16 +63,16 @@ const toLocaleString = (number, locale, options) => {
6563
return result;
6664
};
6765

68-
module.exports = (number, options) => {
66+
export const prettyBytes = (number, options) => {
6967
if (!Number.isFinite(number)) {
7068
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
7169
}
7270

73-
options = Object.assign({bits: false, binary: false}, options);
71+
options = {bits: false, binary: false, ...options};
7472

75-
const UNITS = options.bits ?
76-
(options.binary ? BIBIT_UNITS : BIT_UNITS) :
77-
(options.binary ? BIBYTE_UNITS : BYTE_UNITS);
73+
const UNITS = options.bits
74+
? (options.binary ? BIBIT_UNITS : BIT_UNITS)
75+
: (options.binary ? BIBYTE_UNITS : BYTE_UNITS);
7876

7977
if (options.signed && number === 0) {
8078
return ` 0 ${UNITS[0]}`;
@@ -94,7 +92,7 @@ module.exports = (number, options) => {
9492
}
9593

9694
if (options.maximumFractionDigits !== undefined) {
97-
localeOptions = Object.assign({maximumFractionDigits: options.maximumFractionDigits}, localeOptions);
95+
localeOptions = {maximumFractionDigits: options.maximumFractionDigits, ...localeOptions};
9896
}
9997

10098
if (number < 1) {
@@ -103,7 +101,7 @@ module.exports = (number, options) => {
103101
}
104102

105103
const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3), UNITS.length - 1);
106-
// eslint-disable-next-line unicorn/prefer-exponentiation-operator
104+
// eslint-disable-next-line prefer-exponentiation-operator
107105
number /= Math.pow(options.binary ? 1024 : 1000, exponent);
108106

109107
if (!localeOptions) {

index.test-d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {expectType} from 'tsd';
2-
import prettyBytes = require('.');
3-
4-
const options: prettyBytes.Options = {};
2+
import {prettyBytes} from '.';
53

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

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
"email": "[email protected]",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
15+
"types": "./index.d.ts",
1316
"engines": {
14-
"node": ">=6"
17+
"node": ">=12"
1518
},
1619
"scripts": {
1720
"test": "xo && ava && tsd"
@@ -37,8 +40,8 @@
3740
"localized"
3841
],
3942
"devDependencies": {
40-
"ava": "^1.4.1",
41-
"tsd": "^0.7.2",
42-
"xo": "^0.24.0"
43+
"ava": "^4.0.1",
44+
"tsd": "^0.19.1",
45+
"xo": "^0.47.0"
4346
}
4447
}

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $ npm install pretty-bytes
1616
## Usage
1717

1818
```js
19-
const prettyBytes = require('pretty-bytes');
19+
import { prettyBytes } from 'pretty-bytes';
2020

2121
prettyBytes(1337);
2222
//=> '1.34 kB'
@@ -95,7 +95,7 @@ The minimum number of fraction digits to display.
9595
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
9696

9797
```js
98-
const prettyBytes = require('pretty-bytes');
98+
import { prettyBytes } from 'pretty-bytes';
9999

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

117117
```js
118-
const prettyBytes = require('pretty-bytes');
118+
import { prettyBytes } from 'pretty-bytes';
119119

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

test.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import process from 'node:process';
12
import test from 'ava';
2-
import prettyBytes from '.';
3+
import {prettyBytes} from './index.js';
34

45
test('throws on invalid input', t => {
56
t.throws(() => prettyBytes(''));
67
t.throws(() => prettyBytes('1'));
7-
t.throws(() => prettyBytes(NaN));
8+
t.throws(() => prettyBytes(Number.NaN));
89
t.throws(() => prettyBytes(true));
9-
t.throws(() => prettyBytes(Infinity));
10-
t.throws(() => prettyBytes(-Infinity));
10+
t.throws(() => prettyBytes(Number.POSITIVE_INFINITY));
11+
t.throws(() => prettyBytes(Number.NEGATIVE_INFINITY));
1112
t.throws(() => prettyBytes(null));
1213
});
1314

@@ -122,11 +123,11 @@ test('fractional digits options', t => {
122123
t.is(prettyBytes(1001, {maximumFractionDigits: 3}), '1.001 kB');
123124
t.is(prettyBytes(1000, {minimumFractionDigits: 1, maximumFractionDigits: 3}), '1.0 kB');
124125
t.is(prettyBytes(3942, {minimumFractionDigits: 1, maximumFractionDigits: 2}), '3.94 kB');
125-
t.is.skip(prettyBytes(59952784, {maximumFractionDigits: 1}), '59.9 MB'); // eslint-disable-line ava/no-skip-assert
126-
t.is.skip(prettyBytes(59952784, {minimumFractionDigits: 1, maximumFractionDigits: 1}), '59.9 MB'); // eslint-disable-line ava/no-skip-assert
126+
t.is.skip(prettyBytes(59_952_784, {maximumFractionDigits: 1}), '59.9 MB'); // eslint-disable-line ava/no-skip-assert
127+
t.is.skip(prettyBytes(59_952_784, {minimumFractionDigits: 1, maximumFractionDigits: 1}), '59.9 MB'); // eslint-disable-line ava/no-skip-assert
127128
t.is(prettyBytes(4001, {maximumFractionDigits: 3, binary: true}), '3.907 kiB');
128-
t.is(prettyBytes(18717, {maximumFractionDigits: 2, binary: true}), '18.28 kiB');
129-
t.is(prettyBytes(18717, {maximumFractionDigits: 4, binary: true}), '18.2783 kiB');
130-
t.is(prettyBytes(32768, {minimumFractionDigits: 2, maximumFractionDigits: 3, binary: true}), '32.00 kiB');
131-
t.is(prettyBytes(65536, {minimumFractionDigits: 1, maximumFractionDigits: 3, binary: true}), '64.0 kiB');
129+
t.is(prettyBytes(18_717, {maximumFractionDigits: 2, binary: true}), '18.28 kiB');
130+
t.is(prettyBytes(18_717, {maximumFractionDigits: 4, binary: true}), '18.2783 kiB');
131+
t.is(prettyBytes(32_768, {minimumFractionDigits: 2, maximumFractionDigits: 3, binary: true}), '32.00 kiB');
132+
t.is(prettyBytes(65_536, {minimumFractionDigits: 1, maximumFractionDigits: 3, binary: true}), '64.0 kiB');
132133
});

0 commit comments

Comments
 (0)