Skip to content

Commit

Permalink
HexGrid.map
Browse files Browse the repository at this point in the history
  • Loading branch information
butzopower committed Jan 20, 2022
1 parent c58a2d0 commit 6cd8340
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/hexgrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ export class Hex<T = void> {
readonly y: number;
content: T | undefined;

constructor(
parent: HexGrid<T>,
x: number,
y: number,
initializeFn?: InitHexFn<T>,
) {
constructor(parent: HexGrid<T>, x: number, y: number, initializeFn?: InitHexFn<T>) {
this.parent = parent;
this.x = x;
this.y = y;
Expand All @@ -27,8 +22,8 @@ export class HexGrid<T = void> {
private readonly hexArray: Hex<T>[][]

constructor(
width: number,
height: number,
private width: number,
private height: number,
initializeFn?: InitHexFn<T>,
) {
this.hexArray = Array.from(new Array(height))
Expand Down Expand Up @@ -68,4 +63,16 @@ export class HexGrid<T = void> {
]
.filter(hex => hex !== undefined);
}

map<U>(mappingFunction: (x: Hex<T>) => U): HexGrid<U> {
const originalHex = (hex: Hex<unknown>) => {
return this.at(hex.x, hex.y);
}

return new HexGrid<U>(
this.width,
this.height,
(hex) => mappingFunction(originalHex(hex))
);
}
}
37 changes: 37 additions & 0 deletions tests/hexgrid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,43 @@ describe('a hex grid', () => {
expect(grid.at(2, 2).content).to.eql('2,2');
});
});

describe('mapping over the grid', () => {
it('returns a new grid with the mapped values', () => {
fc.assert(fc.property(fc.anything(), thing => {
const originalGrid = new HexGrid<string>(1, 1, () => '');
const grid = originalGrid.map(() => thing);

expect(grid.at(0, 0).content).to.eql(thing);
}));
});

it('passes the original hex properties into the function', () => {
fc.assert(fc.property(fc.anything(), fc.anything(), (thing, thing2) => {
const originalGrid = new HexGrid<unknown>(3, 3, () => thing);
const grid = originalGrid.map((hex) => `${hex.x},${hex.y}`);

expect(grid.at(0, 0).content).to.eql('0,0');
expect(grid.at(1, 0).content).to.eql('1,0');
expect(grid.at(2, 0).content).to.eql('2,0');
expect(grid.at(0, 1).content).to.eql('0,1');
expect(grid.at(1, 1).content).to.eql('1,1');
expect(grid.at(2, 1).content).to.eql('2,1');
expect(grid.at(0, 2).content).to.eql('0,2');
expect(grid.at(1, 2).content).to.eql('1,2');
expect(grid.at(2, 2).content).to.eql('2,2');
}));
});

it('passes the original hex content into the function', () => {
fc.assert(fc.property(fc.anything(), fc.anything(), (thing, thing2) => {
const originalGrid = new HexGrid<unknown>(1, 1, () => thing);
const grid = originalGrid.map((x) => `${JSON.stringify(x.content)}|${JSON.stringify(thing2)}`);

expect(grid.at(0, 0).content).to.eql(`${JSON.stringify(thing)}|${JSON.stringify(thing2)}`);
}));
});
});
});

describe('a hex', () => {
Expand Down

0 comments on commit 6cd8340

Please sign in to comment.