@ethersphere/bee-js
Version:
Javascript client for Bee
45 lines (44 loc) • 1.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Size = void 0;
const cafe_utility_1 = require("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.
*/
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 cafe_utility_1.Numbers.convertBytes(this.bytes, 1000);
}
represent() {
return this.toFormattedString();
}
}
exports.Size = Size;