From c9e48fe8b463084544bb0604da2df605e03a24b2 Mon Sep 17 00:00:00 2001 From: minie4 Date: Mon, 28 Aug 2023 12:14:40 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=20Prevent=20duplicate=20substituti?= =?UTF-8?q?ons=20within=20a=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/parser/index.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/server/parser/index.js b/server/parser/index.js index 3a1e448..8db1e6c 100644 --- a/server/parser/index.js +++ b/server/parser/index.js @@ -39,10 +39,24 @@ export class Parser { const dayPlans = []; for (const plan of plans) { const foundPlan = dayPlans.find((e) => e.date == plan.date); - if (!foundPlan) dayPlans.push(plan); - else { + if (!foundPlan) { + // Make sure to not insert duplicate substitutions within a file + const changes = structuredClone(plan.changes); + const cleanedChanges = []; + for (const change of changes) { + const changeExists = cleanedChanges.find( + (e) => JSON.stringify(e) == JSON.stringify(change), + ); + if (!changeExists) { + cleanedChanges.push(change); + } + // Use the new array of changes + plan.changes = cleanedChanges; + } + dayPlans.push(plan); + } else { for (const change of plan.changes) { - // Make sure to not insert the exact same substitution twice + // Make sure to not insert a substitution that already exists in the changes const changeExists = foundPlan.changes.find( (e) => JSON.stringify(e) == JSON.stringify(change), );