2023-04-08 21:07:48 +08:00
|
|
|
"use strict";
|
2021-05-02 19:59:16 +02:00
|
|
|
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { AttributeRow } from "../becca/entities/rows.js";
|
2024-02-17 19:15:50 +02:00
|
|
|
|
2024-02-18 11:26:05 +02:00
|
|
|
function formatAttrForSearch(attr: AttributeRow, searchWithValue: boolean) {
|
2025-01-09 18:07:02 +02:00
|
|
|
let searchStr = "";
|
2021-05-02 19:59:16 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (attr.type === "label") {
|
|
|
|
searchStr += "#";
|
|
|
|
} else if (attr.type === "relation") {
|
|
|
|
searchStr += "~";
|
|
|
|
} else {
|
2021-05-02 19:59:16 +02:00
|
|
|
throw new Error(`Unrecognized attribute type ${JSON.stringify(attr)}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
searchStr += attr.name;
|
|
|
|
|
|
|
|
if (searchWithValue && attr.value) {
|
2025-01-09 18:07:02 +02:00
|
|
|
if (attr.type === "relation") {
|
2021-05-02 19:59:16 +02:00
|
|
|
searchStr += ".noteId";
|
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
searchStr += "=";
|
2021-05-02 19:59:16 +02:00
|
|
|
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;
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (!val.includes('"')) {
|
2022-12-21 15:19:05 +01:00
|
|
|
return `"${val}"`;
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (!val.includes("'")) {
|
2022-12-21 15:19:05 +01:00
|
|
|
return `'${val}'`;
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (!val.includes("`")) {
|
2022-12-21 15:19:05 +01:00
|
|
|
return `\`${val}\``;
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2022-12-21 15:19:05 +01:00
|
|
|
return `"${val.replace(/"/g, '\\"')}"`;
|
2021-05-02 19:59:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:42:44 +03:00
|
|
|
export default {
|
2021-05-02 19:59:16 +02:00
|
|
|
formatAttrForSearch
|
2023-04-08 21:07:48 +08:00
|
|
|
};
|