@digigov/text-search 0.1.0-a131264d

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +70 -0
  3. package/es/hook.js +54 -0
  4. package/es/hook.spec.js +560 -0
  5. package/es/index.js +3 -0
  6. package/es/search/__tests__/utils.spec.js +81 -0
  7. package/es/search/index.js +2 -0
  8. package/es/search/lang/gr/encoder.js +20 -0
  9. package/es/search/lang/gr/normalization-map.js +100 -0
  10. package/es/search/search-index.js +150 -0
  11. package/es/search/utils.js +88 -0
  12. package/es/test-utils/data.json +552 -0
  13. package/es/types.js +1 -0
  14. package/hook.d.ts +19 -0
  15. package/hook.js +65 -0
  16. package/hook.mjs +54 -0
  17. package/hook.spec.d.ts +1 -0
  18. package/hook.spec.js +568 -0
  19. package/hook.spec.mjs +560 -0
  20. package/index.d.ts +3 -0
  21. package/index.js +27 -0
  22. package/index.mjs +3 -0
  23. package/package.json +22 -0
  24. package/search/__tests__/utils.spec.d.ts +1 -0
  25. package/search/__tests__/utils.spec.js +84 -0
  26. package/search/__tests__/utils.spec.mjs +81 -0
  27. package/search/index.d.ts +2 -0
  28. package/search/index.js +11 -0
  29. package/search/index.mjs +2 -0
  30. package/search/lang/gr/encoder.d.ts +11 -0
  31. package/search/lang/gr/encoder.js +28 -0
  32. package/search/lang/gr/encoder.mjs +20 -0
  33. package/search/lang/gr/normalization-map.d.ts +100 -0
  34. package/search/lang/gr/normalization-map.js +107 -0
  35. package/search/lang/gr/normalization-map.mjs +100 -0
  36. package/search/search-index.d.ts +42 -0
  37. package/search/search-index.js +165 -0
  38. package/search/search-index.mjs +150 -0
  39. package/search/utils.d.ts +25 -0
  40. package/search/utils.js +100 -0
  41. package/search/utils.mjs +88 -0
  42. package/src/hook.spec.ts +289 -0
  43. package/src/hook.ts +50 -0
  44. package/src/index.ts +4 -0
  45. package/src/search/__tests__/utils.spec.ts +73 -0
  46. package/src/search/index.ts +3 -0
  47. package/src/search/lang/gr/encoder.ts +27 -0
  48. package/src/search/lang/gr/normalization-map.ts +100 -0
  49. package/src/search/search-index.ts +103 -0
  50. package/src/search/utils.ts +72 -0
  51. package/src/test-utils/data.json +552 -0
  52. package/src/types.ts +65 -0
  53. package/test-utils/data.json +552 -0
  54. package/types.d.ts +56 -0
  55. package/types.js +5 -0
  56. package/types.mjs +1 -0
package/LICENSE ADDED
File without changes
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # @digigov/text-search
2
+
3
+ `@digigov/text-search` is a library that provides text search functionality in the form of a React hook. It allows searching through a list of objects by creating an index of the objects' properties. It uses [flexsearch](https://github.com/nextapps-de/flexsearch) under the hood.
4
+
5
+ The objects in the list can have any structure, but an identifier property must be provided. The hook will use the `id` property (if present) or it will create an `id` property for each object, using the index of the object in the list.
6
+ Alternatively, you may specify which property to use as an identifier by passing it in the `idKey` of the hook's options. Obviously, the identifier property must be unique for each object.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install @digigov/text-search
12
+ ```
13
+
14
+ or
15
+
16
+ ```bash
17
+ yarn add @digigov/text-search
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ Here is a basic example of how to use the hook:
23
+
24
+ ```typescript
25
+ import React from 'react';
26
+ import useSearch from '@digigov/text-search';
27
+
28
+ const documents = [
29
+ { name: 'John', address: '123 Main St' },
30
+ { name: 'Jane', address: '456 Main St' },
31
+ { name: 'Jack', address: '789 Main St' },
32
+ { name: 'Jill', address: '101 Main St' },
33
+ ];
34
+
35
+ const MyComponent = () => {
36
+ const [searchTerm, setSearchTerm] = React.useState('');
37
+ const { data, loading, search, reset } = useSearch(documents, searchTerm);
38
+
39
+ const handleReset = () => {
40
+ setSearchTerm('');
41
+ reset();
42
+ };
43
+
44
+ return (
45
+ <div>
46
+ <input
47
+ type="text"
48
+ value={searchTerm}
49
+ onChange={(e) => setSearchTerm(e.target.value)}
50
+ placeholder="Enter search term"
51
+ />
52
+ <button onClick={search}>Search</button>
53
+ <button onClick={handleReset}>Reset</button>
54
+
55
+ {loading && <p>Loading...</p>}
56
+
57
+ <ul>
58
+ {data.map((item) => (
59
+ <li key={item.name}>
60
+ <p>{item.name}</p>
61
+ <p>{item.address}</p>
62
+ </li>
63
+ ))}
64
+ </ul>
65
+ </div>
66
+ );
67
+ };
68
+
69
+ export default MyComponent;
70
+ ```
package/es/hook.js ADDED
@@ -0,0 +1,54 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import { useCallback, useMemo, useState } from 'react';
3
+ import SearchIndex from "./search";
4
+
5
+ /**
6
+ * Hook for searching through a list of documents
7
+ *
8
+ * Returns a list of documents that match the search query.
9
+ * If no query is provided, it returns the original list of documents.
10
+ *
11
+ * @param documents - The list of documents to search through
12
+ * @param options - Options for configuring the search index
13
+ * @param query - The search query
14
+ *
15
+ * @typeParam T - The type of the data in the documents list
16
+ */
17
+ export default function useSearch(documents, query, options) {
18
+ var _useState = useState(false),
19
+ _useState2 = _slicedToArray(_useState, 2),
20
+ loading = _useState2[0],
21
+ setLoading = _useState2[1];
22
+
23
+ var _useState3 = useState(documents),
24
+ _useState4 = _slicedToArray(_useState3, 2),
25
+ data = _useState4[0],
26
+ setData = _useState4[1];
27
+
28
+ var indexing = options === null || options === void 0 ? void 0 : options.indexing;
29
+ var index = useMemo(function () {
30
+ return new SearchIndex(documents, indexing);
31
+ }, [documents, indexing]);
32
+ var search = useCallback(function () {
33
+ if (query) {
34
+ setLoading(true);
35
+ index.searchAsync(documents, query).then(function (data) {
36
+ return setData(data);
37
+ })["finally"](function () {
38
+ return setLoading(false);
39
+ });
40
+ } else {
41
+ setData(documents);
42
+ }
43
+ }, [query, index, documents]);
44
+ var reset = useCallback(function () {
45
+ setData(documents);
46
+ setLoading(false);
47
+ }, [documents]);
48
+ return {
49
+ data: data,
50
+ loading: loading,
51
+ search: search,
52
+ reset: reset
53
+ };
54
+ }