Filtra objetos JS por um objeto representando um critério de pesquisa
const objectKeys = require('./obj-keys')
const is = require('./obj-is')
const contains = require('./functional-tools').contains
const map = require('./functional-tools').map
exports.filterByCriteria = function filterByCriteria (criteria) {
return function filterByCriteria (dataset) {
let matches = [].concat(dataset)
const isArray = is('Array')
function isEqual (item1, item2) {
return item1 === item2
}
for (let key of objectKeys(criteria)) {
matches = matches.filter(datasetItem => {
let normDtItem = isArray(datasetItem[key]) ? datasetItem[key] : [].concat(datasetItem[key])
let normCriterias = isArray(criteria[key]) ? criteria[key] : [].concat(criteria[key])
for (let item of normDtItem) {
for (let criteriaItem of normCriterias) {
if (isEqual(item, criteriaItem)) {
return true
}
}
}
})
}
return matches
}
}