mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-24 01:47:59 +01:00
31 lines
679 B
TypeScript
31 lines
679 B
TypeScript
|
import { FieldMatcher, RangeEqualQualifier } from './types';
|
||
|
|
||
|
export function makeNumberMatcher(term: number, fuzz: number, qual: RangeEqualQualifier): FieldMatcher {
|
||
|
// Range matching.
|
||
|
return v => {
|
||
|
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;
|
||
|
}
|
||
|
};
|
||
|
}
|