bytes-to-human 1.1.2
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/LICENSE +21 -0
- package/README.md +14 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/package.json +18 -0
- package/src/index.ts +14 -0
- package/test/index.test.ts +74 -0
- package/tsconfig.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Drakos
|
|
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/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Bytes to Human
|
|
2
|
+
Convert bytes to human format like "1 KB". 0 dependencies. Easy to copy paste into new projects.
|
|
3
|
+
|
|
4
|
+
# How to install
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
pnpm install human-to-bytes
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
# How to use
|
|
11
|
+
```ts
|
|
12
|
+
formatBytes(1500, 1) // 1.5 KB
|
|
13
|
+
formatBytes(1048576) // 1MB
|
|
14
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,GAAE,MAAU,GAAG,MAAM,CAaxE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bytesToHuman = bytesToHuman;
|
|
4
|
+
function bytesToHuman(bytes, decimals = 1) {
|
|
5
|
+
if (!Number.isFinite(bytes) || bytes < 0) {
|
|
6
|
+
throw new RangeError(`Invalid byte value: ${bytes}`);
|
|
7
|
+
}
|
|
8
|
+
if (bytes === 0)
|
|
9
|
+
return "0 B";
|
|
10
|
+
const UNITS = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
|
|
11
|
+
const exponent = Math.min(Math.floor(Math.log2(bytes) / 10), UNITS.length - 1);
|
|
12
|
+
const value = bytes / (1024 ** exponent);
|
|
13
|
+
const formatted = Number(value.toFixed(decimals)).toString(); // strips trailing zeros
|
|
14
|
+
return `${formatted} ${UNITS[exponent]}`;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,oCAaC;AAbD,SAAgB,YAAY,CAAC,KAAa,EAAE,WAAmB,CAAC;IAC9D,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,UAAU,CAAC,uBAAuB,KAAK,EAAE,CAAC,CAAC;IACvD,CAAC;IAED,IAAI,KAAK,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAE9B,MAAM,KAAK,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAU,CAAC;IACjE,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAC/E,MAAM,KAAK,GAAG,KAAK,GAAG,CAAC,IAAI,IAAI,QAAQ,CAAC,CAAC;IACzC,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,wBAAwB;IAEtF,OAAO,GAAG,SAAS,IAAI,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC;AAC3C,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bytes-to-human",
|
|
3
|
+
"version": "1.1.2",
|
|
4
|
+
"description": "Convert bytes to human format like '1 KB'",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "vitest run"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"author": "Drakos",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"packageManager": "pnpm@10.32.1",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/node": "^25.8.0",
|
|
16
|
+
"vitest": "^4.1.6"
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function bytesToHuman(bytes: number, decimals: number = 1): string {
|
|
2
|
+
if (!Number.isFinite(bytes) || bytes < 0) {
|
|
3
|
+
throw new RangeError(`Invalid byte value: ${bytes}`);
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (bytes === 0) return "0 B";
|
|
7
|
+
|
|
8
|
+
const UNITS = ["B", "KB", "MB", "GB", "TB", "PB", "EB"] as const;
|
|
9
|
+
const exponent = Math.min(Math.floor(Math.log2(bytes) / 10), UNITS.length - 1);
|
|
10
|
+
const value = bytes / (1024 ** exponent);
|
|
11
|
+
const formatted = Number(value.toFixed(decimals)).toString(); // strips trailing zeros
|
|
12
|
+
|
|
13
|
+
return `${formatted} ${UNITS[exponent]}`;
|
|
14
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { bytesToHuman } from "../src/index";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
describe("bytesToHuman", () => {
|
|
5
|
+
it("formats zero bytes", () => {
|
|
6
|
+
expect(bytesToHuman(0)).toBe("0 B");
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("formats bytes", () => {
|
|
10
|
+
expect(bytesToHuman(500)).toBe("500 B");
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("formats kilobytes", () => {
|
|
14
|
+
expect(bytesToHuman(1024)).toBe("1 KB");
|
|
15
|
+
expect(bytesToHuman(2560)).toBe("2.5 KB");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("formats megabytes", () => {
|
|
19
|
+
expect(bytesToHuman(1048576)).toBe("1 MB");
|
|
20
|
+
expect(bytesToHuman(5242880)).toBe("5 MB");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("formats gigabytes", () => {
|
|
24
|
+
expect(bytesToHuman(1073741824)).toBe("1 GB");
|
|
25
|
+
expect(bytesToHuman(2147483648)).toBe("2 GB");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("formats terabytes", () => {
|
|
29
|
+
expect(bytesToHuman(1099511627776)).toBe("1 TB");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("formats petabytes", () => {
|
|
33
|
+
expect(bytesToHuman(1125899906842624)).toBe("1 PB");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("respects decimal precision", () => {
|
|
37
|
+
expect(bytesToHuman(1500, 0)).toBe("1 KB");
|
|
38
|
+
expect(bytesToHuman(1500, 1)).toBe("1.5 KB");
|
|
39
|
+
expect(bytesToHuman(1500, 2)).toBe("1.46 KB");
|
|
40
|
+
expect(bytesToHuman(1500, 3)).toBe("1.465 KB");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("strips trailing zeros", () => {
|
|
44
|
+
expect(bytesToHuman(1024, 2)).toBe("1 KB");
|
|
45
|
+
expect(bytesToHuman(2048, 1)).toBe("2 KB");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("throws on negative values", () => {
|
|
49
|
+
expect(() => bytesToHuman(-1)).toThrow(RangeError);
|
|
50
|
+
expect(() => bytesToHuman(-1)).toThrow("Invalid byte value: -1");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("throws on non-finite values", () => {
|
|
54
|
+
expect(() => bytesToHuman(Infinity)).toThrow(RangeError);
|
|
55
|
+
expect(() => bytesToHuman(NaN)).toThrow(RangeError);
|
|
56
|
+
expect(() => bytesToHuman(Infinity)).toThrow("Invalid byte value: Infinity");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("throws on non-numeric values", () => {
|
|
60
|
+
// biome-ignore lint/suspicious/noExplicitAny: off
|
|
61
|
+
expect(() => bytesToHuman("100" as any)).toThrow(RangeError);
|
|
62
|
+
// biome-ignore lint/suspicious/noExplicitAny: off
|
|
63
|
+
expect(() => bytesToHuman(null as any)).toThrow(RangeError);
|
|
64
|
+
// biome-ignore lint/suspicious/noExplicitAny: off
|
|
65
|
+
expect(() => bytesToHuman(undefined as any)).toThrow(RangeError);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("handles values just below unit thresholds", () => {
|
|
69
|
+
expect(bytesToHuman(1023)).toBe("1023 B");
|
|
70
|
+
expect(bytesToHuman(1023999)).toBe("1000 KB");
|
|
71
|
+
expect(bytesToHuman(1048575)).toBe("1024 KB");
|
|
72
|
+
expect(bytesToHuman(1048576)).toBe("1 MB");
|
|
73
|
+
});
|
|
74
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
// File Layout
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
|
|
8
|
+
// Environment Settings
|
|
9
|
+
// See also https://aka.ms/tsconfig/module
|
|
10
|
+
"module": "nodenext",
|
|
11
|
+
"target": "esnext",
|
|
12
|
+
// For nodejs:
|
|
13
|
+
"lib": ["esnext"],
|
|
14
|
+
"types": ["node"],
|
|
15
|
+
// and npm install -D @types/node
|
|
16
|
+
|
|
17
|
+
// Other Outputs
|
|
18
|
+
"sourceMap": true,
|
|
19
|
+
"declaration": true,
|
|
20
|
+
"declarationMap": true,
|
|
21
|
+
|
|
22
|
+
// Stricter Typechecking Options
|
|
23
|
+
"noUncheckedIndexedAccess": true,
|
|
24
|
+
"exactOptionalPropertyTypes": true,
|
|
25
|
+
|
|
26
|
+
// Style Options
|
|
27
|
+
// "noImplicitReturns": true,
|
|
28
|
+
// "noImplicitOverride": true,
|
|
29
|
+
// "noUnusedLocals": true,
|
|
30
|
+
// "noUnusedParameters": true,
|
|
31
|
+
// "noFallthroughCasesInSwitch": true,
|
|
32
|
+
// "noPropertyAccessFromIndexSignature": true,
|
|
33
|
+
|
|
34
|
+
// Recommended Options
|
|
35
|
+
"strict": true,
|
|
36
|
+
"jsx": "react-jsx",
|
|
37
|
+
// "verbatimModuleSyntax": true,
|
|
38
|
+
"isolatedModules": true,
|
|
39
|
+
"noUncheckedSideEffectImports": true,
|
|
40
|
+
"moduleDetection": "force",
|
|
41
|
+
"skipLibCheck": true,
|
|
42
|
+
},
|
|
43
|
+
"exclude": ["test"],
|
|
44
|
+
"include": ["src"]
|
|
45
|
+
}
|