Filter negation bugfix
This commit is contained in:
parent
3eac7f8e6d
commit
4d65eb19a6
2 changed files with 23 additions and 15 deletions
|
@ -9,9 +9,8 @@ const _false = () => false
|
|||
const _and = arr => p => arr.map(e => e(p)).reduce((a, b) => a && b)
|
||||
const _or = arr => p => arr.map(e => e(p)).reduce((a, b) => a || b)
|
||||
|
||||
const similar = (key, reference, name, units) => {
|
||||
const similar = (keys, reference, name, units) => {
|
||||
let neg = false
|
||||
|
||||
if (reference.startsWith('-')) {
|
||||
neg = true
|
||||
reference = reference.slice(1)
|
||||
|
@ -20,15 +19,19 @@ const similar = (key, reference, name, units) => {
|
|||
// support both string or regex as reference
|
||||
let internal_tester = val => (''+val) == reference
|
||||
if (reference.startsWith('/')) {
|
||||
const regex_parts = reference.split('/')
|
||||
regex_parts.shift() // remove starting slash
|
||||
const flags = regex_parts.pop()
|
||||
const regex = new RegExp(regex_parts.join('/'), flags)
|
||||
internal_tester = val => regex.test(''+val)
|
||||
try {
|
||||
const regex_parts = reference.split('/')
|
||||
regex_parts.shift() // remove starting slash
|
||||
const flags = regex_parts.pop()
|
||||
const regex = new RegExp(regex_parts.join('/'), flags)
|
||||
internal_tester = val => regex.test(''+val)
|
||||
} catch (ex) {
|
||||
throw new Error(`Invalid regex "${reference}" found at filter "${name}"!`)
|
||||
}
|
||||
}
|
||||
|
||||
// support strings, arrays, or objects as key
|
||||
const external_tester = point => {
|
||||
const external_tester = (point, key) => {
|
||||
const value = u.deep(point, key)
|
||||
if (a.type(value)() == 'array') {
|
||||
return value.some(subkey => internal_tester(subkey))
|
||||
|
@ -39,11 +42,12 @@ const similar = (key, reference, name, units) => {
|
|||
}
|
||||
}
|
||||
|
||||
// negation happens at the end
|
||||
// consider negation
|
||||
if (neg) {
|
||||
return point => !external_tester(point)
|
||||
return point => keys.every(key => !external_tester(point, key))
|
||||
} else {
|
||||
return point => keys.some(key => external_tester(point, key))
|
||||
}
|
||||
return external_tester
|
||||
}
|
||||
|
||||
const comparators = {
|
||||
|
@ -75,7 +79,7 @@ const simple = (exp, name, units) => {
|
|||
value = exp
|
||||
}
|
||||
|
||||
return point => keys.some(key => comparators[op](key, value, name, units)(point))
|
||||
return point => comparators[op](keys, value, name, units)(point)
|
||||
}
|
||||
|
||||
const complex = (config, name, units, aggregator=_or) => {
|
||||
|
@ -109,7 +113,7 @@ const contains_object = (val) => {
|
|||
}
|
||||
|
||||
exports.parse = (config, name, points={}, units={}, asym='source') => {
|
||||
|
||||
|
||||
let result = []
|
||||
|
||||
// if a filter decl is undefined, it's just the default point at [0, 0]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue