mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	server-ts: Port services/search/services/build_comparator
This commit is contained in:
		
							parent
							
								
									f5b690d088
								
							
						
					
					
						commit
						8acb64198c
					
				@ -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,
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
@ -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');
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user