index.d.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. @default false
  6. */
  7. readonly signed?: boolean;
  8. /**
  9. - If `false`: Output won't be localized.
  10. - If `true`: Localize the output using the system/browser locale.
  11. - If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
  12. - If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
  13. __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.
  14. @default false
  15. */
  16. readonly locale?: boolean | string | readonly string[];
  17. /**
  18. 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).
  19. @default false
  20. @example
  21. ```
  22. import prettyBytes = require('pretty-bytes');
  23. prettyBytes(1337, {bits: true});
  24. //=> '1.34 kbit'
  25. ```
  26. */
  27. readonly bits?: boolean;
  28. /**
  29. 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.
  30. @default false
  31. @example
  32. ```
  33. import prettyBytes = require('pretty-bytes');
  34. prettyBytes(1000, {binary: true});
  35. //=> '1000 bit'
  36. prettyBytes(1024, {binary: true});
  37. //=> '1 kiB'
  38. ```
  39. */
  40. readonly binary?: boolean;
  41. /**
  42. The minimum number of fraction digits to display.
  43. If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
  44. @default undefined
  45. ```
  46. import prettyBytes = require('pretty-bytes');
  47. // Show the number with at least 3 fractional digits
  48. prettyBytes(1900, {minimumFractionDigits: 3});
  49. //=> '1.900 kB'
  50. prettyBytes(1900);
  51. //=> '1.9 kB'
  52. ```
  53. */
  54. readonly minimumFractionDigits?: number;
  55. /**
  56. The maximum number of fraction digits to display.
  57. If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
  58. @default undefined
  59. ```
  60. import prettyBytes = require('pretty-bytes');
  61. // Show the number with at most 1 fractional digit
  62. prettyBytes(1920, {maximumFractionDigits: 1});
  63. //=> '1.9 kB'
  64. prettyBytes(1920);
  65. //=> '1.92 kB'
  66. ```
  67. */
  68. readonly maximumFractionDigits?: number;
  69. }
  70. }
  71. /**
  72. Convert bytes to a human readable string: `1337` → `1.34 kB`.
  73. @param number - The number to format.
  74. @example
  75. ```
  76. import prettyBytes = require('pretty-bytes');
  77. prettyBytes(1337);
  78. //=> '1.34 kB'
  79. prettyBytes(100);
  80. //=> '100 B'
  81. // Display file size differences
  82. prettyBytes(42, {signed: true});
  83. //=> '+42 B'
  84. // Localized output using German locale
  85. prettyBytes(1337, {locale: 'de'});
  86. //=> '1,34 kB'
  87. ```
  88. */
  89. declare function prettyBytes(
  90. number: number,
  91. options?: prettyBytes.Options
  92. ): string;
  93. export = prettyBytes;