All files / lib/domain Checksum.ts

100% Statements 11/11
100% Branches 0/0
100% Functions 7/7
100% Lines 11/11

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 391x         1x   7x       1x           8x       2x       1x       2x 2x 2x       1x      
import { crc32c } from '@node-dlc/checksum';
 
/**
 * CRC32C checksum for the provided value
 */
export class Checksum {
  public static fromBuffer(buf: Buffer): Checksum {
    return new Checksum(crc32c(buf));
  }
 
  public static empty(): Checksum {
    return new Checksum(0);
  }
 
  private _checksum: number;
 
  private constructor(checksum: number) {
    this._checksum = checksum;
  }
 
  public equals(other: Checksum): boolean {
    return this._checksum === other._checksum;
  }
 
  public toNumber(): number {
    return this._checksum;
  }
 
  public toBuffer(): Buffer {
    const buf = Buffer.alloc(4);
    buf.writeUInt32BE(this._checksum, 0);
    return buf;
  }
 
  public toString(): string {
    return this._checksum.toString(16);
  }
}