字节效用
用于解析字符串字节的实用程序(例如: 1TB)到字节(1099511627776),反之亦然。
安装
这是一个 node . js 模块可通过
npm注册表. 安装是使用
npm install 命令:
$ npm install bytes
用法
var bytes = require('bytes');
Bytes (number|string value, [options]): number|string|null
默认导出功能。委托给任一方 bytes.format 或 bytes.parse 基于类型 value。
参数
| 名字 | 类型 | 描述 |
|---|---|---|
| 价值 | number|string | 要格式化的数值或要解析的字符串值 |
| 选项 | Object | 转换选项 format |
返回
| 名字 | 类型 | 描述 |
|---|---|---|
| 结果 | string|number|null | 错误时返回null。以字节为单位的数字值,否则为字符串值。 |
例子
bytes(1024);
// output: '1KB'
bytes('1KB');
// output: 1024
字节。格式(数值,[options]): string|null
将给定的值以字节为单位格式化为字符串。如果值为负,则保持为负。如果它是浮点数,它就是 圆。
参数
| 名字 | 类型 | 描述 |
|---|---|---|
| 价值 | number | 以字节为单位的值 |
| 选项 | Object | 转换选项 |
选项
| 财产 | 类型 | 描述 |
|---|---|---|
| decimalPlaces | number|null | 输出中要包含的最大小数位数。默认值为 2。 |
| fixedDecimals | boolean|null | 是否始终显示小数点后位数的最大值。默认值为 false |
| thousandsSeparator | string|null | 值的例子: ' ', ',' 和 '.'…默认值为 ''。 |
| 单位 | string|null | 返回结果的单位(B/KB/MB/GB/TB)。默认值为 '' (意思是自动检测)。 |
| unitSeparator | string|null | 数字和单位之间使用的分隔符。默认值为 ''。 |
返回
| 名字 | 类型 | 描述 |
|---|---|---|
| 结果 | string|null | 错误时返回null。否则为字符串值。 |
例子
bytes.format(1024);
// output: '1KB'
bytes.format(1000);
// output: '1000B'
bytes.format(1000, {thousandsSeparator: ' '});
// output: '1 000B'
bytes.format(1024 * 1.7, {decimalPlaces: 0});
// output: '2KB'
bytes.format(1024, {unitSeparator: ' '});
// output: '1 KB'
字节。解析(string|number value): number|null
将字符串值解析为以字节为单位的整数。如果未给出单位,则为 value
是一个数字,则假定该值以字节为单位。
支持的单位和缩写如下,不区分大小写:
b为字节kb为千字节mb为字节gb为字节tb为字节pb对pb
单位是2的幂,不是10。根据这个解析器,这意味着1kb = 1024b。
参数
| 名字 | 类型 | 描述 |
|---|---|---|
| 价值 | string|number | 要解析的字符串,或以字节为单位的数字。 |
返回
| 名字 | 类型 | 描述 |
|---|---|---|
| 结果 | number|null | 错误时返回null。否则以字节为单位。 |
例子
bytes.parse('1KB');
// output: 1024
bytes.parse('1024');
// output: 1024
bytes.parse(1024);
// output: 1024