0.0.4 • Published 7 months ago
@iohub/sizeinfo v0.0.4
@iohub/sizeinfo
Precise byte-size formatting utility for Node.js and browsers
Features
- Precise byte-size formatting with decimal point control
- Support for both binary (1024) and decimal (1000) units
- Automatic scale detection from bytes to yottabytes
- Zero dependencies
- ES2015+ syntax
Installation
# Using npm
$ npm install @iohub/sizeinfo
# Using yarn
$ yarn add @iohub/sizeinfo
# Using pnpm
$ pnpm add @iohub/sizeinfoUsage
import { humanScale, base3ToScale, trailZero } from '@iohub/sizeinfo'
// humanScale(num, dec)
// Converts bytes to human-readable format
// @param {number} num - Size in bytes
// @param {boolean} dec - Use decimal (1000) instead of binary (1024)
// @returns {string} Formatted size with unit
humanScale(1500000, false)  // => "1.43 MB"
humanScale(1500000, true)   // => "1.50 M"
// base3ToScale(base3, dec)
// Gets the appropriate unit suffix for a given scale
// @param {number} base3 - Power of 1024/1000 (0-8)
// @param {boolean} dec - Use decimal units (K,M,G) instead of binary (KB,MB,GB)
// @returns {string} Unit suffix
base3ToScale(2, false)  // => "MB"
base3ToScale(2, true)   // => "M"
// trailZero(num)
// Formats a number with exactly two decimal places
// @param {number} num - Number to format
// @returns {string} Formatted number with two decimals
trailZero(1.5)    // => "1.50"
trailZero(1.567)  // => "1.57"License
MIT © Hoyeung Wong