Skip to content

Releases: surpri6e/bytes-transform

1.3.5

18 Mar 18:09
Compare
Choose a tag to compare

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!

1.3.0

09 Feb 11:46
75acee1
Compare
Choose a tag to compare

Bytes transform

Getting started

npm i bytes-transform

After that we can use:

import { formatBytes } from 'bytes-transform';

const newFormat = formatBytes(5000, {'MB', 'GB'});
console.log(newFormat.amountOfCapacityBytes, newFormat.size);

Try to integrate this function in Files prototype.

1.3.1

09 Feb 11:52
Compare
Choose a tag to compare

Just end!