2024-03-18 13:20:47 +01:00
|
|
|
import { FieldMatcher, RangeEqualQualifier } from './types';
|
|
|
|
|
|
|
|
export function makeNumberMatcher(term: number, fuzz: number, qual: RangeEqualQualifier): FieldMatcher {
|
|
|
|
// Range matching.
|
2024-07-03 22:54:14 +02:00
|
|
|
return (v) => {
|
2024-03-18 13:20:47 +01:00
|
|
|
const attrVal = parseFloat(v);
|
|
|
|
|
|
|
|
if (isNaN(attrVal)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fuzz !== 0) {
|
|
|
|
return term - fuzz <= attrVal && term + fuzz >= attrVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (qual) {
|
|
|
|
case 'lt':
|
|
|
|
return attrVal < term;
|
|
|
|
case 'gt':
|
|
|
|
return attrVal > term;
|
|
|
|
case 'lte':
|
|
|
|
return attrVal <= term;
|
|
|
|
case 'gte':
|
|
|
|
return attrVal >= term;
|
|
|
|
case 'eq':
|
|
|
|
default:
|
|
|
|
return attrVal === term;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|