@toolsbee/humanize
v0.1.0
Published
Tiny modern data formatter (Bytes, Numbers, Lists, Duration).
Downloads
102
Readme
@toolsbee/humanize
Humanize
Humanize is a tiny, modern data formatter library. It provides essential utilities for humanizing data without the bloat.
It focuses on:
- Zero Dependencies: Lightweight and tree-shakeable.
- Modern APIs: Leverages native
Intlfor robust internationalization (where applicable). - Speed: Optimized implementations (e.g., log-based byte calculation).
Early development. API stable for v0.1.0.
Maintained by toolsbee.com · https://www.toolsbee.com
Install
npm install @toolsbee/humanizeQuick Start
import { bytes, number, list, duration, ordinal } from "@toolsbee/humanize";
bytes(1500000);
// "1.43 MB"
number(2500000);
// "2.5M"
list(["Apple", "Banana", "Cherry"]);
// "Apple, Banana, and Cherry"
duration(3661000);
// "1h 1m"
duration(60000, { ago: true });
// "1m 0s ago"
ordinal(21);
// "21st"Core API
1. bytes(n: number, options?)
Convert bytes to a readable string with unit (B, KB, MB, GB, TB, etc.).
| Option | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| decimals | number | 2 | Number of decimal places. |
bytes(1024); // "1 KB"
bytes(1234, { decimals: 3 }); // "1.205 KB"
bytes(0); // "0 B"2. number(n: number)
Compact number formatting using Intl.NumberFormat.
number(1500); // "1.5K"
number(1000000); // "1M"3. list(items: string[])
Natural list formatting using Intl.ListFormat.
list(["A", "B"]); // "A and B"
list(["A", "B", "C"]); // "A, B, and C"4. duration(ms: number, options?)
Convert milliseconds to a human-readable duration string.
| Option | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| ago | boolean | false | Append " ago" suffix (ignored if "just now"). |
duration(500); // "just now"
duration(1000 * 60); // "1m 0s"
duration(1000 * 60 * 60 * 24); // "1d 0h"
duration(3600000, { ago: true }); // "1h 0m ago"5. ordinal(n: number)
Convert a number to its ordinal string representation (1st, 2nd, 3rd, etc.).
ordinal(1); // "1st"
ordinal(2); // "2nd"
ordinal(11); // "11th"
ordinal(21); // "21st"Browser Usage
<!doctype html>
<html>
<body>
<pre id="out">Loading…</pre>
<script type="module">
import { bytes, number, list, duration, ordinal } from "https://cdn.jsdelivr.net/npm/@toolsbee/humanize/dist/index.js";
document.getElementById("out").textContent = JSON.stringify(
{
file_size: bytes(15728640), // "15 MB"
views: number(1234567), // "1.2M"
authors: list(["John", "Jane"]), // "John and Jane"
time_passed: duration(3600000 * 2.5), // "2h 30m"
ranking: ordinal(3) // "3rd"
},
null,
2
);
</script>
</body>
</html>Design Goals
- Tiny: Minimal footprint, tree-shakeable.
- Modern: Uses native
IntlAPIs where possible. - Zero-Deps: No external dependencies.
- Strict: Built with strict TypeScript settings.
License
MIT
