filesize.js

A lightweight, high-performance file size utility for JavaScript that converts bytes to human-readable strings. Works in both Node.js and browser environments with comprehensive format support.
Installation
npm install filesize
Usage
ES Modules
import {filesize} from "filesize";
filesize(265318, {standard: "jedec"});
CommonJS
const {filesize} = require("filesize");
filesize(1024);
Partial Application
import {partial} from "filesize";
const size = partial({standard: "jedec"});
size(265318);
Parameters
- input
{Number|String|BigInt} - The value to convert (required)
- options
{Object} - Configuration object (optional)
Options Object
- 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
{String|Boolean} - BCP 47 language tag to specify a locale, or true to use default locale, default is ""
- localeOptions
{Object} - Dictionary of options defined by ECMA-402 (Number.prototype.toLocaleString)
- 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)
- symbols
{Object} - Dictionary of IEC/JEDEC symbols to replace for localization
Input Validation
The function validates input and throws TypeError for invalid values:
try {
filesize("invalid");
} catch (error) {
console.error(error.message);
}
try {
filesize(NaN);
} catch (error) {
console.error(error.message);
}
Testing
filesize.js maintains 100% test coverage across all metrics with a comprehensive test suite of 47 test cases:
-------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
filesize.js | 100 | 100 | 100 | 100 |
-------------|---------|----------|---------|---------|-------------------
Running Tests
npm test
npm run mocha
Test Coverage
The test suite comprehensively covers:
- Basic functionality: Core conversion logic and edge cases
- Output formats: All output types (string, array, object, exponent)
- Standards support: IEC, JEDEC, and SI standards with different bases
- Bit conversion: Bits vs bytes with auto-increment logic
- Precision handling: Rounding methods and decimal precision
- Localization: Locale formatting and custom symbols
- Error handling: Invalid inputs and boundary conditions
- Partial functions: All option combinations with curried functions
API Reference
Functions
filesize(input, options)
Converts a numeric value to a human-readable file size string.
Parameters:
input {Number|String|BigInt} - The value to convert
options {Object} - Configuration options (optional)
Returns: {String|Array|Object|Number} - Formatted size based on output option
filesize(500);
filesize(1024, {base: 2});
filesize(265318, {output: "array"});
See also: partial()
partial(options)
Creates a pre-configured filesize function with options applied.
Parameters:
options {Object} - Configuration options to apply
Returns: {Function} - New function with options pre-applied
const formatBinary = partial({base: 2, standard: "iec"});
formatBinary(1048576);
const formatBits = partial({bits: true});
formatBits(1024);
See also: filesize()
Output Formats
String Output (default)
filesize(265318);
filesize(265318, {separator: ","});
Array Output
filesize(265318, {output: "array"});
filesize(1024, {output: "array", base: 2});
Object Output
filesize(265318, {output: "object"});
Exponent Output
filesize(1024, {output: "exponent"});
filesize(1048576, {output: "exponent", base: 2});
Standards Support
SI (International System of Units) - Default
filesize(1000);
filesize(1000000);
IEC (International Electrotechnical Commission)
filesize(1024, {standard: "iec", base: 2});
filesize(1048576, {standard: "iec", base: 2});
JEDEC (Joint Electron Device Engineering Council)
filesize(1024, {standard: "jedec"});
filesize(1048576, {standard: "jedec"});
Examples
Basic Usage
import {filesize} from "filesize";
filesize(500);
filesize(1024);
filesize(265318);
filesize(265318, {round: 0});
Binary Formats
filesize(1024, {base: 2, standard: "iec"});
filesize(1048576, {base: 2, standard: "iec"});
filesize(1024, {standard: "jedec"});
filesize(265318, {standard: "jedec"});
Bits vs Bytes
filesize(500, {bits: true});
filesize(1024, {bits: true});
filesize(1024, {bits: true, base: 2});
Custom Formatting
filesize(1024, {fullform: true});
filesize(1024, {base: 2, fullform: true});
filesize(265318, {separator: ","});
filesize(265318, {spacer: ""});
filesize(1536, {round: 3, pad: true});
filesize(1536, {precision: 3});
Localization
filesize(265318, {locale: "de"});
filesize(1, {symbols: {B: "Б"}});
filesize(12, {fullform: true, fullforms: ["байтов"]});
Advanced Usage
filesize(1024, {exponent: 0});
filesize(1024, {exponent: 1});
filesize(BigInt(1024), {standard: "jedec"});
filesize(Math.pow(1024, 8), {precision: 3});
Partial Application Patterns
import {partial} from "filesize";
const formatBinary = partial({base: 2, standard: "iec"});
const formatBits = partial({bits: true});
const formatPrecise = partial({round: 3, pad: true});
const formatGerman = partial({locale: "de"});
formatBinary(1048576);
formatBits(1024);
formatPrecise(1536);
formatGerman(265318);
const sizes = [1024, 2048, 4096];
sizes.map(formatBinary);
Development
This project follows Node.js best practices and uses:
- ES Modules for modern JavaScript
- Mocha for testing with comprehensive coverage
- ESLint for code quality and consistency
- Rollup for building distributions
- TypeScript definitions for type safety
Project Structure
filesize.js/
├── src/
│ ├── filesize.js # Main implementation
│ └── constants.js # Unit definitions and constants
├── tests/
│ └── unit/
│ └── filesize.test.js # Comprehensive test suite
├── types/
│ ├── filesize.d.ts # TypeScript definitions
│ └── constants.d.ts # Constants type definitions
└── package.json # Dependencies and scripts
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature)
- Write tests for your changes
- Ensure all tests pass (
npm test)
- Commit your changes (
git commit -m 'Add amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- Open a Pull Request
Development Commands
npm install
npm run lint
npm test
npm run build
npm run ci
License
Copyright (c) 2025 Jason Mulligan
Licensed under the BSD-3 license.