mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-08-05 05:18:05 +08:00
✨ feat(redis): 新增 Key 精确搜索模式
- 增加 Redis Key 模糊/精确搜索切换 - 精确模式不再追加通配符并保留大小写敏感匹配 - 转义 Redis glob 特殊字符避免误匹配 - 补充搜索模式回归测试
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
const REDIS_GLOB_SPECIAL_CHARS = /([*?\[\]\\])/g;
|
||||
const ASCII_LETTER = /^[A-Za-z]$/;
|
||||
|
||||
export type RedisSearchMode = 'fuzzy' | 'exact';
|
||||
|
||||
const escapeRedisGlobLiteral = (value: string): string => {
|
||||
return value.replace(REDIS_GLOB_SPECIAL_CHARS, '\\$1');
|
||||
};
|
||||
@@ -17,23 +19,32 @@ const toCaseInsensitiveRedisGlobLiteral = (value: string): string => {
|
||||
}).join('');
|
||||
};
|
||||
|
||||
export const normalizeRedisSearchInput = (rawValue: string): { keyword: string; pattern: string } => {
|
||||
export const normalizeRedisSearchInput = (
|
||||
rawValue: string,
|
||||
mode: RedisSearchMode = 'fuzzy',
|
||||
): { keyword: string; pattern: string } => {
|
||||
const keyword = String(rawValue || '').trim();
|
||||
if (!keyword) {
|
||||
return { keyword: '', pattern: '*' };
|
||||
}
|
||||
if (mode === 'exact') {
|
||||
return {
|
||||
keyword,
|
||||
pattern: escapeRedisGlobLiteral(keyword),
|
||||
};
|
||||
}
|
||||
return {
|
||||
keyword,
|
||||
pattern: `*${toCaseInsensitiveRedisGlobLiteral(keyword)}*`,
|
||||
};
|
||||
};
|
||||
|
||||
export const normalizeRedisSearchDraftChange = (rawValue: string): {
|
||||
export const normalizeRedisSearchDraftChange = (rawValue: string, mode: RedisSearchMode = 'fuzzy'): {
|
||||
keyword: string;
|
||||
pattern: string;
|
||||
shouldSearchImmediately: boolean;
|
||||
} => {
|
||||
const normalized = normalizeRedisSearchInput(rawValue);
|
||||
const normalized = normalizeRedisSearchInput(rawValue, mode);
|
||||
return {
|
||||
...normalized,
|
||||
shouldSearchImmediately: normalized.keyword === '',
|
||||
|
||||
Reference in New Issue
Block a user