From 322dc88c59f99b42317e4db6b4ce62b78c31c963 Mon Sep 17 00:00:00 2001 From: Robin Pokorny Date: Wed, 6 Jan 2021 03:48:58 +0100 Subject: [PATCH] feat: add ability to build query (#9) --- README.md | 12 ++++++------ src/index.ts | 5 +++-- tests/index.test.ts | 8 ++++++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 052a42c..030155a 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ npm install --save react-lunr ### `useLunr` ```js -useLunr(query?: string, index?: lunr.Index | JSON | string, store: object | string) => object[] +useLunr(query?: string | lunr.Index.QueryBuilder, index?: lunr.Index | JSON | string, store: object | string) => object[] ``` The `useLunr` [hook][hooks] takes your search query, index, and store and @@ -27,11 +27,11 @@ searching. #### Parameters -| Name | Type | Description | -| ----------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------- | -| **`query`** | `string` | The search query. As this value updates, the return value will be updated. | -| **`index`** | `lunr.Index \| JSON \| string` | The Lunr index. This can be an instance of a Lunr index or one that has been exported via `index.toJSON` or `JSON.stringify`. | -| **`store`** | `JSON \| string` | Object mapping a result `ref` to an object of data. This can be an object or an object that has been exported via `JSON.stringified`. | +| Name | Type | Description | +| ----------- | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | +| **`query`** | `string` \| `lunr.Index.QueryBuilder` | The search query. As this value updates, the return value will be updated. This can be a string or a query builder. | +| **`index`** | `lunr.Index \| JSON \| string` | The Lunr index. This can be an instance of a Lunr index or one that has been exported via `index.toJSON` or `JSON.stringify`. | +| **`store`** | `JSON \| string` | Object mapping a result `ref` to an object of data. This can be an object or an object that has been exported via `JSON.stringified`. | If `store` is not provided, the raw results data from Lunr will be returned instead. This includes the `ref` used when creating the index. diff --git a/src/index.ts b/src/index.ts index ceac6e6..4ad6df4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import invariant from 'tiny-invariant' type Store = Record export const useLunr = ( - query?: string, + query?: string | Index.QueryBuilder, rawIndex?: Index | object | string, rawStore?: Store | string, ) => { @@ -37,7 +37,8 @@ export const useLunr = ( return useMemo(() => { if (!query || !index) return [] - const results = index.search(query) + const results = + typeof query === 'string' ? index.search(query) : index.query(query) if (store) return results.map(({ ref }) => store[ref]) diff --git a/tests/index.test.ts b/tests/index.test.ts index 8c5b2fc..9892a2b 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -103,4 +103,12 @@ describe('useLunr', () => { const { result } = renderHook(() => useLunr(documents[0].name, 0 as any)) expect(result.error.message).toMatch(/invalid index provided/i) }) + + test.only('returns results if builded query has matches', () => { + const queryBuilder: lunr.Index.QueryBuilder = (q) => { + q.term(documents[0].name.toLowerCase(), {}) + } + const { result } = renderHook(() => useLunr(queryBuilder, index, store)) + expect(result.current).toEqual([documents[0]]) + }) })