server-ts: Port services/search/services/lex

This commit is contained in:
Elian Doran 2024-02-18 01:46:22 +02:00
parent deed58c2fc
commit 533a597a5c
No known key found for this signature in database
3 changed files with 17 additions and 11 deletions

View File

@ -1,4 +1,4 @@
const lex = require('../../src/services/search/services/lex.js'); const lex = require('../../src/services/search/services/lex');
describe("Lexer fulltext", () => { describe("Lexer fulltext", () => {
it("simple lexing", () => { it("simple lexing", () => {

View File

@ -1,16 +1,22 @@
function lex(str) { interface TokenData {
token: string;
inQuotes: boolean;
startIndex: number;
endIndex: number;
}
function lex(str: string) {
str = str.toLowerCase(); str = str.toLowerCase();
let fulltextQuery = ""; let fulltextQuery = "";
const fulltextTokens = []; const fulltextTokens: TokenData[] = [];
const expressionTokens = []; const expressionTokens: TokenData[] = [];
/** @type {boolean|string} */ let quotes: boolean | string = false; // otherwise contains used quote - ', " or `
let quotes = false; // otherwise contains used quote - ', " or `
let fulltextEnded = false; let fulltextEnded = false;
let currentWord = ''; let currentWord = '';
function isSymbolAnOperator(chr) { function isSymbolAnOperator(chr: string) {
return ['=', '*', '>', '<', '!', "-", "+", '%', ','].includes(chr); return ['=', '*', '>', '<', '!', "-", "+", '%', ','].includes(chr);
} }
@ -23,12 +29,12 @@ function lex(str) {
} }
} }
function finishWord(endIndex, createAlsoForEmptyWords = false) { function finishWord(endIndex: number, createAlsoForEmptyWords = false) {
if (currentWord === '' && !createAlsoForEmptyWords) { if (currentWord === '' && !createAlsoForEmptyWords) {
return; return;
} }
const rec = { const rec: TokenData = {
token: currentWord, token: currentWord,
inQuotes: !!quotes, inQuotes: !!quotes,
startIndex: endIndex - currentWord.length + 1, startIndex: endIndex - currentWord.length + 1,
@ -146,4 +152,4 @@ function lex(str) {
} }
} }
module.exports = lex; export = lex;

View File

@ -1,7 +1,7 @@
"use strict"; "use strict";
const normalizeString = require("normalize-strings"); const normalizeString = require("normalize-strings");
const lex = require('./lex.js'); const lex = require('./lex');
const handleParens = require('./handle_parens'); const handleParens = require('./handle_parens');
const parse = require('./parse.js'); const parse = require('./parse.js');
const SearchResult = require('../search_result'); const SearchResult = require('../search_result');