Notes/src/services/search/expressions/property_comparison.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

"use strict";
const Expression = require('./expression');
const NoteSet = require('../note_set');
2020-05-23 20:52:55 +02:00
/**
* Search string is lower cased for case insensitive comparison. But when retrieving properties
* we need case sensitive form so we have this translation object.
*/
const PROP_MAPPING = {
"noteid": "noteId",
"title": "title",
"type": "type",
"mime": "mime",
"isprotected": "isProtected",
"isarhived": "isArchived",
2020-05-23 20:52:55 +02:00
"datecreated": "dateCreated",
"datemodified": "dateModified",
"utcdatecreated": "utcDateCreated",
"utcdatemodified": "utcDateModified",
"contentlength": "contentLength",
"parentcount": "parentCount",
"childrencount": "childrenCount",
"attributecount": "attributeCount",
"labelcount": "labelCount",
"relationcount": "relationCount"
};
class PropertyComparisonExp extends Expression {
2020-05-23 20:52:55 +02:00
static isProperty(name) {
return name in PROP_MAPPING;
}
constructor(propertyName, comparator) {
super();
2020-05-23 20:52:55 +02:00
this.propertyName = PROP_MAPPING[propertyName];
this.comparator = comparator;
}
execute(inputNoteSet, searchContext) {
const resNoteSet = new NoteSet();
for (const note of inputNoteSet.notes) {
2020-05-23 20:52:55 +02:00
let value = note[this.propertyName];
2020-05-23 20:52:55 +02:00
if (value !== undefined && value !== null && typeof value !== 'string') {
value = value.toString();
}
if (value) {
value = value.toLowerCase();
}
if (this.comparator(value)) {
resNoteSet.add(note);
}
}
return resNoteSet;
}
}
module.exports = PropertyComparisonExp;