1 | # filesize.js
|
2 |
|
3 | [![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)
|
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 | ```javascript
|
8 | import {filesize} from "filesize";
|
9 | filesize(265318, {base: 2, standard: "jedec"}); // "259.1 KB"
|
10 | ```
|
11 |
|
12 | ## Optional settings
|
13 |
|
14 | `filesize()` accepts an optional descriptor Object as a second argument, so you can customize the output.
|
15 |
|
16 | ### base
|
17 | _*(number)*_ Number base, default is `10`
|
18 |
|
19 | ### bits
|
20 | _*(boolean)*_ Enables `bit` sizes, default is `false`
|
21 |
|
22 | ### exponent
|
23 | _*(number)*_ Specifies the symbol via exponent, e.g. `2` is `MB` for base 2, default is `-1`
|
24 |
|
25 | ### fullform
|
26 | _*(boolean)*_ Enables full form of unit of measure, default is `false`
|
27 |
|
28 | ### fullforms
|
29 | _*(array)*_ Array of full form overrides, default is `[]`
|
30 |
|
31 | ### locale (overrides 'separator')
|
32 | _*(string || boolean)*_ BCP 47 language tag to specify a locale, or `true` to use default locale, default is `""`
|
33 |
|
34 | ### localeOptions (overrides 'separator', requires string for 'locale' option)
|
35 | _*(object)*_ Dictionary of options defined by ECMA-402 ([Number.prototype.toLocaleString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString)). Requires locale option to be explicitly passed as a string, otherwise is ignored.
|
36 |
|
37 | ### output
|
38 | _*(string)*_ Output of function (`array`, `exponent`, `object`, or `string`), default is `string`
|
39 |
|
40 | ### pad
|
41 | _*(boolean)*_ Decimal place end padding, default is `false`
|
42 |
|
43 | ### precision
|
44 | _*(number)*_ Sets precision of numerical output, default is `0`
|
45 |
|
46 | ### round
|
47 | _*(number)*_ Decimal place, default is `2`
|
48 |
|
49 | ### roundingMethod
|
50 | _*(string)*_ Rounding method, can be `round`, `floor`, or `ceil`, default is `round`
|
51 |
|
52 | ### separator
|
53 | _*(string)*_ Decimal separator character, default is `.`
|
54 |
|
55 | ### spacer
|
56 | _*(string)*_ Character between the `result` and `symbol`, default is `" "`
|
57 |
|
58 | ### standard
|
59 | _*(string)*_ Standard unit of measure, can be `iec` or `jedec`, default is `iec`; can be overruled by `base`
|
60 |
|
61 | ### symbols
|
62 | _*(object)*_ Dictionary of IEC/JEDEC symbols to replace for localization, defaults to english if no match is found
|
63 |
|
64 |
|
65 | ## Partial Application
|
66 | `partial()` takes the second parameter of `filesize()` and returns a new function with the configuration applied
|
67 | upon execution. This can be used to reduce `Object` creation if you call `filesize()` without caching the `descriptor`
|
68 | in lexical scope.
|
69 |
|
70 | ```javascript
|
71 | import {partial} from "filesize";
|
72 | const size = partial({base: 2, standard: "jedec"});
|
73 |
|
74 | size(265318); // "259.1 KB"
|
75 | ```
|
76 |
|
77 | ## License
|
78 | Copyright (c) 2022 Jason Mulligan
|
79 | Licensed under the BSD-3 license.
|