All files / src position.ts

96.36% Statements 53/55
93.75% Branches 15/16
100% Functions 9/9
96.36% Lines 53/55

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 551x 1x 1x 1x 1x 1x 244379x 244379x 244379x 1x 1x 76257x 76257x 1x 1x 74538x 74538x 1x 1x 3010x 3010x 1x 1x 5089x 5089x 1x 1x 2677x 2677x 1x 1x 2708x 2708x 1x 1x 1x 1x 1x 1x 1x 1x 1x 166x 166x 166x 166x 166x 1x 1x 7x     7x 7x 1x
// first position is (1,1)
export class Position {
  private readonly row: number;
  private readonly col: number;
 
  public constructor(row: number, col: number) {
    this.row = row;
    this.col = col;
  }
 
  public getCol(): number {
    return this.col;
  }
 
  public getRow(): number {
    return this.row;
  }
 
  public isAfter(p: Position): boolean {
    return this.row > p.row || (this.row === p.row && this.col >= p.col);
  }
 
  public equals(p: Position): boolean {
    return this.row === p.getRow() && this.col === p.getCol();
  }
 
  public isBefore(p: Position): boolean {
    return this.row < p.row || (this.row === p.row && this.col < p.col);
  }
 
  public isBetween(p1: Position, p2: Position): boolean {
    return this.isAfter(p1) && this.isBefore(p2);
  }
}
 
/** used for macro calls */
export class VirtualPosition extends Position {
  private readonly virtual: Position;
  public readonly vrow: number;
  public readonly vcol: number;
 
  public constructor(virtual: Position, row: number, col: number) {
    super(virtual.getRow(), virtual.getCol());
    this.virtual = virtual;
    this.vrow = row;
    this.vcol = col;
  }
 
  public equals(p: Position): boolean {
    if (!(p instanceof VirtualPosition)) {
      return false;
    }
    return super.equals(this.virtual) && this.vrow === p.vrow && this.vcol === p.vcol;
  }
}