-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename assertCubeCoordinates() to toCube() and completeCubeCoordinates() to completeCube() and add tests. Also add isNumber() util.
- Loading branch information
1 parent
b1f8cae
commit 6fc63cb
Showing
21 changed files
with
109 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { assertCubeCoordinates, Hex, HexCoordinates } from '../../hex' | ||
import { Hex, HexCoordinates, toCube } from '../../hex' | ||
|
||
export function distance(hex: Pick<Hex, 'offset' | 'isPointy'>, from: HexCoordinates, to: HexCoordinates) { | ||
const { q: fromQ, r: fromR, s: fromS } = assertCubeCoordinates(hex, from) | ||
const { q: toQ, r: toR, s: toS } = assertCubeCoordinates(hex, to) | ||
const { q: fromQ, r: fromR, s: fromS } = toCube(hex, from) | ||
const { q: toQ, r: toR, s: toS } = toCube(hex, to) | ||
return Math.max(Math.abs(fromQ - toQ), Math.abs(fromR - toR), Math.abs(fromS - toS)) | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect, test } from 'vitest' | ||
import { completeCube } from './completeCube' | ||
|
||
test('returns complete cube coordinates', () => { | ||
expect(completeCube({ q: 1, r: 2, s: -3 })).toEqual({ q: 1, r: 2, s: -3 }) | ||
}) | ||
|
||
test('converts partial cube coordinates to complete cube coordinates', () => { | ||
expect(completeCube({ q: 1, r: 2 })).toEqual({ q: 1, r: 2, s: -3 }) | ||
expect(completeCube({ q: 1, s: 2 })).toEqual({ q: 1, r: -3, s: 2 }) | ||
expect(completeCube({ r: 1, s: 2 })).toEqual({ q: -3, r: 1, s: 2 }) | ||
}) | ||
|
||
test('throws when passed less than 2 coordinates', () => { | ||
// @ts-expect-error | ||
expect(() => completeCube({ q: 1 })).toThrowError( | ||
TypeError( | ||
`Can't determine three cube coordinates from less than two coordinates. Received: { q: 1, r: undefined, s: undefined }.`, | ||
), | ||
) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { isNumber } from '../../utils' | ||
import { CubeCoordinates, PartialCubeCoordinates } from '../types' | ||
|
||
/** | ||
* @category Coordinates | ||
*/ | ||
export function completeCube({ q, r, s }: PartialCubeCoordinates): CubeCoordinates { | ||
const validQ = isNumber(q) | ||
const validR = isNumber(r) | ||
const validS = isNumber(s) | ||
|
||
if (validQ && validR && validS) return { q, r, s } | ||
|
||
if (validQ && validR) return { q, r, s: -q - r } | ||
|
||
if (validQ && validS) return { q, r: -q - s, s } | ||
|
||
if (validR && validS) return { q: -r - s, r, s } | ||
|
||
throw new TypeError( | ||
`Can't determine three cube coordinates from less than two coordinates. Received: { q: ${q}, r: ${r}, s: ${s} }.`, | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect, test } from 'vitest' | ||
import { defineHex } from './defineHex' | ||
import { toCube } from './toCube' | ||
|
||
const Hex = defineHex() | ||
const hex = new Hex() | ||
|
||
test('converts tuple coordinates to cube coordinates', () => { | ||
expect(toCube(hex, [1, 2])).toEqual({ q: 1, r: 2, s: -3 }) | ||
expect(toCube(hex, [0, 2, -2])).toEqual({ q: 0, r: 2, s: -2 }) | ||
}) | ||
|
||
test('converts offset coordinates to cube coordinates', () => { | ||
expect(toCube(hex, { col: 1, row: 2 })).toEqual({ q: 0, r: 2, s: -2 }) | ||
}) | ||
|
||
test('converts partial cube coordinates to cube coordinates', () => { | ||
expect(toCube(hex, { q: 1, r: 2 })).toEqual({ q: 1, r: 2, s: -3 }) | ||
expect(toCube(hex, { q: 1, s: 2 })).toEqual({ q: 1, r: -3, s: 2 }) | ||
expect(toCube(hex, { r: 1, s: 2 })).toEqual({ q: -3, r: 1, s: 2 }) | ||
}) |
15 changes: 6 additions & 9 deletions
15
src/hex/functions/assertCubeCoordinates.ts → src/hex/functions/toCube.ts
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,21 +1,18 @@ | ||
import { isOffset, isTuple, tupleToCube } from '../../utils' | ||
import { Hex } from '../hex' | ||
import { CubeCoordinates, HexCoordinates } from '../types' | ||
import { completeCubeCoordinates } from './completeCubeCoordinates' | ||
import { completeCube } from './completeCube' | ||
import { offsetToCube } from './offsetToCube' | ||
|
||
/** | ||
* Util for converting offset/axial/cube/tuple coordinates to cube coordinates. | ||
* @category Coordinates | ||
* @privateRemarks It's not placed in /src/utils because that causes circular dependencies. | ||
*/ | ||
export function assertCubeCoordinates( | ||
hex: Pick<Hex, 'offset' | 'isPointy'>, | ||
coordinates: HexCoordinates, | ||
): CubeCoordinates { | ||
return isOffset(coordinates) | ||
? offsetToCube(hex, coordinates) | ||
: isTuple(coordinates) | ||
export function toCube(hex: Pick<Hex, 'offset' | 'isPointy'>, coordinates: HexCoordinates): CubeCoordinates { | ||
return isTuple(coordinates) | ||
? tupleToCube(coordinates) | ||
: completeCubeCoordinates(coordinates) | ||
: isOffset(coordinates) | ||
? offsetToCube(hex, coordinates) | ||
: completeCube(coordinates) | ||
} |
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
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,8 +1,9 @@ | ||
import { AxialCoordinates } from '../hex' | ||
import { isNumber } from './isNumber' | ||
import { isObject } from './isObject' | ||
|
||
/** | ||
* @category Coordinates | ||
*/ | ||
export const isAxial = (value: unknown): value is AxialCoordinates => | ||
isObject<AxialCoordinates>(value) && Number.isFinite(value.q) && Number.isFinite(value.r) | ||
isObject<AxialCoordinates>(value) && isNumber(value.q) && isNumber(value.r) |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const isNumber = (value: unknown): value is number => Number.isFinite(value) && !Number.isNaN(value) |
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,8 +1,9 @@ | ||
import { OffsetCoordinates } from '../hex' | ||
import { isNumber } from './isNumber' | ||
import { isObject } from './isObject' | ||
|
||
/** | ||
* @category Coordinates | ||
*/ | ||
export const isOffset = (value: unknown): value is OffsetCoordinates => | ||
isObject<OffsetCoordinates>(value) && Number.isFinite(value.col) && Number.isFinite(value.row) | ||
isObject<OffsetCoordinates>(value) && isNumber(value.col) && isNumber(value.row) |
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,5 +1,6 @@ | ||
import { Point } from '../hex' | ||
import { isNumber } from './isNumber' | ||
import { isObject } from './isObject' | ||
|
||
export const isPoint = (value: unknown): value is Point => | ||
isObject<Point>(value) && Number.isFinite(value.x) && Number.isFinite(value.y) | ||
isObject<Point>(value) && isNumber(value.x) && isNumber(value.y) |
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,7 +1,8 @@ | ||
import { TupleCoordinates } from '../hex' | ||
import { isNumber } from './isNumber' | ||
|
||
/** | ||
* @category Coordinates | ||
*/ | ||
export const isTuple = (value: unknown): value is TupleCoordinates => | ||
Array.isArray(value) && Number.isFinite(value[0]) && Number.isFinite(value[1]) | ||
Array.isArray(value) && isNumber(value[0]) && isNumber(value[1]) |