mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
server-ts: Port services/search/services/handle_parens
This commit is contained in:
parent
8acb64198c
commit
deed58c2fc
@ -1,4 +1,4 @@
|
|||||||
const handleParens = require('../../src/services/search/services/handle_parens.js');
|
const handleParens = require('../../src/services/search/services/handle_parens');
|
||||||
|
|
||||||
describe("Parens handler", () => {
|
describe("Parens handler", () => {
|
||||||
it("handles parens", () => {
|
it("handles parens", () => {
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
|
interface Token {
|
||||||
|
token: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This will create a recursive object from a list of tokens - tokens between parenthesis are grouped in a single array
|
* This will create a recursive object from a list of tokens - tokens between parenthesis are grouped in a single array
|
||||||
*/
|
*/
|
||||||
function handleParens(tokens) {
|
function handleParens(tokens: (Token | Token[])[]) {
|
||||||
if (tokens.length === 0) {
|
if (tokens.length === 0) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const leftIdx = tokens.findIndex(token => token.token === '(');
|
const leftIdx = tokens.findIndex(token => "token" in token && token.token === '(');
|
||||||
|
|
||||||
if (leftIdx === -1) {
|
if (leftIdx === -1) {
|
||||||
return tokens;
|
return tokens;
|
||||||
@ -17,13 +21,18 @@ function handleParens(tokens) {
|
|||||||
let parensLevel = 0
|
let parensLevel = 0
|
||||||
|
|
||||||
for (rightIdx = leftIdx; rightIdx < tokens.length; rightIdx++) {
|
for (rightIdx = leftIdx; rightIdx < tokens.length; rightIdx++) {
|
||||||
if (tokens[rightIdx].token === ')') {
|
const token = tokens[rightIdx];
|
||||||
|
if (!("token" in token)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token.token === ')') {
|
||||||
parensLevel--;
|
parensLevel--;
|
||||||
|
|
||||||
if (parensLevel === 0) {
|
if (parensLevel === 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (tokens[rightIdx].token === '(') {
|
} else if (token.token === '(') {
|
||||||
parensLevel++;
|
parensLevel++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -36,8 +45,8 @@ function handleParens(tokens) {
|
|||||||
...tokens.slice(0, leftIdx),
|
...tokens.slice(0, leftIdx),
|
||||||
handleParens(tokens.slice(leftIdx + 1, rightIdx)),
|
handleParens(tokens.slice(leftIdx + 1, rightIdx)),
|
||||||
...tokens.slice(rightIdx + 1)
|
...tokens.slice(rightIdx + 1)
|
||||||
];
|
] as (Token | Token[])[];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = handleParens;
|
export = handleParens;
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const normalizeString = require("normalize-strings");
|
const normalizeString = require("normalize-strings");
|
||||||
const lex = require('./lex.js');
|
const lex = require('./lex.js');
|
||||||
const handleParens = require('./handle_parens.js');
|
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');
|
||||||
const SearchContext = require('../search_context');
|
const SearchContext = require('../search_context');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user