JavaScript library to generate a human readable String describing the file size


Keywords
file, filesize, size, readable, file system, bytes, diff, bits, filesystem, iec, jedec, simple, size-calculation
License
BSD-3-Clause
Install
npm install filesize@10.0.12

Documentation

filesize.js

downloads CDNJS version

filesize.js provides a simple way to get a human-readable file size string from a number (float or integer) or string.

import {filesize} from "filesize";
filesize(265318, {standard: "jedec"}); // "259.1 KB"

Testing

filesize has 100% code coverage with its tests.

--------------|---------|----------|---------|---------|-----------------------
File          | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-----------------------
All files     |     100 |    95.52 |     100 |     100 |                      
 filesize.cjs |     100 |    95.52 |     100 |     100 | 77-78,173,196,199,210
--------------|---------|----------|---------|---------|-----------------------

Optional settings

filesize() accepts an optional descriptor Object as a second argument, so you can customize the output.

base

(number) Number base, default is 10

bits

(boolean) Enables bit sizes, default is false

exponent

(number) Specifies the symbol via exponent, e.g. 2 is MB for base 2, default is -1

fullform

(boolean) Enables full form of unit of measure, default is false

fullforms

(array) Array of full form overrides, default is []

locale (overrides 'separator')

(string || boolean) BCP 47 language tag to specify a locale, or true to use default locale, default is ""

localeOptions (overrides 'separator', requires string for 'locale' option)

(object) Dictionary of options defined by ECMA-402 (Number.prototype.toLocaleString). Requires locale option to be explicitly passed as a string, otherwise is ignored.

output

(string) Output of function (array, exponent, object, or string), default is string

pad

(boolean) Decimal place end padding, default is false

precision

(number) Sets precision of numerical output, default is 0

round

(number) Decimal place, default is 2

roundingMethod

(string) Rounding method, can be round, floor, or ceil, default is round

separator

(string) Decimal separator character, default is an empty string.

spacer

(string) Character between the result and symbol, default is " "

standard

(string) Standard unit of measure, can be iec, jedec, or si. Default is si (base 10). The si option is an alias of jedec, such that it is not valid for other configuration options.

symbols

(object) Dictionary of IEC/JEDEC symbols to replace for localization, defaults to english if no match is found; SI is handled automatically with JEDEC values.

Examples

filesize(500);                        // "500 B"
filesize(500, {bits: true});          // "4 kbit"
filesize(265318, {base: 2});          // "259.1 KiB"
filesize(265318);                     // "265.32 kB"
filesize(265318, {round: 0});         // "265 kB"
filesize(265318, {output: "array"});  // [265.32, "kB"]
filesize(265318, {output: "object"}); // {value: 265.32, symbol: "kB", exponent: 1, unit: "kB"}
filesize(1, {symbols: {B: "Б"}});     // "1 Б"
filesize(1024);                       // "1.02 kB"
filesize(1024, {exponent: 0});        // "1024 B"
filesize(1024, {output: "exponent"}); // 1
filesize(265318, {standard: "jedec"});  // "259.1 KB"
filesize(265318, {base: 2, fullform: true}); // "259.1 kibibytes"
filesize(12, {fullform: true, fullforms: ["байтов"]});  // "12 байтов"
filesize(265318, {separator: ","});   // "265,32 kB"
filesize(265318, {locale: "de"});   // "265,32 kB"

Partial Application

partial() takes the second parameter of filesize() and returns a new function with the configuration applied upon execution. This can be used to reduce Object creation if you call filesize() without caching the descriptor in lexical scope.

import {partial} from "filesize";
const size = partial({standard: "jedec"});

size(265318); // "259.1 KB"

License

Copyright (c) 2024 Jason Mulligan Licensed under the BSD-3 license.