From c9f66b831b2de2c59a3644067c86163e17d0f4a9 Mon Sep 17 00:00:00 2001 From: bogay Date: Fri, 13 Jan 2023 02:23:07 +0800 Subject: [PATCH 1/2] test(search): parse query --- packages/search/tests/parse-query.test.ts | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 packages/search/tests/parse-query.test.ts diff --git a/packages/search/tests/parse-query.test.ts b/packages/search/tests/parse-query.test.ts new file mode 100644 index 0000000..69a4562 --- /dev/null +++ b/packages/search/tests/parse-query.test.ts @@ -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"]); + }); +}); From 8b82cca418f2c4a0380922db08a3299698630b83 Mon Sep 17 00:00:00 2001 From: JacobLinCool Date: Sun, 15 Jan 2023 14:59:45 +0000 Subject: [PATCH 2/2] fix(search): broken test, `=` inside quotes --- packages/search/src/parser.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/search/src/parser.ts b/packages/search/src/parser.ts index 80d869c..7071478 100644 --- a/packages/search/src/parser.ts +++ b/packages/search/src/parser.ts @@ -15,7 +15,15 @@ export function parse_query(q: string): Record { const result: Record = {}; 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, "");