ui: fix the problematic search stringify function

This commit is contained in:
undefined committed 2023-08-10 14:04:00 +08:00
1 parent 81d6582d33
commit f79f3d2d79
6 files changed
+41 -6

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
import { parse, SearchParserOptions, SearchParserResult } from 'search-query-parser';
export { parse, SearchParserOptions, SearchParserResult } from 'search-query-parser';
export function stringify(queryObject: SearchParserResult, options: SearchParserOptions = {}, prefix = ''): string {
if (!Object.keys(queryObject || {}).length) return '';
prefix ||= '';
const toArray = (val: string | string[]) => (typeof val === 'string' ? [val] : val);
const addQuotes = (s: string) => (s.indexOf(' ') > -1 ? JSON.stringify(s) : s);
const addPrefix = (s: string) => prefix + s;
const parts = [];
if (queryObject.text) {
const value = toArray(queryObject.text);
if (value.length) parts.push(value.map(addQuotes).map(addPrefix).join(' '));
}
for (const range of options.ranges || []) {
if (!queryObject[range]) continue;
let value = queryObject[range].from;
const to = queryObject[range].to;
if (to) value = `${value}-${to}`;
if (value) parts.push(addPrefix(`${range}:${value}`));
}
for (const keyword of options.keywords || []) {
if (!queryObject[keyword]) continue;
const value = toArray(queryObject[keyword]);
if (value.length > 0) {
parts.push(addPrefix(`${keyword}:${addQuotes(value.join(','))}`));
}
}
if (Object.keys(queryObject.exclude || {}).length) {
parts.push(stringify(queryObject.exclude, options, '-'));
}
return parts.join(' ');
}
export default { stringify, parse };
+1
View File
@@ -15,6 +15,7 @@
"moment-timezone": "^0.5.43",
"mongodb": "^5.7.0",
"reggol": "^1.4.4",
"search-query-parser": "^1.6.0",
"source-map-support": "^0.5.21",
"systeminformation": "^5.18.8"
},