fmt-bytes
v1.0.2
Published
Utility to format/convert bytes.
Downloads
11
Readme
fmt-bytes
Utility to format/convert bytes.
Installation
Installation comes with flow and typescript definitions.
npm install fmt-bytes
Usage
import { format, toBytes } from "fmt-bytes";
toBytes(100, "MiB");
// 104857600
toBytes(100, "MB");
// 100000000
toBytes(100, "gb");
// TypeError: Unknown byte type: "gb"
// at expect_never (fmt-bytes/lib/index.js:55:11)
// at Object.toBytes (fmt-bytes/lib/index.js:50:20)
format(117203200);
// '117.2 MB'
format(117203200, { precision: 3 });
// '117.203 MB'
format(117203200, { useBinary: true });
// '111.8 MiB'
format(117203200, { useBinary: true, precision: 3 });
// '111.774 MiB'
format(117203200, { useBinary: true, precision: 0 });
// '112 MiB'
Types
Described in Typescript syntax.
export declare type ByteType =
| "B"
| "KiB"
| "MiB"
| "GiB"
| "TiB"
| "KB"
| "MB"
| "GB"
| "TB";
export declare enum ByteUnit {
B = 1,
KiB = 1024,
MiB = 1048576,
GiB = 1073741824,
TiB = 1099511627776,
KB = 1000,
MB = 1000000,
GB = 1000000000,
TB = 1000000000000,
}
export declare function format(
bytes: number,
options?: {
useBinary?: boolean;
precision?: number;
}
): string;
export declare function toBytes(b: number, unit: ByteType): number;