storj/web/satellite/src/utils/bytesSize.ts
Vitalii Shpital be10ce84f8 web/satellite: implemented charts UI for new project dashboard
Added bandwidth/storage charts (with test data) to new project dashboard.
Added functional buttons to new project dashboard.
Fixed this issue https://github.com/storj/storj/issues/4262.

Change-Id: Ie87370b8f7b6015bc84022a6086ef1db40e16535
2021-12-01 15:55:20 +00:00

60 lines
1.6 KiB
TypeScript

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
export enum Memory {
Bytes = 1e0,
KB = 1e3,
MB = 1e6,
GB = 1e9,
TB = 1e12,
PB = 1e15,
}
export enum Dimensions {
Bytes = 'B',
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;
}
}
}