From 913c9b0f014fdb8cd2739c3b6729fcdda5596aad Mon Sep 17 00:00:00 2001 From: Michael DiRienzo Date: Thu, 9 Jul 2020 17:50:55 -0400 Subject: [PATCH] Address the problem of potential data loss Any errors while iterating the states or burgs could potentially lose the index 0 metadata stored in the arrays. This will instead track the index and ignore the 0th result. --- modules/burgs-and-states.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/modules/burgs-and-states.js b/modules/burgs-and-states.js index 87cad104..659cc1f5 100644 --- a/modules/burgs-and-states.js +++ b/modules/burgs-and-states.js @@ -357,24 +357,25 @@ // cell or center cell's (respectively) culture. const resetCultures = function () { console.time('resetCulturesForBurgsAndStates'); - // Pull out index 0 nodes so iterators don't touch them. - const burgTreeNode = pack.burgs.shift(); - const neutralsStateNode = pack.states.shift(); // Assign the culture associated with the burgs cell. - pack.burgs = pack.burgs.map( (burg) => { + pack.burgs = pack.burgs.map( (burg, index) => { + // Ignore metadata burg + if(index === 0) { + return burg; + } return {...burg, culture: pack.cells.culture[burg.cell]}; }); // Assign the culture associated with the states' center cell. - pack.states = pack.states.map( (state) => { + pack.states = pack.states.map( (state, index) => { + // Ignore neutrals state + if(index === 0) { + return state; + } return {...state, culture: pack.cells.culture[state.center]}; }); - // Prepend the index 0 nodes back onto the packgroups. - pack.burgs.unshift(burgTreeNode); - pack.states.unshift(neutralsStateNode); - console.timeEnd('resetCulturesForBurgsAndStates'); }