All files / src/utils size.ts

80% Statements 8/10
0% Branches 0/1
83.33% Functions 5/6
80% Lines 8/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 4133x                 33x       212x   212x           187x       25x               118x       14x      
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 {
  private bytes: number
 
  private constructor(bytes: number) {
    this.bytes = Math.ceil(bytes)
 
    Iif (bytes < 0) {
      throw Error('Size must be at least 0')
    }
  }
 
  static fromBytes(bytes: number): Size {
    return new Size(bytes)
  }
 
  static fromGigabytes(gigabytes: number): Size {
    return new Size(gigabytes * 1000 * 1000 * 1000)
  }
 
  toBytes(): number {
    return this.bytes
  }
 
  toGigabytes(): number {
    return this.bytes / 1000 / 1000 / 1000
  }
 
  toFormattedString(): string {
    return Numbers.convertBytes(this.bytes, 1000)
  }
}