Skip to content

Commit

Permalink
Merge pull request #69 from devuxd/fix_repetitive_snippets
Browse files Browse the repository at this point in the history
fix issue #62
  • Loading branch information
SaharMehrpour authored May 22, 2024
2 parents 9d3faf1 + 1351f69 commit 57d0f1f
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/core/ruleExecutor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<TEMP>", result1[i].snippet);
xpathQuery = xpathQueries[1].replaceAll("<TEMP>", 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;
Expand All @@ -394,19 +394,37 @@ 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("<TEMP>", "g"), result1[i].snippet);
xpathQuery = xpathQueries[1].replaceAll("<TEMP>", result1[i].snippet);
result2 = runXPathQuery(xmlFiles[j], xpathQuery);

for (let k = 0; k < result2.length; k++) {
xpathQuery = xpathQueries[2].replace("<TEMP>", result2[k].snippet);
result3[base].results = result3[base].results.concat(runXPathQuery(xmlFiles[base], xpathQuery));
xpathQuery = xpathQueries[2].replaceAll("<TEMP>", result2[k].snippet);
mergeArraysUnique(result3[base].results, runXPathQuery(xmlFiles[base], xpathQuery));
}
}
}
}
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
Expand Down

0 comments on commit 57d0f1f

Please sign in to comment.