Notes/src/services/attribute_formatter.js

51 lines
1012 B
JavaScript
Raw Normal View History

2021-05-02 19:59:16 +02:00
"use strict"
function formatAttrForSearch(attr, searchWithValue) {
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;
}
function formatValue(val) {
2021-07-04 21:05:47 +02:00
if (!/[^\w_]/.test(val)) {
2021-05-02 19:59:16 +02:00
return val;
}
else if (!val.includes('"')) {
return '"' + val + '"';
}
else if (!val.includes("'")) {
return "'" + val + "'";
}
else if (!val.includes("`")) {
return "`" + val + "`";
}
else {
return '"' + val.replace(/"/g, '\\"') + '"';
}
}
module.exports = {
formatAttrForSearch
}