From dd2a777ba16cc557d0579a0b806044b517cccc6d Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 14 Feb 2022 01:13:09 +0700 Subject: [PATCH] Meta tweaks --- .github/funding.yml | 3 -- index.d.ts | 14 +++--- index.js | 13 ++++-- index.test-d.ts | 2 +- package.json | 4 +- readme.md | 12 ++--- test.js | 111 +++++++++++++++++++++++++------------------- 7 files changed, 85 insertions(+), 74 deletions(-) delete mode 100644 .github/funding.yml diff --git a/.github/funding.yml b/.github/funding.yml deleted file mode 100644 index 1a630e9..0000000 --- a/.github/funding.yml +++ /dev/null @@ -1,3 +0,0 @@ -github: sindresorhus -open_collective: sindresorhus -custom: https://sindresorhus.com/donate diff --git a/index.d.ts b/index.d.ts index fbf1ea4..efcfd14 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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[]; @@ -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' @@ -40,7 +38,7 @@ export interface Options { @example ``` - import { prettyBytes } from 'pretty-bytes'; + import prettyBytes from 'pretty-bytes'; prettyBytes(1000, {binary: true}); //=> '1000 bit' @@ -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}); @@ -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}); @@ -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' @@ -116,7 +114,7 @@ prettyBytes(1337, {locale: 'de'}); //=> '1,34 kB' ``` */ -export function prettyBytes( +export default function prettyBytes( number: number, options?: Options ): string; diff --git a/index.js b/index.js index ce26746..ca4fd3f 100644 --- a/index.js +++ b/index.js @@ -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) @@ -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); @@ -113,4 +116,4 @@ export const prettyBytes = (number, options) => { const unit = UNITS[exponent]; return prefix + numberString + ' ' + unit; -}; +} diff --git a/index.test-d.ts b/index.test-d.ts index 64c3b30..b98f89a 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType} from 'tsd'; -import {prettyBytes} from '.'; +import prettyBytes from './index.js'; expectType(prettyBytes(1337)); expectType(prettyBytes(42, {signed: true})); diff --git a/package.json b/package.json index d973ce4..dbbfca2 100644 --- a/package.json +++ b/package.json @@ -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" @@ -42,6 +42,6 @@ "devDependencies": { "ava": "^4.0.1", "tsd": "^0.19.1", - "xo": "^0.47.0" + "xo": "^0.48.0" } } diff --git a/readme.md b/readme.md index 58522bc..133c592 100644 --- a/readme.md +++ b/readme.md @@ -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' @@ -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`\ @@ -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}); @@ -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}); diff --git a/test.js b/test.js index 5ff616b..f263d98 100644 --- a/test.js +++ b/test.js @@ -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 => { @@ -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 => {