@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
89 lines • 2.67 kB
JavaScript
/**
* @module FileSize
*/
import { TO_BYTES } from "../../file-size/contracts/_module.js";
import {} from "../../serde/contracts/_module.js";
import {} from "../../utilities/_module.js";
/**
* The `FileSize` class is used for representing file size.
* `FileSize` class cannot be negative, if you pass negative number it will be converted to 0.
*
* IMPORT_PATH: `"@daiso-tech/core/file-size"`
* @group Implementations
*/
export class FileSize {
fileSizeInBytes;
static kbInBytes = 1000;
static mbInBytes = 1000 * FileSize.kbInBytes;
static gbInBytes = 1000 * FileSize.mbInBytes;
static tbInBytes = 1000 * FileSize.gbInBytes;
static pbInBytes = 1000 * FileSize.tbInBytes;
static deserialize(serializedValue) {
return new FileSize(serializedValue.fileSizeInBytes);
}
static fromBytes(bytes) {
return new FileSize(bytes);
}
static fromKiloBytes(kiloBytes) {
return new FileSize(kiloBytes * FileSize.kbInBytes);
}
static fromMegaBytes(megaBytes) {
return new FileSize(megaBytes * FileSize.mbInBytes);
}
static fromGigaBytes(gigaBytes) {
return new FileSize(gigaBytes * FileSize.gbInBytes);
}
static fromTeraBytes(teraBytes) {
return new FileSize(teraBytes * FileSize.tbInBytes);
}
static fromPetaBytes(petaBytes) {
return new FileSize(petaBytes * FileSize.pbInBytes);
}
constructor(fileSizeInBytes) {
this.fileSizeInBytes = fileSizeInBytes;
this.fileSizeInBytes = Math.max(0, this.fileSizeInBytes);
}
[TO_BYTES]() {
return this.fileSizeInBytes;
}
equals(value) {
return value[TO_BYTES]() === this.toBytes();
}
gt(value) {
return value[TO_BYTES]() < this.toBytes();
}
gte(value) {
return value[TO_BYTES]() <= this.toBytes();
}
lt(value) {
return value[TO_BYTES]() > this.toBytes();
}
lte(value) {
return value[TO_BYTES]() >= this.toBytes();
}
serialize() {
return {
version: "1",
fileSizeInBytes: this.fileSizeInBytes,
};
}
toBytes() {
return this[TO_BYTES]();
}
toKiloBytes() {
return Math.floor(this.toBytes() / FileSize.kbInBytes);
}
toMegaBytes() {
return Math.floor(this.toBytes() / FileSize.mbInBytes);
}
toGigaBytes() {
return Math.floor(this.toBytes() / FileSize.gbInBytes);
}
toTeraBytes() {
return Math.floor(this.toBytes() / FileSize.tbInBytes);
}
toPetaBytes() {
return Math.floor(this.toBytes() / FileSize.pbInBytes);
}
}
//# sourceMappingURL=file-size.js.map