-
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(grid): update rectangle() to behave as most traversers should
By default, rectangle() leaves out its start hex. When a `start` coordinate is passed it starts and includes that hex, when `at` is passed it starts but doesn't include that hex. The overload where 2 opposing corners can be passed (`rectangle(cornerA, cornerB)`) by default *does* include cornerA. When `false` is passed as a 3rd argument, cornerA isn't included. BREAKING CHANGE: When rectangle() isn't passed `start` coordinates it now excludes its first hex (the cursor, usually `{ q: 0, r: 0 }`). In other words: you probably want to pass `start` coordinates when using rectangle()
- Loading branch information
1 parent
5205e93
commit 5415029
Showing
3 changed files
with
94 additions
and
14 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,5 +1,69 @@ | ||
test.todo( | ||
'returns a traverser that produces hexes in a rectangular shape with the passed width, height, start and direction', | ||
) | ||
import { CompassDirection } from '../../compass' | ||
import { createHex, createHexPrototype } from '../../hex' | ||
import { rectangle } from './rectangle' | ||
|
||
test.todo('returns a traverser that produces hexes between the passed opposing corners of a rectangle') | ||
const hexPrototype = createHexPrototype() | ||
const cursor = createHex(hexPrototype, { q: 1, r: 2 }) | ||
const getHex = jest.fn((coordinates) => createHex(hexPrototype, coordinates)) | ||
|
||
describe('when only passed width and height', () => { | ||
test('returns a traverser that returns hexes in a rectangular shape without the cursor', () => { | ||
expect(rectangle({ width: 2, height: 2 })(cursor, getHex)).toMatchObject([ | ||
{ q: 2, r: 2 }, | ||
{ q: 1, r: 3 }, | ||
{ q: 2, r: 3 }, | ||
]) | ||
}) | ||
}) | ||
|
||
describe('when passed at', () => { | ||
test('returns a traverser that returns hexes in a rectangular shape at the "at" coordinates', () => { | ||
expect(rectangle({ at: { q: 0, r: 0 }, width: 2, height: 2 })(cursor, getHex)).toMatchObject([ | ||
{ q: 1, r: 0 }, | ||
{ q: 0, r: 1 }, | ||
{ q: 1, r: 1 }, | ||
]) | ||
}) | ||
}) | ||
|
||
describe('when passed start', () => { | ||
test('returns a traverser that returns hexes in a rectangular shape at and including the "start" coordinates', () => { | ||
expect(rectangle({ start: { q: 0, r: 0 }, width: 2, height: 2 })(cursor, getHex)).toMatchObject([ | ||
{ q: 0, r: 0 }, | ||
{ q: 1, r: 0 }, | ||
{ q: 0, r: 1 }, | ||
{ q: 1, r: 1 }, | ||
]) | ||
}) | ||
}) | ||
|
||
describe('when passed direction', () => { | ||
test('returns a traverser that returns hexes in a rectangular shape in order of the passed direction', () => { | ||
expect(rectangle({ width: 2, height: 2, direction: CompassDirection.S })(cursor, getHex)).toMatchObject([ | ||
{ q: 1, r: 3 }, | ||
{ q: 0, r: 2 }, | ||
{ q: 0, r: 3 }, | ||
]) | ||
}) | ||
}) | ||
|
||
describe('when passed two opposing corners', () => { | ||
test('returns a traverser that returns hexes in a rectangular shape within those corners', () => { | ||
expect(rectangle({ q: 1, r: 2 }, { q: 2, r: 3 })(cursor, getHex)).toMatchObject([ | ||
{ q: 1, r: 2 }, | ||
{ q: 2, r: 2 }, | ||
{ q: 1, r: 3 }, | ||
{ q: 2, r: 3 }, | ||
]) | ||
}) | ||
}) | ||
|
||
describe('when passed two opposing corners and false', () => { | ||
test('returns a traverser that returns hexes in a rectangular shape within those corners, excluding the first corner', () => { | ||
expect(rectangle({ q: 1, r: 2 }, { q: 2, r: 3 }, false)(cursor, getHex)).toMatchObject([ | ||
{ q: 2, r: 2 }, | ||
{ q: 1, r: 3 }, | ||
{ q: 2, r: 3 }, | ||
]) | ||
}) | ||
}) |
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