Notes/src/services/attribute_formatter.ts

53 lines
1.1 KiB
TypeScript
Raw Normal View History

"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) {
if (!/[^\w]/.test(val)) {
2021-05-02 19:59:16 +02:00
return val;
}
else if (!val.includes('"')) {
return `"${val}"`;
2021-05-02 19:59:16 +02:00
}
else if (!val.includes("'")) {
return `'${val}'`;
2021-05-02 19:59:16 +02:00
}
else if (!val.includes("`")) {
return `\`${val}\``;
2021-05-02 19:59:16 +02:00
}
else {
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
};