diff --git a/spec/search/value_extractor.spec.js b/spec/search/value_extractor.spec.js index 0e44faf4a..08508a4bf 100644 --- a/spec/search/value_extractor.spec.js +++ b/spec/search/value_extractor.spec.js @@ -1,5 +1,5 @@ const {note} = require('./becca_mocking.js'); -const ValueExtractor = require('../../src/services/search/value_extractor.js'); +const ValueExtractor = require('../../src/services/search/value_extractor'); const becca = require('../../src/becca/becca.js'); const SearchContext = require('../../src/services/search/search_context'); diff --git a/src/becca/entities/bnote.ts b/src/becca/entities/bnote.ts index a39d62706..9886e7ae6 100644 --- a/src/becca/entities/bnote.ts +++ b/src/becca/entities/bnote.ts @@ -450,7 +450,7 @@ class BNote extends AbstractBeccaEntity { ); } - getAttributeCaseInsensitive(type: string, name: string, value: string | null) { + getAttributeCaseInsensitive(type: string, name: string, value?: string | null) { name = name.toLowerCase(); value = value ? value.toLowerCase() : null; diff --git a/src/services/search/services/parse.js b/src/services/search/services/parse.js index 84c717f6e..6b2e8fa84 100644 --- a/src/services/search/services/parse.js +++ b/src/services/search/services/parse.js @@ -16,7 +16,7 @@ const NoteContentFulltextExp = require('../expressions/note_content_fulltext.js' const OrderByAndLimitExp = require('../expressions/order_by_and_limit.js'); const AncestorExp = require('../expressions/ancestor.js'); const buildComparator = require('./build_comparator.js'); -const ValueExtractor = require('../value_extractor.js'); +const ValueExtractor = require('../value_extractor'); const utils = require('../../utils'); const TrueExp = require('../expressions/true.js'); const IsHiddenExp = require('../expressions/is_hidden.js'); diff --git a/src/services/search/value_extractor.js b/src/services/search/value_extractor.ts similarity index 89% rename from src/services/search/value_extractor.js rename to src/services/search/value_extractor.ts index 27aff0e6c..c602e5f49 100644 --- a/src/services/search/value_extractor.js +++ b/src/services/search/value_extractor.ts @@ -1,10 +1,12 @@ "use strict"; +import BNote = require("../../becca/entities/bnote"); + /** * Search string is lower cased for case-insensitive comparison. But when retrieving properties, * we need a case-sensitive form, so we have this translation object. */ -const PROP_MAPPING = { +const PROP_MAPPING: Record = { "noteid": "noteId", "title": "title", "type": "type", @@ -32,8 +34,14 @@ const PROP_MAPPING = { "revisioncount": "revisionCount" }; +interface SearchContext { + dbLoadNeeded: boolean; +} + class ValueExtractor { - constructor(searchContext, propertyPath) { + private propertyPath: string[]; + + constructor(searchContext: SearchContext, propertyPath: string[]) { this.propertyPath = propertyPath.map(pathEl => pathEl.toLowerCase()); if (this.propertyPath[0].startsWith('#')) { @@ -81,10 +89,10 @@ class ValueExtractor { } } - extract(note) { - let cursor = note; + extract(note: BNote) { + let cursor: BNote | null = note; - let i; + let i: number = 0; const cur = () => this.propertyPath[i]; @@ -105,8 +113,7 @@ class ValueExtractor { i++; const attr = cursor.getAttributeCaseInsensitive('relation', cur()); - - cursor = attr ? attr.targetNote : null; + cursor = (attr ? attr.targetNote || null : null); } else if (cur() === 'parents') { cursor = cursor.parents[0]; @@ -118,7 +125,7 @@ class ValueExtractor { return Math.random().toString(); // string is expected for comparison } else if (cur() in PROP_MAPPING) { - return cursor[PROP_MAPPING[cur()]]; + return (cursor as any)[PROP_MAPPING[cur()]]; } else { // FIXME