storj/web/satellite/src/utils/bytesSize.ts
Egon Elbre f5ac00f909 web/satellite: switch to eslint, sass, bump deps
tslint has been deprecated so it's nice to switch to eslint.
Currently this uses a minimal eslint, to get things up and running.

node-sass requires C which does not work nicely on all platforms.

Change-Id: I3ca9adf2971475c009e541652b7637c18ad960f4
2021-08-03 15:56:33 +00:00

60 lines
1.6 KiB
TypeScript

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
enum Memory {
Bytes = 1e0,
KB = 1e3,
MB = 1e6,
GB = 1e9,
TB = 1e12,
PB = 1e15,
}
export enum Dimensions {
Bytes = 'Bytes',
KB = 'KB',
MB = 'MB',
GB = 'GB',
TB = 'TB',
PB = 'PB',
}
export class Size {
private readonly precision: number;
public readonly bytes: number;
public readonly formattedBytes: string;
public readonly label: Dimensions;
public constructor(bytes: number, precision = 0) {
const _bytes = Math.ceil(bytes);
this.bytes = bytes;
this.precision = precision;
switch (true) {
case _bytes === 0:
this.formattedBytes = (bytes / Memory.Bytes).toFixed(this.precision);
this.label = Dimensions.Bytes;
break;
case _bytes < Memory.MB:
this.formattedBytes = (bytes / Memory.KB).toFixed(this.precision);
this.label = Dimensions.KB;
break;
case _bytes < Memory.GB:
this.formattedBytes = (bytes / Memory.MB).toFixed(this.precision);
this.label = Dimensions.MB;
break;
case _bytes < Memory.TB:
this.formattedBytes = (bytes / Memory.GB).toFixed(this.precision);
this.label = Dimensions.GB;
break;
case _bytes < Memory.PB:
this.formattedBytes = (bytes / Memory.TB).toFixed(this.precision);
this.label = Dimensions.TB;
break;
default:
this.formattedBytes = (bytes / Memory.PB).toFixed(this.precision);
this.label = Dimensions.PB;
}
}
}