Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/search/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ export function parse_query(q: string): Record<string, string[]> {
const result: Record<string, string[]> = {};

for (const match of matches) {
if (/[=:]/.test(match)) {
if (/^["\s]/.test(match) && /["\s]$/.test(match)) {
const stripped = match.replace(/^["\s]|["\s]$/g, "");

if ("_" in result) {
result._.push(stripped);
} else {
result._ = [stripped];
}
} else if (/[=:]/.test(match)) {
const [key, value] = match.split(/[:=]/, 2);
const stripped = value.replace(/^["\s]|["\s]$/g, "");

Expand Down
39 changes: 39 additions & 0 deletions packages/search/tests/parse-query.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { parse_query } from "../src";

describe("parse query", () => {
it("should recognize key:value", () => {
const q = parse_query("foo:bar");
expect(q.foo).toEqual(["bar"]);
});

it("should recognize key=value", () => {
const q = parse_query("foo=bar");
expect(q.foo).toEqual(["bar"]);
});

it("should recognize key:\"value with spaces\"", () => {
const q = parse_query("foo:\"bar baz\"");
expect(q.foo).toEqual(["bar baz"]);
});

it("should recognize key=\"value with spaces\"", () => {
const q = parse_query("foo=\"bar baz\"");
expect(q.foo).toEqual(["bar baz"]);
});

it("should collect wild value", () => {
const q = parse_query("foo bar baz");
expect(q._.sort()).toEqual(["foo", "bar", "baz"].sort());
});

it("should collect \"wild value with spaces\"", () => {
const q = parse_query("foo \"bar baz\"");
expect(q._.sort()).toEqual(["foo", "bar baz"].sort());
});

it("should escape `=` inside quotes", () => {
const q = parse_query("\"foo=bar\"");
expect(q).not.toHaveProperty("foo");
expect(q._).toEqual(["foo=bar"]);
});
});