diff --git a/playground/index.ts b/playground/index.ts index 3b5ef350..2ed45774 100644 --- a/playground/index.ts +++ b/playground/index.ts @@ -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' @@ -48,7 +48,8 @@ const store = new Map() // 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()) diff --git a/src/grid/functions/index.ts b/src/grid/functions/index.ts index 66118284..19e1659f 100644 --- a/src/grid/functions/index.ts +++ b/src/grid/functions/index.ts @@ -1,2 +1,3 @@ export * from './inStore' export * from './neighborOf' +export * from './setStore' diff --git a/src/grid/functions/setStore.ts b/src/grid/functions/setStore.ts new file mode 100644 index 00000000..a48dd9c5 --- /dev/null +++ b/src/grid/functions/setStore.ts @@ -0,0 +1,8 @@ +import { Hex } from '../../hex' +import { Grid } from '../grid' +import { GridStore } from '../types' + +export const setStore = , G extends GridStore>(store?: S) => ( + hex: T, + grid: Grid, +): GridStore | void => (store ?? grid.store)?.set(hex.toString(), hex) diff --git a/src/grid/types.ts b/src/grid/types.ts index 1e5b8861..7b3c832f 100644 --- a/src/grid/types.ts +++ b/src/grid/types.ts @@ -20,4 +20,5 @@ export interface GetOrCreateHexFn { export interface GridStore { get(id: string): T | undefined + set(id: string, hex: T): this }