Skip to content
Snippets Groups Projects

[ci skip]Create matchQueries middleware

Closed Vytor Calixto requested to merge match_queries_middleware into development
1 file
+ 41
0
Compare changes
  • Side-by-side
  • Inline
+ 41
0
// Retorna um array de "tuplas" com objetos que são iguais em duas queries distintas, com exceçã do valor total
function matchQueries(queryA = [], queryB = []) {
match = [];
// Para cada objeto na query A
queryA.forEach((result) => {
// Obtém as chaves do objeto
let keys = Object.keys(result);
// Remove o total
let index = keys.indexOf('total');
if(index > -1) keys.splice(index, 1);
let objMatch = null;
// Busca um objeto igual na query B
for(let i=0; i < queryB.length; ++i) {
let partialMatch = queryB[i];
let foundMatch = true;
for(let j=0; j < keys.length; ++j) {
let key = keys[j];
// Se diferem, não são um match
if(partialMatch[key] !== result[key]) {
foundMatch = false;
break;
}
}
// Se encontrou, pode parar
if(foundMatch) {
objMatch = partialMatch;
break;
}
}
// Inclui um array com os dois objetos dentro do array de matches
if(objMatch) {
match.push([result, objMatch]);
}
});
return match;
}
module.exports = matchQueries;
\ No newline at end of file
Loading