Skip to content

Commit

Permalink
feat(grid): add setStore helper function and add set method to GridSt…
Browse files Browse the repository at this point in the history
…ore interface

setStore() can be passed to Grid's each() method to write each hex to the grid's store (or the store
passed to setStore())
  • Loading branch information
flauwekeul committed Apr 22, 2021
1 parent 956a2a0 commit 10125c1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions playground/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { at, CompassDirection, createHexPrototype, Grid, Hex, inStore, move, Orientation } from '../dist'
import { at, CompassDirection, createHexPrototype, Grid, Hex, inStore, move, Orientation, setStore } from '../dist'
import { createSuite } from './benchmark'
import { render } from './render'

Expand Down Expand Up @@ -48,7 +48,8 @@ const store = new Map<string, CustomHex>()
// todo: when passed a store as 2nd argument, automatically use it (get and set)?
Grid.of(hexPrototype, store)
.rectangle({ start: { q: 0, r: 0 }, width: 10, height: 10 })
.each((hex, { store }) => store.set(hex.toString(), hex))
// .each((hex, { store }) => store.set(hex.toString(), hex))
.each(setStore())
.traverse(at({ q: 9, r: 0 }), move(CompassDirection.SE, 4), move(CompassDirection.SW, 4))
.filter(inStore())
// .takeWhile(inStore())
Expand Down
1 change: 1 addition & 0 deletions src/grid/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './inStore'
export * from './neighborOf'
export * from './setStore'
8 changes: 8 additions & 0 deletions src/grid/functions/setStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Hex } from '../../hex'
import { Grid } from '../grid'
import { GridStore } from '../types'

export const setStore = <T extends Hex, S extends GridStore<T>, G extends GridStore<T>>(store?: S) => (
hex: T,
grid: Grid<T, G>,
): GridStore<T> | void => (store ?? grid.store)?.set(hex.toString(), hex)
1 change: 1 addition & 0 deletions src/grid/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export interface GetOrCreateHexFn<T extends Hex> {

export interface GridStore<T extends Hex> {
get(id: string): T | undefined
set(id: string, hex: T): this
}

0 comments on commit 10125c1

Please sign in to comment.