Convert bytes to human readable strings and vice versa: 1024 → 1KB and 1KB → 1024
Installations
npm install @pantajoe/bytes
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
20.11.1
NPM Version
10.2.4
Score
72.4
Supply Chain
98.9
Quality
77.7
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (96.15%)
JavaScript (3.85%)
Developer
pantajoe
Download Statistics
Total Downloads
7,985
Last Day
15
Last Week
218
Last Month
691
Last Year
7,985
GitHub Statistics
9 Stars
24 Commits
1 Watching
3 Branches
1 Contributors
Bundle Size
3.27 kB
Minified
1.35 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.2
Package Id
@pantajoe/bytes@1.0.2
Unpacked Size
28.55 kB
Size
5.54 kB
File Count
8
NPM Version
10.2.4
Node Version
20.11.1
Publised On
27 Mar 2024
Total Downloads
Cumulative downloads
Total Downloads
7,985
Last day
-42.3%
15
Compared to previous day
Last week
32.9%
218
Compared to previous week
Last month
6.5%
691
Compared to previous month
Last year
0%
7,985
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
@pantajoe/bytes
Convert bytes to human readable strings and vice versa: 1024 → 1KB and 1KB → 1024
A tiny utility (4.96KB) to convert bytes to a formatted string and vice versa.
✨ Highlights
- Formatting: Convert bytes to a human-readable string (e.g.,
1KB
,2.5MB
,5GB
, etc.). - Parsing: Convert a human-readable string to bytes (e.g.,
1KB
->1024
,2.5MB
->2621440
,5GB
->5368709120
, etc.). - L10n: Supports localization (e.g.,
1.5KB
in German is1,5KB
) viaIntl.NumberFormat
- Opionated: The default format is
0.1
(e.g.,1.5KB
). You can customize the format. - Base 2: Uses base 2 for calculations (e.g.,
1KB = 1024 bytes
). (You can use base 10 if you prefer). - Tiny: Only 5KB in size.
- Zero Dependencies: No dependencies, just pure JavaScript.
Installation
1pnpm add @pantajoe/bytes 2 3#or 4npm install @pantajoe/bytes 5 6# or 7yarn add @pantajoe/bytes
Usage
Default Formatting/Parsing
1import { formatBytes, bytes } from '@pantajoe/bytes' 2 3console.log(formatBytes(123412341, { decimals: 2 })) // "117.70 MB" 4console.log(bytes('117.70 MB')) // 123417395.2 5 6// Localization support in both directions 7console.log(formatBytes(123412341, { decimals: 2, locale: 'de' })) // "117,70 MB" 8console.log(bytes('117,70 MB', { locale: 'de' })) // 123417395.2 9 10// Use a specified unit 11console.log(formatBytes(123412341, { decimals: 2, unit: 'KB' })) // "120,519.86 KB" 12 13// Use a long unit 14console.log(formatBytes(123412341, { decimals: 2, unit: 'GB', long: true })) // "0.11 Gigabytes" 15 16// Use a differnet base 17console.log(formatBytes(123412341, { decimals: 2, base: 10 })) // "123.41 MB"
Byte utilities
1import { megabytes, gigabytes, terabytes, petabytes, exabytes, yottabytes } from '@pantajoe/bytes' 2 3console.log(megabytes(1)) // 1048576 4console.log(gigabytes(1)) // 1073741824 5console.log(terabytes(1)) // 1099511627776 6console.log(petabytes(1)) // 1125899906842624 7console.log(exabytes(1)) // 1152921504606847000 8console.log(yottabytes(1)) // 1.2089258196146292e+24 9 10// Use a different base 11console.log(megabytes(1, { base: 10 })) // 1000000 12console.log(gigabytes(1, { base: 10 })) // 1000000000 13console.log(terabytes(1, { base: 10 })) // 1000000000000 14console.log(petabytes(1, { base: 10 })) // 1000000000000000 15console.log(exabytes(1, { base: 10 })) // 1000000000000000000 16console.log(yottabytes(1, { base: 10 })) // 1e+24
Custom Formatting/Parsing & utilities
@pantajoe/bytes
is a tiny utility that allows you to create your own utility functions
via createBytes
if you have preferences regarding default locale and base:
1// bytes.util.ts 2import { createBytes } from '@pantajoe/bytes' 3 4const { 5 formatBytes, 6 bytes, 7 megabytes, 8 gigabytes, 9 terabytes, 10 petabytes, 11 exabytes, 12 yottabytes 13} = createBytes({ locale: 'de', base: 10 })
1// main.ts 2import { formatBytes } from './bytes.util' 3 4formatBytes(123412341, { decimals: 2 }) // "123,41 MB"
No vulnerabilities found.
No security vulnerabilities found.