2023-04-08 21:07:48 +08:00
|
|
|
"use strict";
|
2021-05-02 19:59:16 +02:00
|
|
|
|
2024-02-17 19:15:50 +02:00
|
|
|
import BAttribute = require("../becca/entities/battribute");
|
|
|
|
|
|
|
|
function formatAttrForSearch(attr: BAttribute, searchWithValue: string) {
|
2021-05-02 19:59:16 +02:00
|
|
|
let searchStr = '';
|
|
|
|
|
|
|
|
if (attr.type === 'label') {
|
|
|
|
searchStr += '#';
|
|
|
|
}
|
|
|
|
else if (attr.type === 'relation') {
|
|
|
|
searchStr += '~';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error(`Unrecognized attribute type ${JSON.stringify(attr)}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
searchStr += attr.name;
|
|
|
|
|
|
|
|
if (searchWithValue && attr.value) {
|
|
|
|
if (attr.type === 'relation') {
|
|
|
|
searchStr += ".noteId";
|
|
|
|
}
|
|
|
|
|
|
|
|
searchStr += '=';
|
|
|
|
searchStr += formatValue(attr.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchStr;
|
|
|
|
}
|
|
|
|
|
2024-02-17 19:15:50 +02:00
|
|
|
function formatValue(val: string) {
|
2023-04-08 21:07:48 +08:00
|
|
|
if (!/[^\w]/.test(val)) {
|
2021-05-02 19:59:16 +02:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
else if (!val.includes('"')) {
|
2022-12-21 15:19:05 +01:00
|
|
|
return `"${val}"`;
|
2021-05-02 19:59:16 +02:00
|
|
|
}
|
|
|
|
else if (!val.includes("'")) {
|
2022-12-21 15:19:05 +01:00
|
|
|
return `'${val}'`;
|
2021-05-02 19:59:16 +02:00
|
|
|
}
|
|
|
|
else if (!val.includes("`")) {
|
2022-12-21 15:19:05 +01:00
|
|
|
return `\`${val}\``;
|
2021-05-02 19:59:16 +02:00
|
|
|
}
|
|
|
|
else {
|
2022-12-21 15:19:05 +01:00
|
|
|
return `"${val.replace(/"/g, '\\"')}"`;
|
2021-05-02 19:59:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-17 19:15:50 +02:00
|
|
|
export = {
|
2021-05-02 19:59:16 +02:00
|
|
|
formatAttrForSearch
|
2023-04-08 21:07:48 +08:00
|
|
|
};
|