mirror of
https://github.com/zadam/trilium.git
synced 2025-11-04 21:49:04 +01:00
feat(search): implement additional operators (with bounds) for search comparison
This commit is contained in:
parent
4db04519bd
commit
b4f503b81e
@ -1,3 +1,5 @@
|
|||||||
|
import { normalizeSearchText, fuzzyMatchWord, FUZZY_SEARCH_CONFIG } from "../utils/text_utils.js";
|
||||||
|
|
||||||
const cachedRegexes: Record<string, RegExp> = {};
|
const cachedRegexes: Record<string, RegExp> = {};
|
||||||
|
|
||||||
function getRegex(str: string) {
|
function getRegex(str: string) {
|
||||||
@ -20,7 +22,41 @@ const stringComparators: Record<string, Comparator<string>> = {
|
|||||||
"*=": (comparedValue) => (val) => !!val && val.endsWith(comparedValue),
|
"*=": (comparedValue) => (val) => !!val && val.endsWith(comparedValue),
|
||||||
"=*": (comparedValue) => (val) => !!val && val.startsWith(comparedValue),
|
"=*": (comparedValue) => (val) => !!val && val.startsWith(comparedValue),
|
||||||
"*=*": (comparedValue) => (val) => !!val && val.includes(comparedValue),
|
"*=*": (comparedValue) => (val) => !!val && val.includes(comparedValue),
|
||||||
"%=": (comparedValue) => (val) => !!val && !!getRegex(comparedValue).test(val)
|
"%=": (comparedValue) => (val) => !!val && !!getRegex(comparedValue).test(val),
|
||||||
|
"~=": (comparedValue) => (val) => {
|
||||||
|
if (!val || !comparedValue) return false;
|
||||||
|
|
||||||
|
// Validate minimum length for fuzzy search to prevent false positives
|
||||||
|
if (comparedValue.length < FUZZY_SEARCH_CONFIG.MIN_FUZZY_TOKEN_LENGTH) {
|
||||||
|
return val.includes(comparedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizedVal = normalizeSearchText(val);
|
||||||
|
const normalizedCompared = normalizeSearchText(comparedValue);
|
||||||
|
|
||||||
|
// First try exact substring match
|
||||||
|
if (normalizedVal.includes(normalizedCompared)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then try fuzzy word matching
|
||||||
|
const words = normalizedVal.split(/\s+/);
|
||||||
|
return words.some(word => fuzzyMatchWord(normalizedCompared, word));
|
||||||
|
},
|
||||||
|
"~*": (comparedValue) => (val) => {
|
||||||
|
if (!val || !comparedValue) return false;
|
||||||
|
|
||||||
|
// Validate minimum length for fuzzy search
|
||||||
|
if (comparedValue.length < FUZZY_SEARCH_CONFIG.MIN_FUZZY_TOKEN_LENGTH) {
|
||||||
|
return val.includes(comparedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizedVal = normalizeSearchText(val);
|
||||||
|
const normalizedCompared = normalizeSearchText(comparedValue);
|
||||||
|
|
||||||
|
// For ~* operator, use fuzzy matching across the entire content
|
||||||
|
return fuzzyMatchWord(normalizedCompared, normalizedVal);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const numericComparators: Record<string, Comparator<number>> = {
|
const numericComparators: Record<string, Comparator<number>> = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user