Skip to content

Commit

Permalink
feat: add ability to build query (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinpokorny authored Jan 6, 2021
1 parent a3ec653 commit 322dc88
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import invariant from 'tiny-invariant'
type Store<T> = Record<string | number | symbol, T>

export const useLunr = <T = unknown>(
query?: string,
query?: string | Index.QueryBuilder,
rawIndex?: Index | object | string,
rawStore?: Store<T> | string,
) => {
Expand Down Expand Up @@ -37,7 +37,8 @@ export const useLunr = <T = unknown>(
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])

Expand Down
8 changes: 8 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
})
})

0 comments on commit 322dc88

Please # to comment.