philomena/assets/js/query/number.ts
liamwhite 3590be1429
match_query: unit test and rewrite for TypeScript (#208)
* match_query: unit test and rewrite for TypeScript

* match_query: use new type for parse errors

* match_query: avoid exceptional control flow in date parsing
2024-03-18 08:20:47 -04:00

30 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;
}
};
}