From 1351f69e0efcf5119d0bac776775e0a430cc52d6 Mon Sep 17 00:00:00 2001 From: saharmehrpour Date: Wed, 22 May 2024 00:10:57 -0400 Subject: [PATCH] fix issue #62 --- src/core/ruleExecutor.js | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/core/ruleExecutor.js b/src/core/ruleExecutor.js index c3dd446..32f4feb 100644 --- a/src/core/ruleExecutor.js +++ b/src/core/ruleExecutor.js @@ -362,15 +362,15 @@ const findFromText = (xmlFiles, xpathQueries) => { let result1 = [], result2 = []; let xpathQuery = xpathQueries[0]; for (let j = 0; j < xmlFiles.length; j++) { - result1 = result1.concat(runXPathQuery(xmlFiles[j], xpathQuery)); + mergeArraysUnique(result1, runXPathQuery(xmlFiles[j], xpathQuery)); } for (let j = 0; j < xmlFiles.length; j++) { result2.push({filePath: xmlFiles[j], results: []}); } for (let i = 0; i < result1.length; i++) { - xpathQuery = xpathQueries[1].replace("", result1[i].snippet); + xpathQuery = xpathQueries[1].replaceAll("", result1[i].snippet); for (let j = 0; j < xmlFiles.length; j++) { - result2[j].results = result2[j].results.concat(runXPathQuery(xmlFiles[j], xpathQuery)); + mergeArraysUnique(result2[j].results, runXPathQuery(xmlFiles[j], xpathQuery)); } } return result2; @@ -394,12 +394,12 @@ const findAndReturnToBase = (xmlFiles, xpathQueries) => { result1 = runXPathQuery(xmlFiles[base], xpathQuery); for (let i = 0; i < result1.length; i++) { for (let j = 0; j < xmlFiles.length; j++) { - xpathQuery = xpathQueries[1].replace(new RegExp("", "g"), result1[i].snippet); + xpathQuery = xpathQueries[1].replaceAll("", result1[i].snippet); result2 = runXPathQuery(xmlFiles[j], xpathQuery); for (let k = 0; k < result2.length; k++) { - xpathQuery = xpathQueries[2].replace("", result2[k].snippet); - result3[base].results = result3[base].results.concat(runXPathQuery(xmlFiles[base], xpathQuery)); + xpathQuery = xpathQueries[2].replaceAll("", result2[k].snippet); + mergeArraysUnique(result3[base].results, runXPathQuery(xmlFiles[base], xpathQuery)); } } } @@ -407,6 +407,24 @@ const findAndReturnToBase = (xmlFiles, xpathQueries) => { return result3; }; +/** + * Merge two arrays of objects without duplicates based on the xml.xml property. + * @param {{filePath, xml: {fileName, xml}, snippet,surroundingNodes}[]} mainArray + * The primary array of objects. + * @param {{filePath, xml: {fileName, xml}, snippet,surroundingNodes}[]} addition + * The secondary array of objects to be merged into the primary array. + * @returns {Array} - The merged array with unique entries. + */ +function mergeArraysUnique(mainArray, addition) { + const existingXmlValues = new Set(mainArray.map(obj => obj.xml.xml)); + addition.forEach(obj => { + if (!existingXmlValues.has(obj.xml.xml)) { + mainArray.push(obj); + existingXmlValues.add(obj.xml.xml); // Add the new xml.xml value to the set + } + }); + return mainArray; +} /** * runs the XPath query and compare results