- commit
- e947297cd1ec27a3f77ebec3fd5041537b84f037
- parent
- aed55112b078949541db0b74d399dcf06962f36b
- author
- Dami <dami.etoile@gmail.com>
| M | src/ | 18 | +++++++++++++----- |
| M | tests/ | 4 | ++++ |
@@ -195,16 +195,24 @@ export function createIndexLayout(source) {
195195 sheet.nextWorldId = worlds[index + 1]?.id || null;196196 }197197 const nodes = sheets.flatMap(sheet => sheet.nodes);198- const byWorld = new Map(worlds.map(world => [world.id, world]));199- return {198+ return withLookupHelpers({200199 columns: INDEX_COLUMNS,201200 rows: INDEX_ROWS,202201 worlds,203202 sheets,204203 nodes,204+ });205+}206+207+function withLookupHelpers(layout) {208+ const byWorld = new Map(layout.worlds.map(world => [world.id, world]));209+ const bySheet = new Map(layout.sheets.map(sheet => [sheet.id, sheet]));210+ const byNode = new Map(layout.nodes.map(node => [node.id, node]));211+ return {212+ ...layout,205213 getWorld(worldId) { return byWorld.get(worldId) || null; },206- getSheet(sheetId) { return sheets.find(sheet => sheet.id === sheetId) || null; },207- getNode(nodeId) { return findNode(this, nodeId); },214+ getSheet(sheetId) { return bySheet.get(sheetId) || null; },215+ getNode(nodeId) { return byNode.get(nodeId) || null; },208216 };209217 }210218 @@ -239,7 +247,7 @@ export function decorateIndex(layout, progress) {
239247 ...world,240248 completedCount: completedByWorld.get(world.id) || 0,241249 }));242- return { ...layout, worlds, sheets, nodes: sheets.flatMap(sheet => sheet.nodes) };250+ return withLookupHelpers({ ...layout, worlds, sheets, nodes: sheets.flatMap(sheet => sheet.nodes) });243251 }244252 245253 function findNode(layout, nodeId) {@@ -209,4 +209,8 @@ test('catalog pack metadata becomes world metadata and completion is scoped', ()
209209 ]);210210 const decorated = decorateIndex(layout, { isSolved(packId) { return packId === 'tidewater'; } });211211 assert.deepEqual(decorated.worlds.map(world => world.completedCount), [0, 1]);212+ assert.equal(decorated.getWorld('tidewater').completedCount, 1);213+ assert.equal(decorated.getSheet('tidewater:sheet-1').nodes[0].solved, true);214+ const detachedGetNode = decorated.getNode;215+ assert.equal(detachedGetNode('tidewater:one').id, 'tidewater:one');212216 });