Registry / devops / human-unit

human-unit

JSON →
library2.0.1jsnpmunverified

A flexible unit formatter for human-readable file size, time, and custom units. Current stable version 2.0.1, shipped as ESM with TypeScript types. Differentiates by allowing per-unit factors and ceils for precise control over unit bumping, using mixed SI/IEC standards for file sizes. Suitable for formatting bytes, durations, or any custom unit system.

npm install human-unit
INSTALL
IMPORT
SIG · HUMAN-UNIT
H
human-unit
devopsjavascriptv2.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

humanUnit
import humanUnit from 'human-unit'
const humanUnit = require('human-unit')
Default export; ESM-only since v2, no CommonJS support.
sizePreset
import { sizePreset } from 'human-unit'
import sizePreset from 'human-unit'
Named export for the built-in file size preset.
humanUnit (CommonJS stub)
const humanUnit = require('human-unit').default
const humanUnit = require('human-unit')
If using CommonJS, you must access .default; but ESM is preferred.

Demonstrates default size formatting and custom time units with the humanUnit function.

import humanUnit, { sizePreset } from 'human-unit'; // Format file size const result1 = humanUnit(1024, sizePreset); console.log(result1); // { value: 1, unit: 'KB' } // Custom unit preset for time const timePreset = { factors: [1000, 60, 60, 24], units: ['mili', 'sec', 'min', 'hour', 'day'] }; const result2 = humanUnit(18000000, timePreset); console.log(result2); // { value: 5, unit: 'hour' } // Override unit target const result3 = humanUnit(1024 * 1024, { ...sizePreset, unit: 'MB' }); console.log(result3); // { value: 1, unit: 'GB' }
Debug
Known issues
breakingv2.0 switched to ESM-only; CommonJS require() no longer works as expected.
fix
Use import syntax or switch to dynamic import().
affects: >=2.0.0
breakingDefault export is the humanUnit function; named exports (like sizePreset) changed paths.
fix
Use `import humanUnit from 'human-unit'` and `import { sizePreset } from 'human-unit'`.
affects: >=2.0.0
gotchaFactors array length must match or exceed number of unit transitions. Mismatch may cause unexpected results.
fix
Ensure factors array has at least (units.length - 1) elements when using array form.
affects: >=1.0.0
gotchaWhen using `unit` option, the value will be converted to that base unit before formatting; may produce unexpected if unit is not in the units list.
fix
Ensure the `unit` option is one of the predefined units in the preset.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: humanUnit is not a function
Using CommonJS require creates an object with a default property; the default export isn't accessed.
fix
Use `import humanUnit from 'human-unit'` or `const humanUnit = require('human-unit').default`.
Error [ERR_REQUIRE_ESM]: require() of ES Module
human-unit v2 is ESM-only; require() is not supported.
fix
Use import syntax or set `type: 'module'` in package.json.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources
human-unit — npm install human-unit · libregistry