Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9db9f7d
commit 44858ce
Showing
7 changed files
with
123 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
* text=auto | ||
*.js text eol=lf | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
declare namespace prettyBytes { | ||
interface Options { | ||
/** | ||
Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment. | ||
@default false | ||
*/ | ||
readonly signed?: boolean; | ||
|
||
/** | ||
- If `false`: Output won't be localized. | ||
- If `true`: Localize the output using the system/browser locale. | ||
- If `string`: Expects a [BCP 47 language tag](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; | ||
} | ||
} | ||
|
||
/** | ||
Convert bytes to a human readable string: `1337` → `1.34 kB`. | ||
@param number - The number to format. | ||
@example | ||
``` | ||
import prettyBytes = require('pretty-bytes'); | ||
prettyBytes(1337); | ||
//=> '1.34 kB' | ||
prettyBytes(100); | ||
//=> '100 B' | ||
// Display file size differences | ||
prettyBytes(42, {signed: true}); | ||
//=> '+42 B' | ||
// Localized output using German locale | ||
prettyBytes(1337, {locale: 'de'}); | ||
//=> '1,34 kB' | ||
``` | ||
*/ | ||
declare function prettyBytes( | ||
number: number, | ||
options?: prettyBytes.Options | ||
): string; | ||
|
||
export = prettyBytes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {expectType} from 'tsd'; | ||
import prettyBytes = require('.'); | ||
|
||
const options: prettyBytes.Options = {}; | ||
|
||
expectType<string>(prettyBytes(1337)); | ||
expectType<string>(prettyBytes(42, {signed: true})); | ||
expectType<string>(prettyBytes(1337, {locale: 'de'})); | ||
expectType<string>(prettyBytes(1337, {locale: true})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,71 @@ | ||
import test from 'ava'; | ||
import m from '.'; | ||
import prettyBytes from '.'; | ||
|
||
test('throws on invalid input', t => { | ||
t.throws(() => m('')); | ||
t.throws(() => m('1')); | ||
t.throws(() => m(NaN)); | ||
t.throws(() => m(true)); | ||
t.throws(() => m(Infinity)); | ||
t.throws(() => m(-Infinity)); | ||
t.throws(() => m(null)); | ||
t.throws(() => prettyBytes('')); | ||
t.throws(() => prettyBytes('1')); | ||
t.throws(() => prettyBytes(NaN)); | ||
t.throws(() => prettyBytes(true)); | ||
t.throws(() => prettyBytes(Infinity)); | ||
t.throws(() => prettyBytes(-Infinity)); | ||
t.throws(() => prettyBytes(null)); | ||
}); | ||
|
||
test('converts bytes to human readable strings', t => { | ||
t.is(m(0), '0 B'); | ||
t.is(m(0.4), '0.4 B'); | ||
t.is(m(0.7), '0.7 B'); | ||
t.is(m(10), '10 B'); | ||
t.is(m(10.1), '10.1 B'); | ||
t.is(m(999), '999 B'); | ||
t.is(m(1001), '1 kB'); | ||
t.is(m(1001), '1 kB'); | ||
t.is(m(1e16), '10 PB'); | ||
t.is(m(1e30), '1000000 YB'); | ||
t.is(prettyBytes(0), '0 B'); | ||
t.is(prettyBytes(0.4), '0.4 B'); | ||
t.is(prettyBytes(0.7), '0.7 B'); | ||
t.is(prettyBytes(10), '10 B'); | ||
t.is(prettyBytes(10.1), '10.1 B'); | ||
t.is(prettyBytes(999), '999 B'); | ||
t.is(prettyBytes(1001), '1 kB'); | ||
t.is(prettyBytes(1001), '1 kB'); | ||
t.is(prettyBytes(1e16), '10 PB'); | ||
t.is(prettyBytes(1e30), '1000000 YB'); | ||
}); | ||
|
||
test('supports negative number', t => { | ||
t.is(m(-0.4), '-0.4 B'); | ||
t.is(m(-0.7), '-0.7 B'); | ||
t.is(m(-10.1), '-10.1 B'); | ||
t.is(m(-999), '-999 B'); | ||
t.is(m(-1001), '-1 kB'); | ||
t.is(prettyBytes(-0.4), '-0.4 B'); | ||
t.is(prettyBytes(-0.7), '-0.7 B'); | ||
t.is(prettyBytes(-10.1), '-10.1 B'); | ||
t.is(prettyBytes(-999), '-999 B'); | ||
t.is(prettyBytes(-1001), '-1 kB'); | ||
}); | ||
|
||
test('locale option', t => { | ||
t.is(m(-0.4, {locale: 'de'}), '-0,4 B'); | ||
t.is(m(0.4, {locale: 'de'}), '0,4 B'); | ||
t.is(m(1001, {locale: 'de'}), '1 kB'); | ||
t.is(m(10.1, {locale: 'de'}), '10,1 B'); | ||
t.is(m(1e30, {locale: 'de'}), '1.000.000 YB'); | ||
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(m(-0.4, {locale: 'en'}), '-0.4 B'); | ||
t.is(m(0.4, {locale: 'en'}), '0.4 B'); | ||
t.is(m(1001, {locale: 'en'}), '1 kB'); | ||
t.is(m(10.1, {locale: 'en'}), '10.1 B'); | ||
t.is(m(1e30, {locale: 'en'}), '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(m(-0.4, {locale: true}), '-0.4 B'); | ||
t.is(m(0.4, {locale: true}), '0.4 B'); | ||
t.is(m(1001, {locale: true}), '1 kB'); | ||
t.is(m(10.1, {locale: true}), '10.1 B'); | ||
t.is(m(1e30, {locale: true}), '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(m(-0.4, {locale: false}), '-0.4 B'); | ||
t.is(m(0.4, {locale: false}), '0.4 B'); | ||
t.is(m(1001, {locale: false}), '1 kB'); | ||
t.is(m(10.1, {locale: false}), '10.1 B'); | ||
t.is(m(1e30, {locale: false}), '1000000 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(m(-0.4, {locale: undefined}), '-0.4 B'); | ||
t.is(m(0.4, {locale: undefined}), '0.4 B'); | ||
t.is(m(1001, {locale: undefined}), '1 kB'); | ||
t.is(m(10.1, {locale: undefined}), '10.1 B'); | ||
t.is(m(1e30, {locale: undefined}), '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 => { | ||
t.is(m(42, {signed: true}), '+42 B'); | ||
t.is(m(-13, {signed: true}), '-13 B'); | ||
t.is(m(0, {signed: true}), ' 0 B'); | ||
t.is(prettyBytes(42, {signed: true}), '+42 B'); | ||
t.is(prettyBytes(-13, {signed: true}), '-13 B'); | ||
t.is(prettyBytes(0, {signed: true}), ' 0 B'); | ||
}); |