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