better-bytes
Better byte base conversion. Supports two base conversions: kilo binary byte (2^10) and kilobyte (10^3).
Reference link: https://physics.nist.gov/cuu/Units/binary.html
更好的字节进制换算。支持 千位二进制字节(2^10) 和 千字节(10^3) 两种进制换算
参考链接:https://physics.nist.gov/cuu/Units/binary.html
🏠 Home/主页
https://github.com/CaoMeiYouRen/better-bytes#readme
📦 Dependency Requirements/依赖要求
🚀 Installation/安装
npm install better-bytes
👨💻 Usage/使用
import { format, parse } from 'better-bytes'
format(1024)
format(1073741824)
format(1125899906842624n)
format(1572864, { standard: 'kilobinary' })
format(1500000, { standard: 'kilo' })
format(1234, { decimal: 3, standard: 'kilobinary' })
format(1234, { decimal: 3, standard: 'kilo' })
format(-1)
format('invalid')
format(NaN)
parse('1KiB')
parse('1GiB')
parse('1KB')
parse('1GB')
parse('1KB', { forceKilobinary: true })
parse('1GB', { forceKilobinary: true })
parse('1.5KiB')
parse('1.5KB')
parse('1024')
parse('1000.5')
parse('1.123456789012345GiB')
parse('1.123456789012345GB')
parse('9007199254740991YiB')
parse('9007199254740991YB')
parse('+1KiB')
parse('-1KiB')
parse('1XB')
parse('invalid')
parse('Infinity')
parse(123)
format(data: number | bigint, options: Options = {}): string
Format the given value in bytes into a string.
将给定的字节值格式化为字符串。
Arguments
Name | Type | Description |
---|
data | number |bigint | Number value to format. 要格式化的数值 |
options | object | Conversion options for format . 格式化的选项。 |
Options
Property | Type | Description |
---|
decimal | number |undefined | Maximum number of decimal places to include in output. Default: 2 . 输出中包含的最大小数位数。默认值:2 。 |
standard | kilobinary |kilo | base. kilobinary = 2^10 ; kilo = 10^3. Default: kilobinary . 进制规范。千位二进制=2^10;千位=10^3。默认值:kilobinary |
unitSeparator | string |undefined | Separator to use between number and unit. Default: ' ' . 用于数字和单位之间的分隔符。默认值:' ' |
Returns
Name | Type | Description |
---|
results | string | Return string. For non number or bigint, as well as cases less than 0, exceptions are thrown. 返回 string。对于非 number 或 bigint,以及小于 0 的情况,均抛出异常。 |
parse(data: string): number | bigint | null
Parse the string value into an integer in bytes. If no unit is given, it is assumed the value is in bytes.
将字符串值解析为以字节为单位的整数。如果没有给出单位,则假定该值以字节为单位。
Arguments
Name | Type | Description |
---|
data | string | String value to parse. 要解析的字符串。 |
options | object | Parsed options. 解析的选项。 |
Options
Property | Type | Description |
---|
forceKilobinary | boolean |undefined | If true, consider kilo as kilobinary, i.e. using 2 ^ 10 base. 如果为真,则将千位进制视为千位二进制,即使用 2^10 进制换算。 |
Returns
Name | Type | Description |
---|
results | number |bigint |null | Returns null on error. Otherwise, return the value of number or bigint in bytes. 出错时返回 null。否则以字节为单位返回 number 或 bigint 的数值。 |
🛠️ Development/开发
npm run dev
🔧 Build/编译
npm run build
🧪 Test/测试
npm run test
🔍 Lint
npm run lint
💾 Commit
npm run commit
👤 Author/作者
CaoMeiYouRen
🤝 Contribution/贡献
Welcome to contribute, ask questions or propose new features!
If you have any questions, please check the issues page.
For contributions or new feature proposals, please refer to the contributing guide.
欢迎 贡献、提问或提出新功能!
如有问题请查看 issues page.
贡献或提出新功能可以查看contributing guide.
💰 Support/支持
If you find this project useful, please give it a ⭐️. Thank you very much.
如果觉得这个项目有用的话请给一颗⭐️,非常感谢
📝 License
Copyright © 2024 CaoMeiYouRen.
This project is MIT licensed.
This README was generated with ❤️ by cmyr-template-cli