29 lines
425 B
TypeScript
29 lines
425 B
TypeScript
const units = {
|
|||
|
|
B: 1,
|
||
|
|
|
||
|
|
kB: 1000 ** 1,
|
||
|
|
KiB: 1024 ** 1,
|
||
|
|
|
||
|
|
MB: 1000 ** 2,
|
||
|
|
MiB: 1024 ** 2,
|
||
|
|
|
||
|
|
GB: 1000 ** 3,
|
||
|
|
GiB: 1024 ** 3,
|
||
|
|
|
||
|
|
TB: 1000 ** 4,
|
||
|
|
TiB: 1024 ** 4,
|
||
|
|
|
||
|
|
PB: 1000 ** 5,
|
||
|
|
PiB: 1024 ** 5,
|
||
|
|
} as const;
|
||
|
|
|
||
|
|
type Unit = keyof typeof units;
|
||
|
|
|
||
|
|
function convert(value: number, from: Unit, to: Unit): number {
|
||
|
|
return value * units[from] / units[to];
|
||
|
|
}
|
||
|
|
|
||
|
|
if (import.meta.main) {
|
||
|
|
console.log(convert(1, "MiB", "kB"));
|
||
|
|
}
|