Skip to content

Repository files navigation

npm versioninstall sizenpm downloads

Bytes transform

Getting started

npm i bytes-transform

ECMAScript

After that, we can use:

import{formatBytes}from'bytes-transform';constnewFormat=formatBytes(1024,{from: 'MB',to: 'GB'});console.log(newFormat.amount,newFormat.prefix);

Since the object is returned we can use this syntax:

formatBytes(1024,{from: 'MB',to: 'GB'}).amount;// 1formatBytes(1024,{from: 'MB',to: 'GB'}).prefix;// 'GB'

If you need you can convert everything to bytes:

import{formatBytesToBytes}from'bytes-transform';formatBytesToBytes(1024,'MB'))// return -> 1073741824 bytesformatBytesToBytes(4,'MB'))// return -> 4194304 bytes

CommonJs

All that you can use with CommonJs:

const{ formatBytes, formatBytesToBytes }=require('bytes-transform');
...

All structures

formatBytesToBytes

typeTFormatBytesToBytesSignature=(amount: number,from: TListOfPrefix,capacityStrength: TCapacityStrength)=>number;/**Transfer your bytes with prefix to standart bytes.@param {number} amount count of bytes with prefix@param {TListOfPrefix} from prefix@param {TCapacityStrength} capacityStrength capacity strength@returns {number} number of standart bytes*/exportconstformatBytesToBytes: TFormatBytesToBytesSignature=(amount,from,capacityStrength=1024)=>{...};

formatBytes

typeTFormatBytesSignature=(amount: number,options: IFormatBytesOptions)=>IFormattedBytes;/** Transfer your bytes in bytes with other prefix@param {number} amount count of bytes with prefix@param {IFormatBytesOptions} options settigns for other information@returns {IFormatBytesReturned} object with amount and another prefix*/exportconstformatBytes: TFormatBytesSignature=(amount,options)=>{...}

TListOfPrefix

exporttypeTListOfPrefix='B'|'KB'|'MB'|'GB'|'TB';

TCapacityStrength

exporttypeTCapacityStrength=1000|1024;

TFixTo

exporttypeTFixTo=0|1|2|3|4|5|6|7|8|9|10;

IFormatBytesOptions

exportinterfaceIFormatBytesOptions{readonlyfrom: TListOfPrefix;readonlyto: TListOfPrefix;readonlycapacityStrength?: TCapacityStrength;readonlyfixTo?: TFixTo;}

IFormattedBytes

exportinterfaceIFormattedBytes{readonlyamount: number;readonlyprefix: TListOfPrefix;}

Happy hacking!

Releases

Used by

Contributors

Languages