fix using smart values with .dateCreated, closes #1338

This commit is contained in:
zadam 2020-10-27 22:45:22 +01:00
parent 609829653e
commit 8c4ff7ed2a
7 changed files with 36 additions and 21 deletions

8
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "trilium", "name": "trilium",
"version": "0.45.0-beta", "version": "0.45.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -2658,9 +2658,9 @@
} }
}, },
"electron": { "electron": {
"version": "9.3.2", "version": "9.3.3",
"resolved": "https://registry.npmjs.org/electron/-/electron-9.3.2.tgz", "resolved": "https://registry.npmjs.org/electron/-/electron-9.3.3.tgz",
"integrity": "sha512-0lleEf9msAXGDi2GukAuiGdw3VDgSTlONOnJgqDEz1fuSEVsXz5RX+hNPKDsVDerLTFg/C34RuJf4LwHvkKcBA==", "integrity": "sha512-xghKeUY1qgnEcJ5w2rXo/toH+8NT2Dktx2aAxBNPV7CIJr3mejJJAPwLbycwtddzr37tgKxHeHlc8ivfKtMkJQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"@electron/get": "^1.0.1", "@electron/get": "^1.0.1",

View File

@ -77,7 +77,7 @@
}, },
"devDependencies": { "devDependencies": {
"cross-env": "7.0.2", "cross-env": "7.0.2",
"electron": "9.3.2", "electron": "9.3.3",
"electron-builder": "22.9.1", "electron-builder": "22.9.1",
"electron-packager": "15.1.0", "electron-packager": "15.1.0",
"electron-rebuild": "2.3.2", "electron-rebuild": "2.3.2",

View File

@ -57,8 +57,15 @@ function id() {
return randtoken.generate(10); return randtoken.generate(10);
} }
function note(title, type = 'text', mime = 'text/html') { function note(title, extraParams = {}) {
const note = new Note(noteCache, {noteId: id(), title, type, mime}); const row = Object.assign({
noteId: id(),
title: title,
type: 'text',
mime: 'text/html'
}, extraParams);
const note = new Note(noteCache, row);
return new NoteBuilder(note); return new NoteBuilder(note);
} }

View File

@ -247,6 +247,6 @@ describe("Invalid expressions", () => {
searchContext searchContext
}); });
expect(searchContext.error).toEqual('Misplaced or incomplete expression "="') expect(searchContext.error).toEqual('Relation can be compared only with property, e.g. ~relation.title=hello in ""')
}); });
}); });

View File

@ -53,8 +53,8 @@ describe("Search", () => {
it("normal search looks also at type and mime", () => { it("normal search looks also at type and mime", () => {
rootNote rootNote
.child(note("Effective Java", 'book', '')) .child(note("Effective Java", {type: 'book', mime:''}))
.child(note("Hello World.java", 'code', 'text/x-java')); .child(note("Hello World.java", {type: 'code', mime: 'text/x-java'}));
const searchContext = new SearchContext(); const searchContext = new SearchContext();
let searchResults = searchService.findNotesWithQuery('book', searchContext); let searchResults = searchService.findNotesWithQuery('book', searchContext);
@ -178,7 +178,7 @@ describe("Search", () => {
// dates should not be coerced into numbers which would then give wrong numbers // dates should not be coerced into numbers which would then give wrong numbers
rootNote rootNote
.child(note("My note") .child(note("My note", {dateCreated: dateUtils.localNowDateTime()})
.label('year', new Date().getFullYear().toString()) .label('year', new Date().getFullYear().toString())
.label('month', dateUtils.localNowDate().substr(0, 7)) .label('month', dateUtils.localNowDate().substr(0, 7))
.label('date', dateUtils.localNowDate()) .label('date', dateUtils.localNowDate())
@ -209,6 +209,8 @@ describe("Search", () => {
test("#month = month", 1); test("#month = month", 1);
test("#month = 'MONTH'", 0); test("#month = 'MONTH'", 0);
test("note.dateCreated =* month", 1);
test("#date = TODAY", 1); test("#date = TODAY", 1);
test("#date = today", 1); test("#date = today", 1);
test("#date = 'today'", 0); test("#date = 'today'", 0);
@ -586,7 +588,7 @@ describe("Search", () => {
const searchContext = new SearchContext(); const searchContext = new SearchContext();
let searchResults = searchService.findNotesWithQuery('# note.text *=* rati and note.noteId != root', searchContext); let searchResults = searchService.findNotesWithQuery('# note.text *=* vaki and note.noteId != root', searchContext);
expect(searchResults.length).toEqual(1); expect(searchResults.length).toEqual(1);
expect(noteCache.notes[searchResults[0].noteId].title).toEqual("Slovakia"); expect(noteCache.notes[searchResults[0].noteId].title).toEqual("Slovakia");
}); });

View File

@ -5,9 +5,9 @@ const stringComparators = {
">=": comparedValue => (val => val >= comparedValue), ">=": comparedValue => (val => val >= comparedValue),
"<": comparedValue => (val => val < comparedValue), "<": comparedValue => (val => val < comparedValue),
"<=": comparedValue => (val => val <= comparedValue), "<=": comparedValue => (val => val <= comparedValue),
"*=": comparedValue => (val => val.endsWith(comparedValue)), "*=": comparedValue => (val => val && val.endsWith(comparedValue)),
"=*": comparedValue => (val => val.startsWith(comparedValue)), "=*": comparedValue => (val => val && val.startsWith(comparedValue)),
"*=*": comparedValue => (val => val.includes(comparedValue)), "*=*": comparedValue => (val => val && val.includes(comparedValue)),
}; };
const numericComparators = { const numericComparators = {

View File

@ -80,10 +80,14 @@ function getExpression(tokens, searchContext, level = 0) {
if (i + 2 < tokens.length) { if (i + 2 < tokens.length) {
if (tokens[i + 1].token === '+') { if (tokens[i + 1].token === '+') {
delta += parseInt(tokens[i + 2].token); i += 2;
delta += parseInt(tokens[i].token);
} }
else if (tokens[i + 1].token === '-') { else if (tokens[i + 1].token === '-') {
delta -= parseInt(tokens[i + 2].token); i += 2;
delta -= parseInt(tokens[i].token);
} }
} }
@ -196,16 +200,18 @@ function getExpression(tokens, searchContext, level = 0) {
if (PropertyComparisonExp.isProperty(tokens[i].token)) { if (PropertyComparisonExp.isProperty(tokens[i].token)) {
const propertyName = tokens[i].token; const propertyName = tokens[i].token;
const operator = tokens[i + 1].token; const operator = tokens[i + 1].token;
const comparedValue = tokens[i + 2].token;
i += 2;
const comparedValue = resolveConstantOperand();
const comparator = buildComparator(operator, comparedValue); const comparator = buildComparator(operator, comparedValue);
if (!comparator) { if (!comparator) {
searchContext.addError(`Can't find operator '${operator}' in ${context(i)}`); searchContext.addError(`Can't find operator '${operator}' in ${context(i - 2)}`);
return; return;
} }
i += 2;
return new PropertyComparisonExp(propertyName, comparator); return new PropertyComparisonExp(propertyName, comparator);
} }