From 80e6ced5db918e63aa53b0a2438020a842f853dc Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 18 Feb 2024 00:30:16 +0200 Subject: [PATCH] server-ts: Port services/search/search_result --- .../{search_result.js => search_result.ts} | 20 +++++++++++++------ src/services/search/services/search.js | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) rename src/services/search/{search_result.js => search_result.ts} (77%) diff --git a/src/services/search/search_result.js b/src/services/search/search_result.ts similarity index 77% rename from src/services/search/search_result.js rename to src/services/search/search_result.ts index ca3811f8e..61075910f 100644 --- a/src/services/search/search_result.js +++ b/src/services/search/search_result.ts @@ -1,10 +1,14 @@ "use strict"; -const beccaService = require('../../becca/becca_service'); -const becca = require('../../becca/becca'); +import beccaService = require('../../becca/becca_service'); +import becca = require('../../becca/becca'); class SearchResult { - constructor(notePathArray) { + private notePathArray: string[]; + private notePathTitle: string; + private score?: number; + + constructor(notePathArray: string[]) { this.notePathArray = notePathArray; this.notePathTitle = beccaService.getNoteTitleForPath(notePathArray); } @@ -17,7 +21,7 @@ class SearchResult { return this.notePathArray[this.notePathArray.length - 1]; } - computeScore(fulltextQuery, tokens) { + computeScore(fulltextQuery: string, tokens: string[]) { this.score = 0; const note = becca.notes[this.noteId]; @@ -42,9 +46,13 @@ class SearchResult { } } - addScoreForStrings(tokens, str, factor) { + addScoreForStrings(tokens: string[], str: string, factor: number) { const chunks = str.toLowerCase().split(" "); + if (!this.score) { + this.score = 0; + } + for (const chunk of chunks) { for (const token of tokens) { if (chunk === token) { @@ -59,4 +67,4 @@ class SearchResult { } } -module.exports = SearchResult; +export = SearchResult; diff --git a/src/services/search/services/search.js b/src/services/search/services/search.js index 36a654099..4c2291a33 100644 --- a/src/services/search/services/search.js +++ b/src/services/search/services/search.js @@ -4,7 +4,7 @@ const normalizeString = require("normalize-strings"); const lex = require('./lex.js'); const handleParens = require('./handle_parens.js'); const parse = require('./parse.js'); -const SearchResult = require('../search_result.js'); +const SearchResult = require('../search_result'); const SearchContext = require('../search_context'); const becca = require('../../../becca/becca'); const beccaService = require('../../../becca/becca_service');