@@ -362,15 +362,15 @@ const findFromText = (xmlFiles, xpathQueries) => {
362362 let result1 = [ ] , result2 = [ ] ;
363363 let xpathQuery = xpathQueries [ 0 ] ;
364364 for ( let j = 0 ; j < xmlFiles . length ; j ++ ) {
365- result1 = result1 . concat ( runXPathQuery ( xmlFiles [ j ] , xpathQuery ) ) ;
365+ mergeArraysUnique ( result1 , runXPathQuery ( xmlFiles [ j ] , xpathQuery ) ) ;
366366 }
367367 for ( let j = 0 ; j < xmlFiles . length ; j ++ ) {
368368 result2 . push ( { filePath : xmlFiles [ j ] , results : [ ] } ) ;
369369 }
370370 for ( let i = 0 ; i < result1 . length ; i ++ ) {
371- xpathQuery = xpathQueries [ 1 ] . replace ( "<TEMP>" , result1 [ i ] . snippet ) ;
371+ xpathQuery = xpathQueries [ 1 ] . replaceAll ( "<TEMP>" , result1 [ i ] . snippet ) ;
372372 for ( let j = 0 ; j < xmlFiles . length ; j ++ ) {
373- result2 [ j ] . results = result2 [ j ] . results . concat ( runXPathQuery ( xmlFiles [ j ] , xpathQuery ) ) ;
373+ mergeArraysUnique ( result2 [ j ] . results , runXPathQuery ( xmlFiles [ j ] , xpathQuery ) ) ;
374374 }
375375 }
376376 return result2 ;
@@ -394,19 +394,37 @@ const findAndReturnToBase = (xmlFiles, xpathQueries) => {
394394 result1 = runXPathQuery ( xmlFiles [ base ] , xpathQuery ) ;
395395 for ( let i = 0 ; i < result1 . length ; i ++ ) {
396396 for ( let j = 0 ; j < xmlFiles . length ; j ++ ) {
397- xpathQuery = xpathQueries [ 1 ] . replace ( new RegExp ( "<TEMP>" , "g" ) , result1 [ i ] . snippet ) ;
397+ xpathQuery = xpathQueries [ 1 ] . replaceAll ( "<TEMP>" , result1 [ i ] . snippet ) ;
398398 result2 = runXPathQuery ( xmlFiles [ j ] , xpathQuery ) ;
399399
400400 for ( let k = 0 ; k < result2 . length ; k ++ ) {
401- xpathQuery = xpathQueries [ 2 ] . replace ( "<TEMP>" , result2 [ k ] . snippet ) ;
402- result3 [ base ] . results = result3 [ base ] . results . concat ( runXPathQuery ( xmlFiles [ base ] , xpathQuery ) ) ;
401+ xpathQuery = xpathQueries [ 2 ] . replaceAll ( "<TEMP>" , result2 [ k ] . snippet ) ;
402+ mergeArraysUnique ( result3 [ base ] . results , runXPathQuery ( xmlFiles [ base ] , xpathQuery ) ) ;
403403 }
404404 }
405405 }
406406 }
407407 return result3 ;
408408} ;
409409
410+ /**
411+ * Merge two arrays of objects without duplicates based on the xml.xml property.
412+ * @param {{filePath, xml: {fileName, xml}, snippet,surroundingNodes}[] } mainArray
413+ * The primary array of objects.
414+ * @param {{filePath, xml: {fileName, xml}, snippet,surroundingNodes}[] } addition
415+ * The secondary array of objects to be merged into the primary array.
416+ * @returns {Array } - The merged array with unique entries.
417+ */
418+ function mergeArraysUnique ( mainArray , addition ) {
419+ const existingXmlValues = new Set ( mainArray . map ( obj => obj . xml . xml ) ) ;
420+ addition . forEach ( obj => {
421+ if ( ! existingXmlValues . has ( obj . xml . xml ) ) {
422+ mainArray . push ( obj ) ;
423+ existingXmlValues . add ( obj . xml . xml ) ; // Add the new xml.xml value to the set
424+ }
425+ } ) ;
426+ return mainArray ;
427+ }
410428
411429/**
412430 * runs the XPath query and compare results
0 commit comments