server-ts: Port services/search/services/build_comparator

This commit is contained in:
Elian Doran 2024-02-18 01:24:57 +02:00
parent f5b690d088
commit 8acb64198c
No known key found for this signature in database
3 changed files with 19 additions and 14 deletions

View File

@ -2,7 +2,7 @@
const Expression = require('./expression');
const NoteSet = require('../note_set');
const buildComparator = require('../services/build_comparator.js');
const buildComparator = require('../services/build_comparator');
/**
* Search string is lower cased for case-insensitive comparison. But when retrieving properties,

View File

@ -1,6 +1,6 @@
const cachedRegexes = {};
const cachedRegexes: Record<string, RegExp> = {};
function getRegex(str) {
function getRegex(str: string) {
if (!(str in cachedRegexes)) {
cachedRegexes[str] = new RegExp(str);
}
@ -8,31 +8,36 @@ function getRegex(str) {
return cachedRegexes[str];
}
const stringComparators = {
type Comparator<T> = (comparedValue: T) => ((val: string) => boolean);
const stringComparators: Record<string, Comparator<string>> = {
"=": comparedValue => (val => val === comparedValue),
"!=": comparedValue => (val => val !== comparedValue),
">": comparedValue => (val => val > comparedValue),
">=": comparedValue => (val => val >= comparedValue),
"<": comparedValue => (val => val < comparedValue),
"<=": comparedValue => (val => val <= comparedValue),
"*=": comparedValue => (val => val && val.endsWith(comparedValue)),
"=*": comparedValue => (val => val && val.startsWith(comparedValue)),
"*=*": comparedValue => (val => val && val.includes(comparedValue)),
"%=": comparedValue => (val => val && !!getRegex(comparedValue).test(val)),
"*=": comparedValue => (val => !!val && val.endsWith(comparedValue)),
"=*": comparedValue => (val => !!val && val.startsWith(comparedValue)),
"*=*": comparedValue => (val => !!val && val.includes(comparedValue)),
"%=": comparedValue => (val => !!val && !!getRegex(comparedValue).test(val)),
};
const numericComparators = {
const numericComparators: Record<string, Comparator<number>> = {
">": comparedValue => (val => parseFloat(val) > comparedValue),
">=": comparedValue => (val => parseFloat(val) >= comparedValue),
"<": comparedValue => (val => parseFloat(val) < comparedValue),
"<=": comparedValue => (val => parseFloat(val) <= comparedValue)
};
function buildComparator(operator, comparedValue) {
function buildComparator(operator: string, comparedValue: string) {
comparedValue = comparedValue.toLowerCase();
if (operator in numericComparators && !isNaN(comparedValue)) {
return numericComparators[operator](parseFloat(comparedValue));
if (operator in numericComparators) {
const floatValue = parseFloat(comparedValue);
if (!isNaN(floatValue)) {
return numericComparators[operator](floatValue);
}
}
if (operator in stringComparators) {
@ -40,4 +45,4 @@ function buildComparator(operator, comparedValue) {
}
}
module.exports = buildComparator;
export = buildComparator;

View File

@ -15,7 +15,7 @@ const NoteFlatTextExp = require('../expressions/note_flat_text');
const NoteContentFulltextExp = require('../expressions/note_content_fulltext');
const OrderByAndLimitExp = require('../expressions/order_by_and_limit');
const AncestorExp = require('../expressions/ancestor');
const buildComparator = require('./build_comparator.js');
const buildComparator = require('./build_comparator');
const ValueExtractor = require('../value_extractor');
const utils = require('../../utils');
const TrueExp = require('../expressions/true');