Skip to content

Commit

Permalink
fix(hex): createHex() now also accepts tuple coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
flauwekeul committed Apr 29, 2021
1 parent d333679 commit 8f5196e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/grid/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface Traverser<T extends Hex, R extends Iterable<T> = Iterable<T>> {
(cursor: T, getHex: (coordinates: HexCoordinates) => T): R
}

// todo: don't use this, just use a simple function type (e.g.: `(hex: T, grid: Grid<T>) => void`) at the places this is used
export interface Callback<T extends Hex, R> {
(hex: T, grid: Grid<T>): R
}
Expand Down
7 changes: 7 additions & 0 deletions src/hex/functions/createHex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ test('returns a new hex from the passed properties containing offset coordinates
expect(result).toMatchObject({ q: 0, r: 2, custom: 'B' })
})

test('returns a new hex from the passed tuple coordinates', () => {
const hexPrototype = createHexPrototype<CustomHex>()
const result = createHex(hexPrototype, [1, 2])

expect(result).toMatchObject({ q: 1, r: 2 })
})

test('returns a clone when a hex (instance) is passed', () => {
const hexPrototype = createHexPrototype<CustomHex>()
const hex = createHex(hexPrototype)
Expand Down
3 changes: 2 additions & 1 deletion src/hex/functions/createHex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isOffset } from '../../utils'
import { isOffset, isTuple, tupleToCube } from '../../utils'
import { Hex, HexCoordinates } from '../types'
import { isHex } from './isHex'
import { offsetToCube } from './offsetToCube'
Expand All @@ -14,5 +14,6 @@ export const createHex = <T extends Hex>(prototypeOrHex: T, props: Partial<T> |
return Object.assign(Object.create(prototypeOrHex), coordinates, otherProps)
}

props = isTuple(props) ? tupleToCube(props) : props
return Object.assign(Object.create(prototypeOrHex), props)
}
3 changes: 1 addition & 2 deletions src/hex/functions/createHexPrototype.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Ellipse } from '../../../dist'
import { BoundingBox, Hex, HexPrototype, Orientation } from '../types'
import { BoundingBox, Ellipse, Hex, HexPrototype, Orientation } from '../types'
import { cloneHex } from './cloneHex'
import { corners } from './corners'
import { createHex } from './createHex'
Expand Down

0 comments on commit 8f5196e

Please sign in to comment.