UNPKG

@ethersphere/bee-js

Version:
40 lines 960 B
import { Numbers } from 'cafe-utility'; /** * Represents a size in bytes. * * Uses 1000 instead of 1024 for converting between units. * This is to stay consistent with the Swarm papers * on theoretical and effective storage capacity. */ export class Size { constructor(bytes) { this.bytes = Math.ceil(bytes); if (bytes < 0) { throw Error('Size must be at least 0'); } } static fromBytes(bytes) { return new Size(bytes); } static fromKilobytes(kilobytes) { return new Size(kilobytes * 1000); } static fromMegabytes(megabytes) { return new Size(megabytes * 1000 * 1000); } static fromGigabytes(gigabytes) { return new Size(gigabytes * 1000 * 1000 * 1000); } toBytes() { return this.bytes; } toGigabytes() { return this.bytes / 1000 / 1000 / 1000; } toFormattedString() { return Numbers.convertBytes(this.bytes, 1000); } represent() { return this.toFormattedString(); } }