You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
|
|
|
|
|
|
|
class Filter {
|
|
|
|
constructor(options) {
|
|
|
|
this._options = options
|
|
|
|
}
|
|
|
|
|
|
|
|
_eq(value, key, result) {
|
|
|
|
let isValid = false
|
|
|
|
|
|
|
|
if (result[key] === value) {
|
|
|
|
isValid = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return isValid
|
|
|
|
}
|
|
|
|
|
|
|
|
_neq(value, key, result) {
|
|
|
|
let isValid = false
|
|
|
|
|
|
|
|
if (result[key] !== value) {
|
|
|
|
isValid = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return isValid
|
|
|
|
}
|
|
|
|
|
|
|
|
_in(value, key, result) {
|
|
|
|
let isValid = false
|
|
|
|
|
|
|
|
if (!result[key]) {
|
|
|
|
isValid = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
let found = false
|
|
|
|
|
|
|
|
// run through array
|
|
|
|
value.forEach((v, index) => {
|
|
|
|
if (result[key].indexOf(v) !== -1) {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
isValid = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (result[key].indexOf(value) !== -1) {
|
|
|
|
isValid = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return isValid
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
validate(result) {
|
|
|
|
|
|
|
|
let isValid = true
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(this._options)) {
|
|
|
|
|
|
|
|
const operators = Object.keys(value)
|
|
|
|
|
|
|
|
operators.forEach((operator) => {
|
|
|
|
if (!this[operator](value[operator], key, result)) {
|
|
|
|
isValid = false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return isValid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Filter
|