boardgame-core/src/core/part-factory.ts

23 lines
767 B
TypeScript

import {Part} from "./part";
import {Immutable} from "mutative";
export function createPartsFromTable<T>(items: readonly T[], getId: (item: T, index: number) => string, getCount?: ((item: T) => number) | number){
const pool: Record<string, Part<T>> = {};
for (const entry of items) {
const count = getCount ? (typeof getCount === 'function' ? getCount(entry) : getCount) : 1;
for (let i = 0; i < count; i++) {
const id = getId(entry, i);
pool[id] = {
id,
regionId: '',
position: [],
...entry as Immutable<T>
};
}
}
return pool;
}
export function createParts<T>(item: T, getId: (index: number) => string, count?: number){
return createPartsFromTable([item], (_,index) => getId(index), count);
}