Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 13, 2022
1 parent 19e7a3c commit dd2a777
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 74 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

14 changes: 6 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export interface Options {
- If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
- If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
__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.
@default false
*/
readonly locale?: boolean | string | readonly string[];
Expand All @@ -25,7 +23,7 @@ export interface Options {
@example
```
import { prettyBytes } from 'pretty-bytes';
import prettyBytes from 'pretty-bytes';
prettyBytes(1337, {bits: true});
//=> '1.34 kbit'
Expand All @@ -40,7 +38,7 @@ export interface Options {
@example
```
import { prettyBytes } from 'pretty-bytes';
import prettyBytes from 'pretty-bytes';
prettyBytes(1000, {binary: true});
//=> '1000 bit'
Expand All @@ -59,7 +57,7 @@ export interface Options {
@default undefined
```
import { prettyBytes } from 'pretty-bytes';
import prettyBytes from 'pretty-bytes';
// Show the number with at least 3 fractional digits
prettyBytes(1900, {minimumFractionDigits: 3});
Expand All @@ -79,7 +77,7 @@ export interface Options {
@default undefined
```
import { prettyBytes } from 'pretty-bytes';
import prettyBytes from 'pretty-bytes';
// Show the number with at most 1 fractional digit
prettyBytes(1920, {maximumFractionDigits: 1});
Expand All @@ -99,7 +97,7 @@ Convert bytes to a human readable string: `1337` → `1.34 kB`.
@example
```
import { prettyBytes } from 'pretty-bytes';
import prettyBytes from 'pretty-bytes';
prettyBytes(1337);
//=> '1.34 kB'
Expand All @@ -116,7 +114,7 @@ prettyBytes(1337, {locale: 'de'});
//=> '1,34 kB'
```
*/
export function prettyBytes(
export default function prettyBytes(
number: number,
options?: Options
): string;
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ const toLocaleString = (number, locale, options) => {
return result;
};

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

options = {bits: false, binary: false, ...options};
options = {
bits: false,
binary: false,
...options,
};

const UNITS = options.bits
? (options.binary ? BIBIT_UNITS : BIT_UNITS)
Expand Down Expand Up @@ -101,8 +105,7 @@ export const prettyBytes = (number, options) => {
}

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

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

return prefix + numberString + ' ' + unit;
};
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import {prettyBytes} from '.';
import prettyBytes from './index.js';

expectType<string>(prettyBytes(1337));
expectType<string>(prettyBytes(42, {signed: true}));
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"exports": "./index.js",
"types": "./index.d.ts",
"engines": {
"node": ">=12"
"node": "^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -42,6 +42,6 @@
"devDependencies": {
"ava": "^4.0.1",
"tsd": "^0.19.1",
"xo": "^0.47.0"
"xo": "^0.48.0"
}
}
12 changes: 5 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Useful for displaying file sizes for humans.

## Install

```
$ npm install pretty-bytes
```sh
npm install pretty-bytes
```

## Usage

```js
import { prettyBytes } from 'pretty-bytes';
import prettyBytes from 'pretty-bytes';

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

**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.

##### minimumFractionDigits

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

```js
import { prettyBytes } from 'pretty-bytes';
import prettyBytes from 'pretty-bytes';

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

```js
import { prettyBytes } from 'pretty-bytes';
import prettyBytes from 'pretty-bytes';

// Show the number with at most 1 fractional digit
prettyBytes(1920, {maximumFractionDigits: 1});
Expand Down
111 changes: 63 additions & 48 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import process from 'node:process';
import test from 'ava';
import {prettyBytes} from './index.js';
import prettyBytes from './index.js';

test('throws on invalid input', t => {
t.throws(() => prettyBytes(''));
t.throws(() => prettyBytes('1'));
t.throws(() => prettyBytes(Number.NaN));
t.throws(() => prettyBytes(true));
t.throws(() => prettyBytes(Number.POSITIVE_INFINITY));
t.throws(() => prettyBytes(Number.NEGATIVE_INFINITY));
t.throws(() => prettyBytes(null));
t.throws(() => {
prettyBytes('');
});

t.throws(() => {
prettyBytes('1');
});

t.throws(() => {
prettyBytes(Number.NaN);
});

t.throws(() => {
prettyBytes(true);
});

t.throws(() => {
prettyBytes(Number.POSITIVE_INFINITY);
});

t.throws(() => {
prettyBytes(Number.NEGATIVE_INFINITY);
});

t.throws(() => {
prettyBytes(null);
});
});

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

test('locale option', t => {
if (Number(process.version[0]) >= 14) {
t.is(prettyBytes(-0.4, {locale: 'de'}), '-0,4 B');
t.is(prettyBytes(0.4, {locale: 'de'}), '0,4 B');
t.is(prettyBytes(1001, {locale: 'de'}), '1 kB');
t.is(prettyBytes(10.1, {locale: 'de'}), '10,1 B');
t.is(prettyBytes(1e30, {locale: 'de'}), '1.000.000 YB');

t.is(prettyBytes(-0.4, {locale: 'en'}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: 'en'}), '0.4 B');
t.is(prettyBytes(1001, {locale: 'en'}), '1 kB');
t.is(prettyBytes(10.1, {locale: 'en'}), '10.1 B');
t.is(prettyBytes(1e30, {locale: 'en'}), '1,000,000 YB');

t.is(prettyBytes(-0.4, {locale: ['unknown', 'de', 'en']}), '-0,4 B');
t.is(prettyBytes(0.4, {locale: ['unknown', 'de', 'en']}), '0,4 B');
t.is(prettyBytes(1001, {locale: ['unknown', 'de', 'en']}), '1 kB');
t.is(prettyBytes(10.1, {locale: ['unknown', 'de', 'en']}), '10,1 B');
t.is(prettyBytes(1e30, {locale: ['unknown', 'de', 'en']}), '1.000.000 YB');

t.is(prettyBytes(-0.4, {locale: true}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: true}), '0.4 B');
t.is(prettyBytes(1001, {locale: true}), '1 kB');
t.is(prettyBytes(10.1, {locale: true}), '10.1 B');
t.is(prettyBytes(1e30, {locale: true}), '1,000,000 YB');

t.is(prettyBytes(-0.4, {locale: false}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: false}), '0.4 B');
t.is(prettyBytes(1001, {locale: false}), '1 kB');
t.is(prettyBytes(10.1, {locale: false}), '10.1 B');
t.is(prettyBytes(1e30, {locale: false}), '1000000 YB');

t.is(prettyBytes(-0.4, {locale: undefined}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: undefined}), '0.4 B');
t.is(prettyBytes(1001, {locale: undefined}), '1 kB');
t.is(prettyBytes(10.1, {locale: undefined}), '10.1 B');
t.is(prettyBytes(1e30, {locale: undefined}), '1000000 YB');
} else {
t.pass();
}
t.is(prettyBytes(-0.4, {locale: 'de'}), '-0,4 B');
t.is(prettyBytes(0.4, {locale: 'de'}), '0,4 B');
t.is(prettyBytes(1001, {locale: 'de'}), '1 kB');
t.is(prettyBytes(10.1, {locale: 'de'}), '10,1 B');
t.is(prettyBytes(1e30, {locale: 'de'}), '1.000.000 YB');

t.is(prettyBytes(-0.4, {locale: 'en'}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: 'en'}), '0.4 B');
t.is(prettyBytes(1001, {locale: 'en'}), '1 kB');
t.is(prettyBytes(10.1, {locale: 'en'}), '10.1 B');
t.is(prettyBytes(1e30, {locale: 'en'}), '1,000,000 YB');

