-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hex): the equals() function and hex method can now compare diffe…
…rent kinds of hex coordinates equals() no longer requires both parameters to be the same kind of hex coordinates (axial or offset)
- Loading branch information
1 parent
ca41673
commit 5dc14d0
Showing
4 changed files
with
49 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,20 @@ | ||
import { equals } from './equals' | ||
|
||
test('returns whether the 2 passed offset coordinates are the same', () => { | ||
expect(equals({ col: 1, row: 2 }, { col: 1, row: 2 })).toBe(true) | ||
expect(equals({ col: 1, row: 2 }, { col: 3, row: 4 })).toBe(false) | ||
}) | ||
|
||
test('throws when offset coordinates and non-offset coordinates are passed', () => { | ||
expect(() => equals({ col: 1, row: 2 }, { q: 1, r: 2 } as never)).toThrowError( | ||
`Can't compare coordinates where one are offset coordinates. Either pass two offset coordinates or two axial/cube coordinates. Received: {"col":1,"row":2} and {"q":1,"r":2}`, | ||
) | ||
}) | ||
|
||
test('returns whether the 2 passed axial coordinates are the same', () => { | ||
expect(equals({ q: 1, r: 2 }, { q: 1, r: 2 })).toBe(true) | ||
expect(equals({ q: 1, r: 2 }, { q: 3, r: 4 })).toBe(false) | ||
|
||
expect(equals([1, 2], [1, 2])).toBe(true) | ||
expect(equals([1, 2], [3, 4])).toBe(false) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
import { isAxial, isTuple } from '../../utils' | ||
import { AxialCoordinates, HexCoordinates, OffsetCoordinates, TupleCoordinates } from '../types' | ||
import { isOffset, isTuple, tupleToCube } from '../../utils' | ||
import { CubeCoordinates, HexCoordinates, OffsetCoordinates } from '../types' | ||
|
||
// todo: a and b shouldn't have to be the same kind of coordinates | ||
export const equals = (a: HexCoordinates, b: HexCoordinates) => | ||
// when the 2nd coordinates is axial, assume the first is too | ||
// when equals() is used as a hex method, the 1st coordinates is that of the hex itself which is always axial | ||
isAxial(b) | ||
? equalsAxial(a as AxialCoordinates, b) | ||
: isTuple(a) | ||
? equalsTuple(a, b as TupleCoordinates) | ||
: equalsOffset(a as OffsetCoordinates, b as OffsetCoordinates) | ||
export function equals( | ||
a: Exclude<HexCoordinates, OffsetCoordinates>, | ||
b: Exclude<HexCoordinates, OffsetCoordinates>, | ||
): boolean | ||
export function equals(a: OffsetCoordinates, b: OffsetCoordinates): boolean | ||
export function equals(a: HexCoordinates, b: HexCoordinates) { | ||
if (isOffset(a) && isOffset(b)) { | ||
return a.col === b.col && a.row === b.row | ||
} | ||
|
||
function equalsAxial(a: AxialCoordinates, b: AxialCoordinates) { | ||
return a.q === b.q && a.r === b.r | ||
} | ||
|
||
function equalsOffset(a: OffsetCoordinates, b: OffsetCoordinates) { | ||
return a.col === b.col && a.row === b.row | ||
} | ||
// can't use isOffset() because that also checks in the prototype chain and that would always return true for hexes | ||
if (Object.prototype.hasOwnProperty.call(a, 'col') || Object.prototype.hasOwnProperty.call(b, 'col')) { | ||
throw new Error( | ||
`Can't compare coordinates where one are offset coordinates. Either pass two offset coordinates or two axial/cube coordinates. Received: ${JSON.stringify( | ||
a, | ||
)} and ${JSON.stringify(b)}`, | ||
) | ||
} | ||
|
||
function equalsTuple(a: TupleCoordinates, b: TupleCoordinates) { | ||
return a[0] === b[0] && a[1] === b[1] | ||
const cubeA = (isTuple(a) ? tupleToCube(a) : a) as CubeCoordinates | ||
const cubeB = (isTuple(b) ? tupleToCube(b) : b) as CubeCoordinates | ||
return cubeA.q === cubeB.q && cubeA.r === cubeB.r | ||
} |