c89922455417d4b9c520ea069874bf9b1ed81b9ce93da2812317d0322f1edd6512f7e122c65745b9ceb8dc91e2328cfd9713639be5f98c2621d738fdfed2a6 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # filesize.js
  2. [![build status](https://secure.travis-ci.org/avoidwork/filesize.js.svg)](http://travis-ci.org/avoidwork/filesize.js) [![downloads](https://img.shields.io/npm/dt/filesize.svg)](https://www.npmjs.com/package/filesize) [![CDNJS version](https://img.shields.io/cdnjs/v/filesize.svg)](https://cdnjs.com/libraries/filesize)
  3. filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string.
  4. ## Optional settings
  5. `filesize()` accepts an optional descriptor Object as a second argument, so you can customize the output.
  6. ### base
  7. _*(number)*_ Number base, default is `2`
  8. ### bits
  9. _*(boolean)*_ Enables `bit` sizes, default is `false`
  10. ### exponent
  11. _*(number)*_ Specifies the symbol via exponent, e.g. `2` is `MB` for base 2, default is `-1`
  12. ### fullform
  13. _*(boolean)*_ Enables full form of unit of measure, default is `false`
  14. ### fullforms
  15. _*(array)*_ Array of full form overrides, default is `[]`
  16. ### output
  17. _*(string)*_ Output of function (`array`, `exponent`, `object`, or `string`), default is `string`
  18. ### round
  19. _*(number)*_ Decimal place, default is `2`
  20. ### separator
  21. _*(string)*_ Decimal separator character, default is `.`
  22. ### spacer
  23. _*(string)*_ Character between the `result` and `suffix`, default is `" "`
  24. ### standard
  25. _*(string)*_ Standard unit of measure, can be `iec` or `jedec`, default is `jedec`; can be overruled by `base`
  26. ### symbols
  27. _*(object)*_ Dictionary of SI/JEDEC symbols to replace for localization, defaults to english if no match is found
  28. ### suffixes (deprecated: use 'symbols')
  29. _*(object)*_ Dictionary of SI/JEDEC symbols to replace for localization, defaults to english if no match is found
  30. ### unix
  31. _*(boolean)*_ Enables unix style human readable output, e.g `ls -lh`, default is `false`
  32. ## Examples
  33. ```javascript
  34. filesize(500); // "500 B"
  35. filesize(500, {bits: true}); // "4 Kb"
  36. filesize(265318, {base: 10}); // "265.32 kB"
  37. filesize(265318); // "259.1 KB"
  38. filesize(265318, {round: 0}); // "259 KB"
  39. filesize(265318, {output: "array"}); // [259.1, "KB"]
  40. filesize(265318, {output: "object"}); // {value: 259.1, suffix: "KB", symbol: "KB"}
  41. filesize(1, {symbols: {B: "Б"}}); // "1 Б"
  42. filesize(1024); // "1 KB"
  43. filesize(1024, {exponent: 0}); // "1024 B"
  44. filesize(1024, {output: "exponent"}); // 1
  45. filesize(265318, {standard: "iec"}); // "259.1 KiB"
  46. filesize(265318, {standard: "iec", fullform: true}); // "259.1 kibibytes"
  47. filesize(12, {fullform: true, fullforms: ["байтов"]}); // "12 байтов"
  48. filesize(265318, {separator: ","}); // "259,1 KB"
  49. ```
  50. ## Partial Application
  51. `filesize.partial()` takes the second parameter of `filesize()` and returns a new function with the configuration applied
  52. upon execution. This can be used to reduce `Object` creation if you call `filesize()` without caching the `descriptor`
  53. in lexical scope.
  54. ```javascript
  55. const size = filesize.partial({standard: "iec"});
  56. size(265318); // "259.1 KiB"
  57. ```
  58. ## How can I load filesize.js?
  59. filesize.js supports AMD loaders (require.js, curl.js, etc.), node.js & npm (```npm install filesize```), or using a script tag.
  60. An ES6 version is bundled with an npm install, but requires you load it with the full path, e.g. `require(path.join(__dirname, 'node_modules', 'filesize', 'lib', 'filesize.es6.js'))`.
  61. ## License
  62. Copyright (c) 2018 Jason Mulligan
  63. Licensed under the BSD-3 license.