https://github.com/surpri6e/bytes-transform
Package to transform your bytes
https://github.com/surpri6e/bytes-transform
bytes bytesize format formatting npm npm-package size-calculation typescript
Last synced: 8 months ago
JSON representation
Package to transform your bytes
- Host: GitHub
- URL: https://github.com/surpri6e/bytes-transform
- Owner: surpri6e
- License: mit
- Created: 2024-02-07T18:20:08.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-06T16:52:59.000Z (over 1 year ago)
- Last Synced: 2025-04-15T14:04:39.291Z (8 months ago)
- Topics: bytes, bytesize, format, formatting, npm, npm-package, size-calculation, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/bytes-transform
- Size: 88.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.org/package/bytes-transform)
[](https://packagephobia.now.sh/result?p=bytes-transform)
[](https://npm-stat.com/charts.html?package=bytes-transform)
# Bytes transform
## Getting started
```
npm i bytes-transform
```
### ECMAScript
After that, we can use:
```js
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:
```js
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:
```js
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:
```js
const { formatBytes, formatBytesToBytes } = require('bytes-transform');
...
```
## All structures
### formatBytesToBytes
```ts
type TFormatBytesToBytesSignature = (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
*/
export const formatBytesToBytes: TFormatBytesToBytesSignature = (amount, from, capacityStrength = 1024) => {...};
```
### formatBytes
```ts
type TFormatBytesSignature = (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
*/
export const formatBytes: TFormatBytesSignature = (amount, options) => {...}
```
### TListOfPrefix
```ts
export type TListOfPrefix = 'B' | 'KB' | 'MB' | 'GB' | 'TB';
```
### TCapacityStrength
```ts
export type TCapacityStrength = 1000 | 1024;
```
### TFixTo
```ts
export type TFixTo = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
```
### IFormatBytesOptions
```ts
export interface IFormatBytesOptions {
readonly from: TListOfPrefix;
readonly to: TListOfPrefix;
readonly capacityStrength?: TCapacityStrength;
readonly fixTo?: TFixTo;
}
```
### IFormattedBytes
```ts
export interface IFormattedBytes {
readonly amount: number;
readonly prefix: TListOfPrefix;
}
```
Happy hacking!