UNPKG

3.17 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) [![CDNJS version](https://img.shields.io/cdnjs/v/filesize.svg)](https://cdnjs.com/libraries/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 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
50filesize(500); // "500 B"
51filesize(500, {bits: true}); // "4 Kb"
52filesize(265318, {base: 10}); // "265.32 kB"
53filesize(265318); // "259.1 KB"
54filesize(265318, {round: 0}); // "259 KB"
55filesize(265318, {output: "array"}); // [259.1, "KB"]
56filesize(265318, {output: "object"}); // {value: 259.1, suffix: "KB", symbol: "KB"}
57filesize(1, {symbols: {B: "Б"}}); // "1 Б"
58filesize(1024); // "1 KB"
59filesize(1024, {exponent: 0}); // "1024 B"
60filesize(1024, {output: "exponent"}); // 1
61filesize(265318, {standard: "iec"}); // "259.1 KiB"
62filesize(265318, {standard: "iec", fullform: true}); // "259.1 kibibytes"
63filesize(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
68upon execution. This can be used to reduce `Object` creation if you call `filesize()` without caching the `descriptor`
69in lexical scope.
70
71```javascript
72const size = filesize.partial({standard: "iec"});
73
74size(265318); // "259.1 KiB"
75```
76
77## How can I load filesize.js?
78filesize.js supports AMD loaders (require.js, curl.js, etc.), node.js & npm (```npm install filesize```), or using a script tag.
79
80## License
81Copyright (c) 2017 Jason Mulligan
82Licensed under the BSD-3 license.
83
\No newline at end of file