-
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.
feat(hex): normalize and assert all required hex prototype properties…
… in createHexPrototype()
- Loading branch information
1 parent
4c08684
commit 9cf0511
Showing
6 changed files
with
95 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,83 @@ | ||
import { normalizeDimensions } from '../../utils' | ||
import { HexPrototype, Orientation } from '../types' | ||
import { isCartesian, isObject } from '../../utils' | ||
import { CartesianCoordinates, Ellipse, HexPrototype, Orientation, Rectangle } from '../types' | ||
|
||
export interface HexPrototypeOptions { | ||
dimensions: Ellipse | Rectangle | number | ||
orientation: Orientation | 'pointy' | 'flat' | ||
origin: CartesianCoordinates | number | ||
offset: number | ||
} | ||
|
||
export const defaultPrototype: HexPrototype = { | ||
dimensions: { xRadius: 1, yRadius: 1 }, | ||
orientation: Orientation.POINTY, | ||
origin: 0, | ||
// todo: why isn't this the center of the hex: | ||
origin: { x: 0, y: 0 }, | ||
offset: -1, | ||
} | ||
|
||
export const createHexPrototype = <T>(prototype: T & Partial<HexPrototype>) => { | ||
const normalizeDimensions = ({ dimensions, orientation }: HexPrototypeOptions) => { | ||
if (isObject(dimensions)) { | ||
if (Number.isFinite((dimensions as Ellipse).xRadius) && Number.isFinite((dimensions as Ellipse).yRadius)) { | ||
return { ...(dimensions as Ellipse) } | ||
} | ||
|
||
const { width, height } = dimensions as Rectangle | ||
if (Number.isFinite(width) && Number.isFinite(height)) { | ||
return orientation === Orientation.POINTY | ||
? { xRadius: width / Math.sqrt(3), yRadius: height / 2 } | ||
: { xRadius: width / 2, yRadius: height / Math.sqrt(3) } | ||
} | ||
} | ||
|
||
if (Number.isFinite(dimensions as number)) { | ||
return { xRadius: dimensions, yRadius: dimensions } as Ellipse | ||
} | ||
|
||
throw new TypeError( | ||
`Invalid dimensions: ${dimensions}. Dimensions must be expressed as an Ellipse ({ xRadius: number, yRadius: number }), a Rectangle ({ width: number, height: number }) or a number.`, | ||
) | ||
} | ||
|
||
const normalizeOrientation = ({ orientation }: HexPrototypeOptions) => { | ||
orientation = orientation.toUpperCase() as Orientation | ||
|
||
if (orientation === Orientation.POINTY || orientation === Orientation.FLAT) { | ||
return orientation | ||
} | ||
|
||
throw new TypeError(`Invalid orientation: ${orientation}. Orientation must be either 'POINTY' or 'FLAT'.`) | ||
} | ||
|
||
const normalizeOrigin = ({ origin }: HexPrototypeOptions) => { | ||
if (isCartesian(origin)) { | ||
return { ...origin } as CartesianCoordinates | ||
} | ||
|
||
if (Number.isFinite(origin)) { | ||
return { x: origin, y: origin } as CartesianCoordinates | ||
} | ||
|
||
throw new TypeError( | ||
`Invalid origin: ${origin}. Origin must be expressed as CartesianCoordinates ({ x: number, y: number }), or a number.`, | ||
) | ||
} | ||
|
||
const assertOffset = ({ offset }: HexPrototypeOptions) => { | ||
if (!Number.isFinite(offset)) { | ||
throw new TypeError(`Invalid offset: ${offset}. Offset must be a number.`) | ||
} | ||
|
||
return offset | ||
} | ||
|
||
export const createHexPrototype = <T>(prototype: T & Partial<HexPrototypeOptions>) => { | ||
const finalPrototype = { ...defaultPrototype, ...prototype } as T & HexPrototype | ||
|
||
finalPrototype.dimensions = normalizeDimensions(finalPrototype) | ||
finalPrototype.orientation = normalizeOrientation(finalPrototype) | ||
finalPrototype.origin = normalizeOrigin(finalPrototype) | ||
finalPrototype.offset = assertOffset(finalPrototype) | ||
|
||
return finalPrototype | ||
} |
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,4 +1,5 @@ | ||
export * from './isCartesian' | ||
export * from './isCube' | ||
export * from './isObject' | ||
export * from './normalizeDimensions' | ||
export * from './offsetFromZero' | ||
export * from './signedModulo' |
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,5 @@ | ||
import { CartesianCoordinates } from '../hex' | ||
import { isObject } from './isObject' | ||
|
||
export const isCartesian = (value: unknown): value is CartesianCoordinates => | ||
isObject<CartesianCoordinates>(value) && Number.isFinite(value.x) && Number.isFinite(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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { CubeCoordinates } from '../hex' | ||
import { isObject } from './isObject' | ||
|
||
export const isCube = (value: unknown): value is CubeCoordinates => | ||
isObject<CubeCoordinates>(value) && | ||
Number.isFinite(value.q) && | ||
Number.isFinite(value.r) && | ||
Number.isFinite(value.s) && | ||
// todo: not sure if this is necessary | ||
value.q + value.r + value.s === 0 |
This file was deleted.
Oops, something went wrong.