11import { UndirectedGraph } from "graphology" ;
22import { bidirectional } from 'graphology-shortest-path/unweighted' ;
3+ import { indexToColumnLabel , columnLabelToIndex } from "../columnLabels" ;
34import { IGraph } from "./IGraph" ;
45
5- const columnLabels = "abcdefghijklmnopqrstuvwxyz" . split ( "" ) ;
6-
76type Edge = "N" | "NE" | "SE" | "S" | "SW" | "NW" ;
87
98export class HexMoonGraph implements IGraph {
@@ -35,7 +34,7 @@ export class HexMoonGraph implements IGraph {
3534 return `e${ x + 1 } ` ;
3635 }
3736 } else {
38- return columnLabels [ this . height - y - 1 ] + ( x + 1 ) . toString ( ) ;
37+ return indexToColumnLabel ( this . height - y - 1 ) + ( x + 1 ) . toString ( ) ;
3938 }
4039 }
4140
@@ -53,28 +52,28 @@ export class HexMoonGraph implements IGraph {
5352 throw new Error ( "Unrecognized cell suffix." ) ;
5453 }
5554 } else {
56- const num = ( pair . slice ( 1 ) ) . join ( "" ) ;
57- const x = Number ( num ) ;
58- if ( ( x === undefined ) || ( isNaN ( x ) ) || num === "" ) {
59- throw new Error ( `The column label is invalid: ${ num } ` ) ;
55+ const match = cell . match ( / ^ ( [ a - z ] + ) ( \d + ) $ / ) ;
56+ if ( match === null ) {
57+ throw new Error ( `The algebraic notation is invalid: ${ cell } ` ) ;
6058 }
61- const y = columnLabels . indexOf ( pair [ 0 ] ) ;
62- if ( ( y === undefined ) || ( y < 0 ) ) {
63- throw new Error ( `The row label is invalid: ${ pair [ 0 ] } ` ) ;
59+ const x = Number ( match [ 2 ] ) ;
60+ if ( isNaN ( x ) ) {
61+ throw new Error ( `The column label is invalid: ${ match [ 2 ] } ` ) ;
6462 }
63+ const rowIdx = columnLabelToIndex ( match [ 1 ] ) ;
6564
6665 const midrow = Math . floor ( this . height / 2 ) ;
6766 const delta = this . maxwidth - this . minwidth ;
68- const rowWidth = this . minwidth + ( midrow - Math . abs ( delta - y ) ) ;
69- if ( ( x < 0 ) || ( x > rowWidth ) ) {
70- throw new Error ( `The column label is invalid: ${ num } ` ) ;
67+ const rowWidth = this . minwidth + ( midrow - Math . abs ( delta - rowIdx ) ) ;
68+ if ( ( x < 1 ) || ( x > rowWidth ) ) {
69+ throw new Error ( `The column label is invalid: ${ match [ 2 ] } ` ) ;
7170 }
7271
73- if ( pair [ 0 ] === "e" && x > 5 ) {
74- return [ x + 1 , this . height - y - 1 ] ;
75- } else {
76- return [ x - 1 , this . height - y - 1 ] ;
72+ const rowLetter = indexToColumnLabel ( rowIdx ) ;
73+ if ( rowLetter === "e" && x > 5 ) {
74+ return [ x , this . height - rowIdx - 1 ] ;
7775 }
76+ return [ x - 1 , this . height - rowIdx - 1 ] ;
7877 }
7978 }
8079
0 commit comments