| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 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. |
| 5 | |
| 6 | @default false |
| 7 | */ |
| 8 | readonly signed?: boolean; |
| 9 | |
| 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`, …) |
| 15 | |
| 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. |
| 17 | |
| 18 | @default false |
| 19 | */ |
| 20 | readonly locale?: boolean | string | readonly string[]; |
| 21 | |
| 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). |
| 24 | |
| 25 | @default false |
| 26 | |
| 27 | @example |
| 28 | ``` |
| 29 | import prettyBytes = require('pretty-bytes'); |
| 30 | |
| 31 | prettyBytes(1337, {bits: true}); |
| 32 | //=> '1.34 kbit' |
| 33 | ``` |
| 34 | */ |
| 35 | readonly bits?: boolean; |
| 36 | |
| 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. |
| 39 | |
| 40 | @default false |
| 41 | |
| 42 | @example |
| 43 | ``` |
| 44 | import prettyBytes = require('pretty-bytes'); |
| 45 | |
| 46 | prettyBytes(1000, {binary: true}); |
| 47 | //=> '1000 bit' |
| 48 | |
| 49 | prettyBytes(1024, {binary: true}); |
| 50 | //=> '1 kiB' |
| 51 | ``` |
| 52 | */ |
| 53 | readonly binary?: boolean; |
| 54 | |
| 55 | /** |
| 56 | The minimum number of fraction digits to display. |
| 57 | |
| 58 | If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits. |
| 59 | |
| 60 | @default undefined |
| 61 | |
| 62 | ``` |
| 63 | import prettyBytes = require('pretty-bytes'); |
| 64 | |
| 65 | // Show the number with at least 3 fractional digits |
| 66 | prettyBytes(1900, {minimumFractionDigits: 3}); |
| 67 | //=> '1.900 kB' |
| 68 | |
| 69 | prettyBytes(1900); |
| 70 | //=> '1.9 kB' |
| 71 | ``` |
| 72 | */ |
| 73 | readonly minimumFractionDigits?: number; |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | The maximum number of fraction digits to display. |
| 78 | |
| 79 | If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits. |
| 80 | |
| 81 | @default undefined |
| 82 | |
| 83 | ``` |
| 84 | import prettyBytes = require('pretty-bytes'); |
| 85 | |
| 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 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | Convert bytes to a human readable string: `1337` → `1.34 kB`. |
| 100 | |
| 101 | @param number - The number to format. |
| 102 | |
| 103 | @example |
| 104 | ``` |
| 105 | import prettyBytes = require('pretty-bytes'); |
| 106 | |
| 107 | prettyBytes(1337); |
| 108 | //=> '1.34 kB' |
| 109 | |
| 110 | prettyBytes(100); |
| 111 | //=> '100 B' |
| 112 | |
| 113 | // Display file size differences |
| 114 | prettyBytes(42, {signed: true}); |
| 115 | //=> '+42 B' |
| 116 | |
| 117 | // Localized output using German locale |
| 118 | prettyBytes(1337, {locale: 'de'}); |
| 119 | //=> '1,34 kB' |
| 120 | ``` |
| 121 | */ |
| 122 | declare function prettyBytes( |
| 123 | number: number, |
| 124 | options?: prettyBytes.Options |
| 125 | ): string; |
| 126 | |
| 127 | export = prettyBytes; |