fs

file-size

📂 Lightweight filesize to human-readable / proportions w/o dependencies for node.js & browsers.

Showing:

Popularity

Downloads/wk

95.8K

GitHub Stars

226

Maintenance

Last Commit

2yrs ago

Contributors

2

Package

Dependencies

0

Size (min+gzip)

0.6KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Reviews

Average Rating

5.0/51
Read All Reviews
AhmeeedMostafa

Top Feedback

1Great Documentation
1Easy to Use
1Performant
1Bleeding Edge

Readme

File-size.js

filesize

File-size module for node.js for converting, manipulating, and handling file-sizes.

Features

  • Zero Dependencies.
  • Supports IEC (power 1024, default), SI (power 1000), and JEDEC (Alternative SI Unit Notation).
  • Conversion from bytes to K, M, G, and so forth...
  • Customizable human-readable output.

Installation

$ npm install file-size

Now require it in your files like so:

var filesize = require('file-size');

Usage

filesize(Number bytes, Object options)

Using filesize is very easy, a lot easier if you know the Specification Table. However, it's not required to know it, and in most cases you won't need it.

Initialization

Filesize works in a jQuery chainable manner so you can use multiple instances without trouble.

var size = filesize(186457865);

Options

var size = filesize(186457865, {
  fixed: 2,
  spacer: ' '
});
  • fixed - Number of positions after the decimal to show, default is 2
  • spacer - Space between the number and the unit, default is a space

size.human(String spec) -> String

Generates human readable filesize.

  • spec can be one of the following:
    • si
    • iec
    • jedec
IEC Specification

Default

Power of 1024

// outputs: 177.82 MiB
filesize(186457865).human();
SI Specification

Power of 1000

// outputs: 186.46 MB
filesize(186457865).human('si');
JEDEC Specification

Changes IEC default unit notation iB to B for the following: KB, MB, GB

The rest are in iB notation, as per JEDEC Specification.

// outputs: 186.46 MB
filesize(186457865).human('jedec');

size.to(String unit, String spec) -> String

Converts bytes to another filesize unit

  • unit of size can be one of the following:
    • B, KB, MB, GB, TB, PB, EB, ZB, YB
  • spec can be one of the following:
    • si
    • iec
    • jedec
IEC Specification

Default

// outputs: 177.82
filesize(186457865).to('MB');
SI Specification
// outputs: 186.46
filesize(186457865).to('MB', true);

size.calculate(String spec) -> Object

Calculates suffix, magnitude, fixed, non-fixed, bits (fixed, non-fixed) from specified bytes against the specified spec.

  • spec can be one of the following:
    • si
    • iec
    • jedec
/*
 * outputs:
 *
 *    {
 *      suffix: 'Bytes',
 *      magnitude: 0,
 *      result: 8,
 *      fixed: '8.00',
 *      bits: { result: 1, fixed: '1.00' }
 *    }
*/

var result = filesize(8).calculate()

Rate & Review

Great Documentation1
Easy to Use1
Performant1
Highly Customizable0
Bleeding Edge1
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
Ahmed KhallafCairo, Egypt38 Ratings108 Reviews
A full-stack web developer with NodeJS, PHP & ReactJS.
October 20, 2020
Easy to Use
Great Documentation
Performant
Bleeding Edge

Brilliant package for showing the file size in a human readable format instead of showing just a long number or doing a lot of operations to catch the nearest measure unit for the current size


0

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial