Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
server-ts: Port services/search/services/build_comparator
Elian Doran8acb64198cb4
diff --git a/src/services/search/expressions/property_comparison.js b/src/services/search/expressions/property_comparison.jsindex 865e50ff1f..8ab21f448d 100644--- a/src/services/search/expressions/property_comparison.js+++ b/src/services/search/expressions/property_comparison.js@@ -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,diff --git a/src/services/search/services/build_comparator.js b/src/services/search/services/build_comparator.tssimilarity index 51%rename from src/services/search/services/build_comparator.jsrename to src/services/search/services/build_comparator.tsindex 6d3ba463a4..426bfa1959 100644--- a/src/services/search/services/build_comparator.js+++ b/src/services/search/services/build_comparator.ts@@ -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;diff --git a/src/services/search/services/parse.js b/src/services/search/services/parse.jsindex 869046236e..330f139c08 100644--- a/src/services/search/services/parse.js+++ b/src/services/search/services/parse.js@@ -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');