Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(search-parser): add support for empty expressions (#745)

Corentin Thomasset2ff4adf9572d
diff --git a/packages/search-parser/src/index.ts b/packages/search-parser/src/index.ts
index 25406bfa..7c0763a6 100644
--- a/packages/search-parser/src/index.ts
+++ b/packages/search-parser/src/index.ts
@@ -8,6 +8,7 @@ export {
export type {
AndExpression,
+ EmptyExpression,
Expression,
FilterExpression,
Issue,
diff --git a/packages/search-parser/src/parser.test.ts b/packages/search-parser/src/parser.test.ts
index 887083b6..d4735767 100644
--- a/packages/search-parser/src/parser.test.ts
+++ b/packages/search-parser/src/parser.test.ts
@@ -5,7 +5,17 @@ describe('parseSearchQuery', () => {
describe('when parsing simple queries', () => {
test('returns empty expression and no search terms for empty query', () => {
expect(parseSearchQuery({ query: '' })).toEqual({
- expression: { type: 'and', operands: [] },
+ expression: { type: 'empty' },
+ issues: [],
+ });
+
+ expect(parseSearchQuery({ query: ' ' })).toEqual({
+ expression: { type: 'empty' },
+ issues: [],
+ });
+
+ expect(parseSearchQuery({ query: ' \n \t ' })).toEqual({
+ expression: { type: 'empty' },
issues: [],
});
});
diff --git a/packages/search-parser/src/parser.ts b/packages/search-parser/src/parser.ts
index 24ccb1a1..748f8a41 100644
--- a/packages/search-parser/src/parser.ts
+++ b/packages/search-parser/src/parser.ts
@@ -197,7 +197,7 @@ function parseExpression({ tokens, maxDepth }: { tokens: Token[]; maxDepth: numb
}
// Build final result
- const finalExpression: Expression = expression ?? { type: 'and', operands: [] };
+ const finalExpression: Expression = expression ?? { type: 'empty' };
return {
expression: finalExpression,
diff --git a/packages/search-parser/src/parser.types.ts b/packages/search-parser/src/parser.types.ts
index 92466813..2ef7358d 100644
--- a/packages/search-parser/src/parser.types.ts
+++ b/packages/search-parser/src/parser.types.ts
@@ -13,7 +13,12 @@ export type Expression
| OrExpression
| NotExpression
| FilterExpression
- | TextExpression;
+ | TextExpression
+ | EmptyExpression;
+
+export type EmptyExpression = {
+ type: 'empty';
+};
export type AndExpression = {
type: 'and';