@contexis/filesize
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

fileSize

Simple library to convert an integer to an Intl-compatible file size object.

Install

npm install @contexis/filesize --save

Usage

Basically, the function takes one required and one optional argument:

/*
* @param {number} size - filesize in bytes
* @param {boolean} [physical] - use 1000 as a base (like MacOS)
*
* @return {Object} containing the size as float and a unit as string
*/
fileSize(size, physical = false)

Vanilla Javascript

You can simply import the function into any javascript project and convert any number to a number formatter:

import fileSize from '@contexis/filesize'

const size = fileSize(125745000)
// size = {value: 125.74499999', unit: 'megabyte'}

const sizePhysical = fileSize(125745000, true)
// size = {value: 119.9197769', unit: 'megabyte'}

const fileSizeString = new Intl.NumberFormat(
	'en-US',
	{style: 'unit', unit: size.unit }).format(size.value);

console.log(fileSizeString);
// 125.74 Mb

TypeScript

Types can be imported:

import fileSize, { FileSize } from '@contexis/filesize'

const size: FileSize = fileSize(125745000)

Readme

Keywords

Package Sidebar

Install

npm i @contexis/filesize

Weekly Downloads

0

Version

1.2.0

License

ISC

Unpacked Size

3.57 kB

Total Files

6

Last publish

Collaborators

  • contexis