Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cheton/readable-size
Converts bytes into human readable size units.
https://github.com/cheton/readable-size
bytes file filesize human readable size units
Last synced: 10 days ago
JSON representation
Converts bytes into human readable size units.
- Host: GitHub
- URL: https://github.com/cheton/readable-size
- Owner: cheton
- License: mit
- Created: 2018-06-28T11:43:15.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-08-27T11:51:18.000Z (about 5 years ago)
- Last Synced: 2024-10-29T16:26:41.210Z (20 days ago)
- Topics: bytes, file, filesize, human, readable, size, units
- Language: JavaScript
- Homepage:
- Size: 12.7 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# readable-size [![build status](https://travis-ci.org/cheton/readable-size.svg?branch=master)](https://travis-ci.org/cheton/readable-size) [![Coverage Status](https://coveralls.io/repos/github/cheton/readable-size/badge.svg?branch=master)](https://coveralls.io/github/cheton/readable-size?branch=master)
[![NPM](https://nodei.co/npm/readable-size.png?downloads=true&stars=true)](https://www.npmjs.com/package/readable-size)Converts bytes into human readable size units: B, KB, MB, GB, TB, PB, EB, ZB, YB.
## Installation
```bash
npm install --save readable-size
```## Examples
```js
readableSize(1); // '1 B'
readableSize(1023); // '1023 B'
readableSize(1024); // '1.00 KB'
readableSize(1025); // '1.00 KB'
readableSize(1000000); // '976 KB'
readableSize(1023999); // '999 KB'
readableSize(1024000); // '0.97 MB'
readableSize(1048575); // '0.99 MB'
readableSize(1048576); // '1.00 MB'
readableSize(1048577); // '1.00 MB'
readableSize(1000000000); // '953 MB'
readableSize(1048575999); // '999 MB'
readableSize(1048576000); // '0.97 GB'
readableSize(1073741823); // '0.99 GB'
readableSize(1073741824); // '1.00 TB'
```### Output
The output is one of `'string'`, `'array'`, `'object'`, or function type.
#### 'string'
```js
readableSize(1024, { output: 'string' }); // '1.00 KB'
readableSize(1024, { output: 'string', format: '{{size}} ({{unit}})' }); // '1.00 (KB)'
```#### 'array'
```js
readableSize(1024, { output: 'array' }); // [ '1.00', 'KB' ]
```#### 'object'
```js
readableSize(1024, { output: 'object' }); // { size: '1.00', unit: 'KB' }
```#### function
```js
readableSize(999, { // '999 bytes'
output: ({ size, unit }) => {
unit = { B: 'bytes' }[unit] || unit;
return `${size} ${unit}`;
}
});
```### Separators
#### English
```js
const options = {
separator: {
thousands: ',',
decimal: '.',
}
};
readableSize(999, options); // '999 B'
readableSize(1000, options); // '1,000 B'
readableSize(1024, options); // '1.00 KB'
```#### French
```js
const options = {
separator: {
thousands: ' ',
decimal: ',',
}
};
readableSize(999, options); // '999 B'
readableSize(1000, options); // '1 000 B'
readableSize(1024, options); // '1,00 KB'
```#### Dutch
```js
const options = {
separator: {
thousands: '.',
decimal: ',',
}
};
readableSize(999, options); // '999 B'
readableSize(1000, options); // '1.000 B'
readableSize(1024, options); // '1,00 KB'
```## Options
### separator
_*(boolean)*_ Enable separators, default is `false`
_*(object)*_ Specifies the thousands and decimal separators, default is:
```js
separator: {
thousands: ',',
decimal: '.'
}
```### output
_*('string')*_ The output is defined by the format string, default is `'{{size}} {{unit}}'`
_*('array')*_ The output is `[size, unit]`
_*('object')*_ The output is `{ size, unit }`
_*(function)*_ A user-defined output function:
```js
format: ({ size, unit }) => {
unit = { 'B': 'bytes' }[unit] || unit;
return `${size} ${unit}`;
}
```### format
_*(string)*_ The format string, default is `'{{size}} {{unit}}'`
## License
MIT