UNPKG

2.99 kBMarkdownView Raw
1# filesize.js
2
3[![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)
4
5filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string.
6
7## Optional settings
8
9`filesize()` accepts an optional descriptor Object as a second argument, so you can customize the output.
10
11### base
12_*(number)*_ Number base, default is `2`
13
14### bits
15_*(boolean)*_ Enables `bit` sizes, default is `false`
16
17### exponent
18_*(number)*_ Specifies the SI suffix via exponent, e.g. `2` is `MB` for bytes, default is `-1`
19
20### fullform
21_*(boolean)*_ Enables full form of unit of measure, default is `false`
22
23### output
24_*(string)*_ Output of function (`array`, `exponent`, `object`, or `string`), default is `string`
25
26### round
27_*(number)*_ Decimal place, default is `2`
28
29### spacer
30_*(string)*_ Character between the `result` and `suffix`, default is `" "`
31
32### standard
33_*(string)*_ Standard unit of measure, can be `iec` or `jedec`, default is `jedec`; can be overruled by `base`
34
35### symbols
36_*(object)*_ Dictionary of SI/JEDEC symbols to replace for localization, defaults to english if no match is found
37
38### suffixes (deprecated: use 'symbols')
39_*(object)*_ Dictionary of SI/JEDEC symbols to replace for localization, defaults to english if no match is found
40
41### unix
42_*(boolean)*_ Enables unix style human readable output, e.g `ls -lh`, default is `false`
43
44## Examples
45
46```javascript
47filesize(500); // "500 B"
48filesize(500, {bits: true}); // "4 Kb"
49filesize(265318, {base: 10}); // "265.32 kB"
50filesize(265318); // "259.1 KB"
51filesize(265318, {round: 0}); // "259 KB"
52filesize(265318, {output: "array"}); // [259.1, "KB"]
53filesize(265318, {output: "object"}); // {value: 259.1, suffix: "KB", symbol: "KB"}
54filesize(1, {symbols: {B: "Б"}}); // "1 Б"
55filesize(1024); // "1 KB"
56filesize(1024, {exponent: 0}); // "1024 B"
57filesize(1024, {output: "exponent"}); // 1
58filesize(265318, {standard: "iec"}); // "259.1 KiB"
59filesize(265318, {standard: "iec", fullform: true}); // "259.1 kibibytes"
60```
61
62## Partial Application
63`filesize.partial()` takes the second parameter of `filesize()` and returns a new function with the configuration applied
64upon execution. This can be used to reduce `Object` creation if you call `filesize()` without caching the `descriptor`
65in lexical scope.
66
67```javascript
68const size = filesize.partial({standard: "iec"});
69
70size(265318); // "259.1 KiB"
71```
72
73## How can I load filesize.js?
74filesize.js supports AMD loaders (require.js, curl.js, etc.), node.js & npm (```npm install filesize```), or using a script tag.
75
76## License
77Copyright (c) 2017 Jason Mulligan
78Licensed under the BSD-3 license.
79
\No newline at end of file