web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.6 KiB

  1. # pretty-bytes
  2. > Convert bytes to a human readable string: `1337` → `1.34 kB`
  3. Useful for displaying file sizes for humans.
  4. *Note that it uses base-10 (e.g. kilobyte).
  5. [Read about the difference between kilobyte and kibibyte.](https://web.archive.org/web/20150324153922/https://pacoup.com/2009/05/26/kb-kb-kib-whats-up-with-that/)*
  6. ## Install
  7. ```
  8. $ npm install pretty-bytes
  9. ```
  10. ## Usage
  11. ```js
  12. const prettyBytes = require('pretty-bytes');
  13. prettyBytes(1337);
  14. //=> '1.34 kB'
  15. prettyBytes(100);
  16. //=> '100 B'
  17. // Display with units of bits
  18. prettyBytes(1337, {bits: true});
  19. //=> '1.34 kbit'
  20. // Display file size differences
  21. prettyBytes(42, {signed: true});
  22. //=> '+42 B'
  23. // Localized output using German locale
  24. prettyBytes(1337, {locale: 'de'});
  25. //=> '1,34 kB'
  26. ```
  27. ## API
  28. ### prettyBytes(number, options?)
  29. #### number
  30. Type: `number`
  31. The number to format.
  32. #### options
  33. Type: `object`
  34. ##### signed
  35. Type: `boolean`\
  36. Default: `false`
  37. Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.
  38. ##### bits
  39. Type: `boolean`\
  40. Default: `false`
  41. 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).
  42. ##### binary
  43. Type: `boolean`\
  44. Default: `false`
  45. 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.
  46. ##### locale
  47. Type: `boolean | string`\
  48. Default: `false` *(No localization)*
  49. **Important:** Only the number and decimal separator are localized. The unit title is not and will not be localized.
  50. - If `true`: Localize the output using the system/browser locale.
  51. - If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
  52. - If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
  53. **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.
  54. ##### minimumFractionDigits
  55. Type: `number`\
  56. Default: `undefined`
  57. The minimum number of fraction digits to display.
  58. If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
  59. ```js
  60. const prettyBytes = require('pretty-bytes');
  61. // Show the number with at least 3 fractional digits
  62. prettyBytes(1900, {minimumFractionDigits: 3});
  63. //=> '1.900 kB'
  64. prettyBytes(1900);
  65. //=> '1.9 kB'
  66. ```
  67. ##### maximumFractionDigits
  68. Type: `number`\
  69. Default: `undefined`
  70. The maximum number of fraction digits to display.
  71. If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
  72. ```js
  73. const prettyBytes = require('pretty-bytes');
  74. // Show the number with at most 1 fractional digit
  75. prettyBytes(1920, {maximumFractionDigits: 1});
  76. //=> '1.9 kB'
  77. prettyBytes(1920);
  78. //=> '1.92 kB'
  79. ```
  80. ## Related
  81. - [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module
  82. - [pretty-ms](https://github.com/sindresorhus/pretty-ms) - Convert milliseconds to a human readable string