Skip to content

1.3.5

Latest
Compare
Choose a tag to compare
@surpri6e surpri6e released this 18 Mar 18:09
· 4 commits to main since this release

npm version
install size
npm downloads

Bytes transform

Getting started

npm i bytes-transform

ECMAScript

After that, we can use:

import { formatBytes } from 'bytes-transform';

const newFormat = 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; // 1
formatBytes(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 bytes
formatBytesToBytes(4, 'MB')) // return -> 4194304 bytes

CommonJs

All that you can use with CommonJs:

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

...

All structures

formatBytesToBytes

/**
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
*/
export function formatBytesToBytes(amount: number, from: TListOfPrefix, capacityStrength: TCapacityStrength = 1024): number;

formatBytes

/**
  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
*/
export function formatBytes(amount: number, options: IFormatBytesOptions): IFormatBytesReturned;

TListOfPrefix

export type TListOfPrefix = 'B' | 'KB' | 'MB' | 'GB' | 'TB';

TCapacityStrength

export type TCapacityStrength = 1000 | 1024;

IFormatBytesOptions

export interface IFormatBytesOptions {
    readonly from: TListOfPrefix;
    readonly to: TListOfPrefix;
    readonly capacityStrength?: TCapacityStrength;
}

IFormatBytesReturned

export interface IFormatBytesReturned {
    readonly amount: number;
    readonly prefix: TListOfPrefix;
}

Happy hacking!