bytes-iec is a JavaScript and TypeScript utility library, currently at version 3.1.1, designed for parsing and formatting byte size strings. It converts human-readable strings like '1kB' or '2KiB' into their numeric byte equivalents (e.g., 1000, 2048) and vice-versa. As a fork of the popular `bytes` module, `bytes-iec` differentiates itself by offering comprehensive support for ISO/IEC 80000-13:2008 binary and decimal prefixes, including KiB, MiB, etc., and also includes a JEDEC compatibility mode for legacy definitions where metric units have binary values. A key feature is its default use of decimal metric units, which can be configured globally or per-call. The library is actively maintained, indicated by its recent version and clear feature set, and ships with TypeScript definitions, making it suitable for modern JavaScript and TypeScript projects in Node.js environments.
npm install bytes-iecVerified import paths — ran on the pinned version, not inferred.
Demonstrates formatting byte values into human-readable strings and parsing strings back into numeric bytes, showcasing default decimal mode, explicit binary (IEC) mode, and JEDEC compatibility mode.
To achieve binary (base-2) behavior, explicitly set the `mode` option to `'binary'` for IEC units (e.g., 'KiB', 'MiB') or `'jedec'` for compatibility mode where metric abbreviations ('kB', 'MB') represent binary values. Example: `bytes.format(1048576, { mode: 'binary' });` or `bytes.parse('1MB', { mode: 'jedec' });`For consistent binary prefix behavior across all magnitudes, prefer using `mode: 'binary'` which uses the standard IEC prefixes (KiB, MiB, GiB, etc.).
Be aware that fractional inputs are truncated. If precise fractional byte handling is needed, ensure input values are already in base bytes or handle rounding before parsing.
Ensure you are using `import bytes from 'bytes-iec';` for ESM or `const bytes = require('bytes-iec');` for CommonJS, as the library exports a default object containing the `format` and `parse` methods.Change the import statement to `import bytes from 'bytes-iec';` to correctly import the default export.
No dependency data recorded yet.