name:
src/main/js/models/common/ColumnRowKey.ts
-rw-r--r--
1215
1import { AlphaUtils } from "../../utils/AlphaUtils";
2import { Compare } from "../../utils/Compare";
3import { A1Key } from "./A1Key";
4import { Hashable } from "../../common/standard/Hashable";
5
6/**
7 * Represents a single location in a 2D grid - usually {@link Grid}.
8 */
9export class ColumnRowKey implements Hashable {
10 readonly column: number;
11 readonly row: number;
12
13 constructor(column: number, row: number) {
14 this.column = column;
15 this.row = row;
16 }
17
18 get hashKey(): string {
19 return JSON.stringify([this.column, this.row]);
20 }
21
22 static fromA1(a1: string) {
23 const key = A1Key.fromString(a1);
24 return new ColumnRowKey(key.getColumnIndex(), key.row);
25 }
26
27 toA1(): string {
28 return `${AlphaUtils.oneIndexedNumberToLetter(this.column + 1)}${this.row + 1}`;
29 }
30
31 compareTo(other: ColumnRowKey): number {
32 if (this.row === other.row && this.column === other.column) {
33 return 0;
34 }
35 if (this.column === other.column) {
36 return Compare.numberComparison(this.row, other.row);
37 }
38 return Compare.numberComparison(this.column, other.column);
39 }
40
41 equals(other: unknown): boolean {
42 return other instanceof ColumnRowKey && this.hashKey === other.hashKey;
43 }
44}