t.is(prettyBytes(-0.4, {locale: ['unknown', 'de', 'en']}), '-0,4 B');
t.is(prettyBytes(0.4, {locale: ['unknown', 'de', 'en']}), '0,4 B');
t.is(prettyBytes(1001, {locale: ['unknown', 'de', 'en']}), '1 kB');
t.is(prettyBytes(10.1, {locale: ['unknown', 'de', 'en']}), '10,1 B');
t.is(prettyBytes(1e30, {locale: ['unknown', 'de', 'en']}), '1.000.000 YB');

t.is(prettyBytes(-0.4, {locale: true}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: true}), '0.4 B');
t.is(prettyBytes(1001, {locale: true}), '1 kB');
t.is(prettyBytes(10.1, {locale: true}), '10.1 B');
t.is(prettyBytes(1e30, {locale: true}), '1,000,000 YB');

t.is(prettyBytes(-0.4, {locale: false}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: false}), '0.4 B');
t.is(prettyBytes(1001, {locale: false}), '1 kB');
t.is(prettyBytes(10.1, {locale: false}), '10.1 B');
t.is(prettyBytes(1e30, {locale: false}), '1000000 YB');

t.is(prettyBytes(-0.4, {locale: undefined}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: undefined}), '0.4 B');
t.is(prettyBytes(1001, {locale: undefined}), '1 kB');
t.is(prettyBytes(10.1, {locale: undefined}), '10.1 B');
t.is(prettyBytes(1e30, {locale: undefined}), '1000000 YB');
});

test('signed option', t => {
Expand Down

0 comments on commit dd2a777

Please sign in to comment.