@digigov/text-search 0.1.0-8b33e4c8

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 (47) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +70 -0
  3. package/cjs/hook.js +65 -0
  4. package/cjs/hook.spec.js +568 -0
  5. package/cjs/index.js +27 -0
  6. package/cjs/search/__tests__/utils.spec.js +84 -0
  7. package/cjs/search/index.js +11 -0
  8. package/cjs/search/lang/gr/encoder.js +28 -0
  9. package/cjs/search/lang/gr/normalization-map.js +107 -0
  10. package/cjs/search/search-index.js +165 -0
  11. package/cjs/search/utils.js +100 -0
  12. package/cjs/test-utils/data.json +552 -0
  13. package/cjs/types.js +5 -0
  14. package/hook.d.ts +19 -0
  15. package/hook.js +54 -0
  16. package/hook.spec.d.ts +1 -0
  17. package/hook.spec.js +560 -0
  18. package/index.d.ts +3 -0
  19. package/index.js +3 -0
  20. package/package.json +22 -0
  21. package/search/__tests__/utils.spec.d.ts +1 -0
  22. package/search/__tests__/utils.spec.js +81 -0
  23. package/search/index.d.ts +2 -0
  24. package/search/index.js +2 -0
  25. package/search/lang/gr/encoder.d.ts +11 -0
  26. package/search/lang/gr/encoder.js +20 -0
  27. package/search/lang/gr/normalization-map.d.ts +100 -0
  28. package/search/lang/gr/normalization-map.js +100 -0
  29. package/search/package.json +6 -0
  30. package/search/search-index.d.ts +42 -0
  31. package/search/search-index.js +150 -0
  32. package/search/utils.d.ts +25 -0
  33. package/search/utils.js +88 -0
  34. package/src/hook.spec.ts +289 -0
  35. package/src/hook.ts +50 -0
  36. package/src/index.ts +4 -0
  37. package/src/search/__tests__/utils.spec.ts +73 -0
  38. package/src/search/index.ts +3 -0
  39. package/src/search/lang/gr/encoder.ts +27 -0
  40. package/src/search/lang/gr/normalization-map.ts +100 -0
  41. package/src/search/search-index.ts +103 -0
  42. package/src/search/utils.ts +72 -0
  43. package/src/test-utils/data.json +552 -0
  44. package/src/types.ts +65 -0
  45. package/test-utils/data.json +552 -0
  46. package/types.d.ts +56 -0
  47. package/types.js +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/cjs/hook.js ADDED
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports["default"] = useSearch;
9
+
10
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11
+
12
+ var _react = require("react");
13
+
14
+ var _search = _interopRequireDefault(require("./search"));
15
+
16
+ /**
17
+ * Hook for searching through a list of documents
18
+ *
19
+ * Returns a list of documents that match the search query.
20
+ * If no query is provided, it returns the original list of documents.
21
+ *
22
+ * @param documents - The list of documents to search through
23
+ * @param options - Options for configuring the search index
24
+ * @param query - The search query
25
+ *
26
+ * @typeParam T - The type of the data in the documents list
27
+ */
28
+ function useSearch(documents, query, options) {
29
+ var _useState = (0, _react.useState)(false),
30
+ _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
31
+ loading = _useState2[0],
32
+ setLoading = _useState2[1];
33
+
34
+ var _useState3 = (0, _react.useState)(documents),
35
+ _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
36
+ data = _useState4[0],
37
+ setData = _useState4[1];
38
+
39
+ var indexing = options === null || options === void 0 ? void 0 : options.indexing;
40
+ var index = (0, _react.useMemo)(function () {
41
+ return new _search["default"](documents, indexing);
42
+ }, [documents, indexing]);
43
+ var search = (0, _react.useCallback)(function () {
44
+ if (query) {
45
+ setLoading(true);
46
+ index.searchAsync(documents, query).then(function (data) {
47
+ return setData(data);
48
+ })["finally"](function () {
49
+ return setLoading(false);
50
+ });
51
+ } else {
52
+ setData(documents);
53
+ }
54
+ }, [query, index, documents]);
55
+ var reset = (0, _react.useCallback)(function () {
56
+ setData(documents);
57
+ setLoading(false);
58
+ }, [documents]);
59
+ return {
60
+ data: data,
61
+ loading: loading,
62
+ search: search,
63
+ reset: reset
64
+ };
65
+ }