@digigov/text-search 1.2.0-rc.32 → 2.0.0-16fbe090
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.
- package/hook.d.ts +1 -1
- package/hook.js +34 -0
- package/index.d.ts +2 -2
- package/index.js +5 -8
- package/package.json +4 -4
- package/search/index.d.ts +1 -1
- package/search/index.js +3 -6
- package/search/lang/gr/encoder.js +8 -0
- package/search/lang/gr/normalization-map.js +101 -0
- package/search/search-index.d.ts +1 -1
- package/search/search-index.js +46 -0
- package/search/utils.js +26 -0
- package/types.js +0 -0
- package/cjs/hook/index.js +0 -58
- package/cjs/hook.js.map +0 -7
- package/cjs/index.js +0 -42
- package/cjs/index.js.map +0 -7
- package/cjs/search/index.js +0 -26
- package/cjs/search/index.js.map +0 -7
- package/cjs/search/lang/gr/encoder/index.js +0 -41
- package/cjs/search/lang/gr/encoder.js.map +0 -7
- package/cjs/search/lang/gr/normalization-map/index.js +0 -128
- package/cjs/search/lang/gr/normalization-map.js.map +0 -7
- package/cjs/search/search-index/index.js +0 -106
- package/cjs/search/search-index.js.map +0 -7
- package/cjs/search/utils/index.js +0 -68
- package/cjs/search/utils.js.map +0 -7
- package/cjs/types/index.js +0 -17
- package/cjs/types.js.map +0 -7
- package/hook/index.js +0 -28
- package/hook/package.json +0 -6
- package/hook.js.map +0 -7
- package/hook.spec.d.ts +0 -1
- package/index.js.map +0 -7
- package/search/__tests__/utils.spec.d.ts +0 -1
- package/search/index.js.map +0 -7
- package/search/lang/gr/encoder/index.js +0 -17
- package/search/lang/gr/encoder/package.json +0 -6
- package/search/lang/gr/encoder.js.map +0 -7
- package/search/lang/gr/normalization-map/index.js +0 -104
- package/search/lang/gr/normalization-map/package.json +0 -6
- package/search/lang/gr/normalization-map.js.map +0 -7
- package/search/package.json +0 -6
- package/search/search-index/index.js +0 -86
- package/search/search-index/package.json +0 -6
- package/search/search-index.js.map +0 -7
- package/search/utils/index.js +0 -42
- package/search/utils/package.json +0 -6
- package/search/utils.js.map +0 -7
- package/types/index.js +0 -1
- package/types/package.json +0 -6
- package/types.js.map +0 -7
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var search_index_exports = {};
|
|
20
|
-
__export(search_index_exports, {
|
|
21
|
-
SearchIndex: () => SearchIndex
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(search_index_exports);
|
|
24
|
-
var import_flexsearch = require("flexsearch");
|
|
25
|
-
var import_encoder = require("@digigov/text-search/search/lang/gr/encoder");
|
|
26
|
-
var import_utils = require("@digigov/text-search/search/utils");
|
|
27
|
-
class SearchIndex {
|
|
28
|
-
constructor(items, options) {
|
|
29
|
-
this.idKey = "id";
|
|
30
|
-
if (options && options.idKey) {
|
|
31
|
-
this.idKey = options.idKey.replace(".", ":");
|
|
32
|
-
} else if (!(items[0] && "id" in items[0])) {
|
|
33
|
-
items = items.map((item, index) => ({ ...item, id: index }));
|
|
34
|
-
}
|
|
35
|
-
let fields = [];
|
|
36
|
-
if (options && options.fields) {
|
|
37
|
-
fields = options.fields;
|
|
38
|
-
} else if (items[0]) {
|
|
39
|
-
fields = (0, import_utils.getAllItemKeys)(items[0]);
|
|
40
|
-
}
|
|
41
|
-
fields = fields.filter((field) => field !== this.idKey).map((field) => field.replace(".", ":"));
|
|
42
|
-
this.index = new import_flexsearch.Document({
|
|
43
|
-
document: {
|
|
44
|
-
id: this.idKey,
|
|
45
|
-
index: fields
|
|
46
|
-
},
|
|
47
|
-
tokenize: "forward",
|
|
48
|
-
worker: options?.enableWorker,
|
|
49
|
-
encode: import_encoder.encodeGreek
|
|
50
|
-
});
|
|
51
|
-
this.addAll(items);
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Add an item to the index
|
|
55
|
-
*
|
|
56
|
-
* @param item - The item to add to the index
|
|
57
|
-
*/
|
|
58
|
-
add(item) {
|
|
59
|
-
this.index.add(item);
|
|
60
|
-
return this;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Add multiple items to the index
|
|
64
|
-
*
|
|
65
|
-
* @param items - The list of items to add to the index
|
|
66
|
-
*/
|
|
67
|
-
addAll(items) {
|
|
68
|
-
items.forEach((item) => this.index.add(item));
|
|
69
|
-
return this;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Search the index for the given term
|
|
73
|
-
*
|
|
74
|
-
* @param documents - The list of documents to search
|
|
75
|
-
* @param searchTerm - The term to search for
|
|
76
|
-
* @returns The list of items that match the search term
|
|
77
|
-
*/
|
|
78
|
-
search(documents, searchTerm) {
|
|
79
|
-
const res = this.index.search(searchTerm);
|
|
80
|
-
const ids = (0, import_utils.getResultIds)(res);
|
|
81
|
-
return (0, import_utils.findItemsByIds)(
|
|
82
|
-
documents,
|
|
83
|
-
ids,
|
|
84
|
-
this.idKey
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Asynchronously search the index for the given term
|
|
89
|
-
*
|
|
90
|
-
* @param documents - The list of documents to search
|
|
91
|
-
* @param searchTerm - The term to search for
|
|
92
|
-
* @returns The list of items that match the search term
|
|
93
|
-
*/
|
|
94
|
-
async searchAsync(documents, searchTerm) {
|
|
95
|
-
return this.index.searchAsync(searchTerm).then((res) => {
|
|
96
|
-
return (0, import_utils.getResultIds)(res);
|
|
97
|
-
}).then(
|
|
98
|
-
(res) => (0, import_utils.findItemsByIds)(documents, res, this.idKey)
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
103
|
-
0 && (module.exports = {
|
|
104
|
-
SearchIndex
|
|
105
|
-
});
|
|
106
|
-
//# sourceMappingURL=search-index.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/search/search-index.ts"],
|
|
4
|
-
"sourcesContent": ["import { Document } from 'flexsearch';\nimport { encodeGreek } from '@digigov/text-search/search/lang/gr/encoder';\nimport {\n findItemsByIds,\n getAllItemKeys,\n getResultIds,\n} from '@digigov/text-search/search/utils';\nimport type { SearchIndexOptions } from '@digigov/text-search/types';\n\n/**\n * Wrapper class around third party index and search library\n *\n * @typeParam T - The type of the data that will be indexed\n */\nexport class SearchIndex<T extends Record<string, any>> {\n /**\n * Instance of the third party library's index\n */\n private index: Document<T>;\n private idKey: string;\n\n constructor(items: T[], options?: SearchIndexOptions<T>) {\n this.idKey = 'id';\n if (options && options.idKey) {\n this.idKey = options.idKey.replace('.', ':');\n } else if (!(items[0] && 'id' in items[0])) {\n items = items.map((item, index) => ({ ...item, id: index }));\n }\n\n let fields: string[] = [];\n if (options && options.fields) {\n fields = options.fields;\n } else if (items[0]) {\n fields = getAllItemKeys(items[0]);\n }\n fields = fields\n .filter((field) => field !== this.idKey)\n .map((field) => field.replace('.', ':'));\n\n this.index = new Document<T>({\n document: {\n id: this.idKey,\n index: fields,\n },\n tokenize: 'forward',\n worker: options?.enableWorker,\n encode: encodeGreek,\n });\n\n this.addAll(items);\n }\n\n /**\n * Add an item to the index\n *\n * @param item - The item to add to the index\n */\n public add(item: T) {\n this.index.add(item);\n return this;\n }\n\n /**\n * Add multiple items to the index\n *\n * @param items - The list of items to add to the index\n */\n public addAll(items: T[]) {\n items.forEach((item) => this.index.add(item));\n return this;\n }\n\n /**\n * Search the index for the given term\n *\n * @param documents - The list of documents to search\n * @param searchTerm - The term to search for\n * @returns The list of items that match the search term\n */\n public search(documents: T[], searchTerm: string) {\n const res = this.index.search(searchTerm);\n const ids = getResultIds(res);\n return findItemsByIds(\n documents,\n ids,\n this.idKey as Extract<keyof T, string>\n );\n }\n\n /**\n * Asynchronously search the index for the given term\n *\n * @param documents - The list of documents to search\n * @param searchTerm - The term to search for\n * @returns The list of items that match the search term\n */\n public async searchAsync(documents: T[], searchTerm: string) {\n return this.index\n .searchAsync(searchTerm)\n .then((res) => {\n return getResultIds(res);\n })\n .then((res) =>\n findItemsByIds(documents, res, this.idKey as Extract<keyof T, string>)\n );\n }\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAyB;AACzB,qBAA4B;AAC5B,mBAIO;AAQA,MAAM,YAA2C;AAAA,EAOtD,YAAY,OAAY,SAAiC;AACvD,SAAK,QAAQ;AACb,QAAI,WAAW,QAAQ,OAAO;AAC5B,WAAK,QAAQ,QAAQ,MAAM,QAAQ,KAAK,GAAG;AAAA,IAC7C,WAAW,EAAE,MAAM,CAAC,KAAK,QAAQ,MAAM,CAAC,IAAI;AAC1C,cAAQ,MAAM,IAAI,CAAC,MAAM,WAAW,EAAE,GAAG,MAAM,IAAI,MAAM,EAAE;AAAA,IAC7D;AAEA,QAAI,SAAmB,CAAC;AACxB,QAAI,WAAW,QAAQ,QAAQ;AAC7B,eAAS,QAAQ;AAAA,IACnB,WAAW,MAAM,CAAC,GAAG;AACnB,mBAAS,6BAAe,MAAM,CAAC,CAAC;AAAA,IAClC;AACA,aAAS,OACN,OAAO,CAAC,UAAU,UAAU,KAAK,KAAK,EACtC,IAAI,CAAC,UAAU,MAAM,QAAQ,KAAK,GAAG,CAAC;AAEzC,SAAK,QAAQ,IAAI,2BAAY;AAAA,MAC3B,UAAU;AAAA,QACR,IAAI,KAAK;AAAA,QACT,OAAO;AAAA,MACT;AAAA,MACA,UAAU;AAAA,MACV,QAAQ,SAAS;AAAA,MACjB,QAAQ;AAAA,IACV,CAAC;AAED,SAAK,OAAO,KAAK;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,IAAI,MAAS;AAClB,SAAK,MAAM,IAAI,IAAI;AACnB,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,OAAO,OAAY;AACxB,UAAM,QAAQ,CAAC,SAAS,KAAK,MAAM,IAAI,IAAI,CAAC;AAC5C,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASO,OAAO,WAAgB,YAAoB;AAChD,UAAM,MAAM,KAAK,MAAM,OAAO,UAAU;AACxC,UAAM,UAAM,2BAAa,GAAG;AAC5B,eAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA,KAAK;AAAA,IACP;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAa,YAAY,WAAgB,YAAoB;AAC3D,WAAO,KAAK,MACT,YAAY,UAAU,EACtB,KAAK,CAAC,QAAQ;AACb,iBAAO,2BAAa,GAAG;AAAA,IACzB,CAAC,EACA;AAAA,MAAK,CAAC,YACL,6BAAe,WAAW,KAAK,KAAK,KAAiC;AAAA,IACvE;AAAA,EACJ;AACF;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var utils_exports = {};
|
|
20
|
-
__export(utils_exports, {
|
|
21
|
-
findItemsByIds: () => findItemsByIds,
|
|
22
|
-
getAllItemKeys: () => getAllItemKeys,
|
|
23
|
-
getResultIds: () => getResultIds
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(utils_exports);
|
|
26
|
-
function getResultIds(result) {
|
|
27
|
-
const set = /* @__PURE__ */ new Set();
|
|
28
|
-
result.forEach((item) => {
|
|
29
|
-
item.result.forEach((id) => set.add(id));
|
|
30
|
-
});
|
|
31
|
-
return Array.from(set);
|
|
32
|
-
}
|
|
33
|
-
function findItemsByIds(items, ids, idKey) {
|
|
34
|
-
return items.filter(
|
|
35
|
-
(item) => item && ids.includes(getValueFromNestedKey(item, idKey))
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
function getValueFromNestedKey(object, nestedKey) {
|
|
39
|
-
const keys = nestedKey.split(":");
|
|
40
|
-
let result = object;
|
|
41
|
-
for (const keySegment of keys) {
|
|
42
|
-
if (Object.prototype.hasOwnProperty.call(result, keySegment)) {
|
|
43
|
-
result = result[keySegment];
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return result;
|
|
47
|
-
}
|
|
48
|
-
function getAllItemKeys(object, parentKey = "") {
|
|
49
|
-
let keys = [];
|
|
50
|
-
for (const key in object) {
|
|
51
|
-
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
52
|
-
const currentKey = parentKey ? `${parentKey}.${key}` : key;
|
|
53
|
-
if (typeof object[key] === "object" && object[key] !== null) {
|
|
54
|
-
keys = keys.concat(getAllItemKeys(object[key], currentKey));
|
|
55
|
-
} else {
|
|
56
|
-
keys.push(currentKey);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return keys;
|
|
61
|
-
}
|
|
62
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
63
|
-
0 && (module.exports = {
|
|
64
|
-
findItemsByIds,
|
|
65
|
-
getAllItemKeys,
|
|
66
|
-
getResultIds
|
|
67
|
-
});
|
|
68
|
-
//# sourceMappingURL=utils.js.map
|
package/cjs/search/utils.js.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/search/utils.ts"],
|
|
4
|
-
"sourcesContent": ["import { SimpleDocumentSearchResultSetUnit } from 'flexsearch';\n\n/**\n * Extracts the unique item IDs from the result set\n *\n * @param result - The result set to extract the IDs from\n */\nexport function getResultIds(result: SimpleDocumentSearchResultSetUnit[]) {\n const set = new Set<number | string>();\n result.forEach((item) => {\n item.result.forEach((id) => set.add(id));\n });\n return Array.from(set);\n}\n\n/**\n * Get the items from the list that match the given IDs\n *\n * @param items - The list of items to search through\n * @param ids - The list of IDs to search for\n * @param idKey - The key to use to find the IDs in the items\n */\nexport function findItemsByIds<T extends Record<string, any>>(\n items: T[],\n ids: (string | number)[],\n idKey: Extract<keyof T, string>\n) {\n return items.filter(\n (item) => item && ids.includes(getValueFromNestedKey(item, idKey))\n );\n}\n\nfunction getValueFromNestedKey(object: Record<string, any>, nestedKey: string) {\n const keys = nestedKey.split(':');\n\n let result = object;\n for (const keySegment of keys) {\n if (Object.prototype.hasOwnProperty.call(result, keySegment)) {\n result = result[keySegment];\n }\n }\n return result as unknown as string;\n}\n\n/**\n * Recursively get all the keys from an object\n *\n * The keys are returned in the format `parentKey.childKey` for nested objects\n * and `key` for top level objects (objects that are not nested inside another object).\n *\n * @param object - The item to get the keys from\n * @param parentKey - The parent key to which the current key should be appended\n */\nexport function getAllItemKeys(\n object: Record<string, any>,\n parentKey = ''\n): string[] {\n let keys: string[] = [];\n for (const key in object) {\n if (Object.prototype.hasOwnProperty.call(object, key)) {\n const currentKey = parentKey ? `${parentKey}.${key}` : key;\n\n if (typeof object[key] === 'object' && object[key] !== null) {\n keys = keys.concat(getAllItemKeys(object[key], currentKey));\n } else {\n keys.push(currentKey);\n }\n }\n }\n\n return keys;\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAOO,SAAS,aAAa,QAA6C;AACxE,QAAM,MAAM,oBAAI,IAAqB;AACrC,SAAO,QAAQ,CAAC,SAAS;AACvB,SAAK,OAAO,QAAQ,CAAC,OAAO,IAAI,IAAI,EAAE,CAAC;AAAA,EACzC,CAAC;AACD,SAAO,MAAM,KAAK,GAAG;AACvB;AASO,SAAS,eACd,OACA,KACA,OACA;AACA,SAAO,MAAM;AAAA,IACX,CAAC,SAAS,QAAQ,IAAI,SAAS,sBAAsB,MAAM,KAAK,CAAC;AAAA,EACnE;AACF;AAEA,SAAS,sBAAsB,QAA6B,WAAmB;AAC7E,QAAM,OAAO,UAAU,MAAM,GAAG;AAEhC,MAAI,SAAS;AACb,aAAW,cAAc,MAAM;AAC7B,QAAI,OAAO,UAAU,eAAe,KAAK,QAAQ,UAAU,GAAG;AAC5D,eAAS,OAAO,UAAU;AAAA,IAC5B;AAAA,EACF;AACA,SAAO;AACT;AAWO,SAAS,eACd,QACA,YAAY,IACF;AACV,MAAI,OAAiB,CAAC;AACtB,aAAW,OAAO,QAAQ;AACxB,QAAI,OAAO,UAAU,eAAe,KAAK,QAAQ,GAAG,GAAG;AACrD,YAAM,aAAa,YAAY,GAAG,SAAS,IAAI,GAAG,KAAK;AAEvD,UAAI,OAAO,OAAO,GAAG,MAAM,YAAY,OAAO,GAAG,MAAM,MAAM;AAC3D,eAAO,KAAK,OAAO,eAAe,OAAO,GAAG,GAAG,UAAU,CAAC;AAAA,MAC5D,OAAO;AACL,aAAK,KAAK,UAAU;AAAA,MACtB;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
package/cjs/types/index.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __copyProps = (to, from, except, desc) => {
|
|
7
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
-
for (let key of __getOwnPropNames(from))
|
|
9
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
-
}
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
-
var types_exports = {};
|
|
16
|
-
module.exports = __toCommonJS(types_exports);
|
|
17
|
-
//# sourceMappingURL=types.js.map
|
package/cjs/types.js.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/types.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Options for configuring the useSearch hook\n */\nexport interface UseSearchOptions<T extends Record<string, any>> {\n indexing: SearchIndexOptions<T>;\n}\n\n/**\n * Options for configuring the search index\n *\n * @typeParam T - The type of the data that will be indexed\n */\nexport interface SearchIndexOptions<T> {\n /**\n * Describes where to find the unique ID for each item in the data\n *\n * This can also be a nested key, e.g. `address.street`. If not specified, the search index will\n * create an 'id' field on each item, with the value being the index of the item in the array.\n */\n idKey?: NestedKeyOf<T>;\n /**\n * List of fields to index\n *\n * Contains a list of fields from the document, that should be indexed. The fields can be nested.\n *\n * @example Using nested keys\n * ```\n * {\n * id: 1,\n * name: 'John',\n * address: {\n * street: '123 Main St',\n * city: 'New York'\n * }\n * }\n * ```\n * To search against the `street` key, add `address.street` to the fields array.\n */\n fields?: NestedKeyOf<T>[];\n /**\n * Whether to use a web worker for indexing\n */\n enableWorker?: boolean;\n}\n\n/**\n * Any key of an object that has nested objects.\n *\n * This utility type is used to get all the primitive keys of an object, including the keys of any nested objects.\n *\n * @typeParam ObjectType - The type of the object to get the keys of\n */\nexport type NestedKeyOf<T> = T extends Primitive\n ? never\n :\n | (keyof T & string)\n | { [P in keyof T & string]: DeepSubKeys<T, P> }[keyof T & string];\n\ntype DeepSubKeys<T, P extends keyof T & string> = T[P] extends (infer Inner)[]\n ? `${P}.${NestedKeyOf<Inner>}`\n : T[P] extends object\n ? `${P}.${NestedKeyOf<T[P]>}`\n : never;\n\ntype Primitive = null | undefined | string | number | boolean | symbol | bigint;\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
package/hook/index.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { useCallback, useMemo, useState } from "react";
|
|
2
|
-
import SearchIndex from "@digigov/text-search/search";
|
|
3
|
-
function useSearch(documents, query, options) {
|
|
4
|
-
const [loading, setLoading] = useState(false);
|
|
5
|
-
const [data, setData] = useState(documents);
|
|
6
|
-
const indexing = options?.indexing;
|
|
7
|
-
const index = useMemo(
|
|
8
|
-
() => new SearchIndex(documents, indexing),
|
|
9
|
-
[documents, indexing]
|
|
10
|
-
);
|
|
11
|
-
const search = useCallback(() => {
|
|
12
|
-
if (query) {
|
|
13
|
-
setLoading(true);
|
|
14
|
-
index.searchAsync(documents, query).then((data2) => setData(data2)).catch(() => setData([])).finally(() => setLoading(false));
|
|
15
|
-
} else {
|
|
16
|
-
setData(documents);
|
|
17
|
-
}
|
|
18
|
-
}, [query, index, documents]);
|
|
19
|
-
const reset = useCallback(() => {
|
|
20
|
-
setData(documents);
|
|
21
|
-
setLoading(false);
|
|
22
|
-
}, [documents]);
|
|
23
|
-
return { data, loading, search, reset };
|
|
24
|
-
}
|
|
25
|
-
export {
|
|
26
|
-
useSearch as default
|
|
27
|
-
};
|
|
28
|
-
//# sourceMappingURL=hook.js.map
|
package/hook/package.json
DELETED
package/hook.js.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/hook.ts"],
|
|
4
|
-
"sourcesContent": ["import { useCallback, useMemo, useState } from 'react';\nimport SearchIndex from '@digigov/text-search/search';\nimport { UseSearchOptions } from '@digigov/text-search/types';\n\n/**\n * Hook for searching through a list of documents\n *\n * Returns a list of documents that match the search query.\n * If no query is provided, it returns the original list of documents.\n *\n * @param documents - The list of documents to search through\n * @param options - Options for configuring the search index\n * @param query - The search query\n *\n * @typeParam T - The type of the data in the documents list\n */\nexport default function useSearch<T extends Record<string, any>>(\n documents: T[],\n query?: string,\n options?: UseSearchOptions<T>\n) {\n const [loading, setLoading] = useState(false);\n const [data, setData] = useState(documents);\n\n const indexing = options?.indexing;\n\n const index = useMemo(\n () => new SearchIndex(documents, indexing),\n [documents, indexing]\n );\n\n const search = useCallback(() => {\n if (query) {\n setLoading(true);\n index\n .searchAsync(documents, query)\n .then((data) => setData(data))\n .catch(() => setData([]))\n .finally(() => setLoading(false));\n } else {\n setData(documents);\n }\n }, [query, index, documents]);\n\n const reset = useCallback(() => {\n setData(documents);\n setLoading(false);\n }, [documents]);\n\n return { data, loading, search, reset };\n}\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,aAAa,SAAS,gBAAgB;AAC/C,OAAO,iBAAiB;AAeT,SAAR,UACL,WACA,OACA,SACA;AACA,QAAM,CAAC,SAAS,UAAU,IAAI,SAAS,KAAK;AAC5C,QAAM,CAAC,MAAM,OAAO,IAAI,SAAS,SAAS;AAE1C,QAAM,WAAW,SAAS;AAE1B,QAAM,QAAQ;AAAA,IACZ,MAAM,IAAI,YAAY,WAAW,QAAQ;AAAA,IACzC,CAAC,WAAW,QAAQ;AAAA,EACtB;AAEA,QAAM,SAAS,YAAY,MAAM;AAC/B,QAAI,OAAO;AACT,iBAAW,IAAI;AACf,YACG,YAAY,WAAW,KAAK,EAC5B,KAAK,CAACA,UAAS,QAAQA,KAAI,CAAC,EAC5B,MAAM,MAAM,QAAQ,CAAC,CAAC,CAAC,EACvB,QAAQ,MAAM,WAAW,KAAK,CAAC;AAAA,IACpC,OAAO;AACL,cAAQ,SAAS;AAAA,IACnB;AAAA,EACF,GAAG,CAAC,OAAO,OAAO,SAAS,CAAC;AAE5B,QAAM,QAAQ,YAAY,MAAM;AAC9B,YAAQ,SAAS;AACjB,eAAW,KAAK;AAAA,EAClB,GAAG,CAAC,SAAS,CAAC;AAEd,SAAO,EAAE,MAAM,SAAS,QAAQ,MAAM;AACxC;",
|
|
6
|
-
"names": ["data"]
|
|
7
|
-
}
|
package/hook.spec.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/index.js.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["import useSearch from '@digigov/text-search/hook';\n\nexport default useSearch;\nexport * from '@digigov/text-search/search';\n"],
|
|
5
|
-
"mappings": "AAAA,OAAO,eAAe;AAEtB,IAAO,cAAQ;AACf,cAAc;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/search/index.js.map
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { greekToGreeklishMap } from "@digigov/text-search/search/lang/gr/normalization-map";
|
|
2
|
-
const whitespaceRegex = new RegExp("[p{Z}p{S}p{P}p{C}]+", "u");
|
|
3
|
-
const diacriticsRegex = new RegExp("[\u0300-\u036F]", "g");
|
|
4
|
-
const greekCharsRegex = new RegExp(
|
|
5
|
-
Object.keys(greekToGreeklishMap).join("|"),
|
|
6
|
-
"igu"
|
|
7
|
-
);
|
|
8
|
-
function encodeGreek(str) {
|
|
9
|
-
return ("" + str).toLowerCase().normalize("NFD").replace(diacriticsRegex, "").replace(
|
|
10
|
-
greekCharsRegex,
|
|
11
|
-
(match) => match in greekToGreeklishMap ? greekToGreeklishMap[match] : match
|
|
12
|
-
).split(whitespaceRegex);
|
|
13
|
-
}
|
|
14
|
-
export {
|
|
15
|
-
encodeGreek
|
|
16
|
-
};
|
|
17
|
-
//# sourceMappingURL=encoder.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../src/search/lang/gr/encoder.ts"],
|
|
4
|
-
"sourcesContent": ["import { greekToGreeklishMap } from '@digigov/text-search/search/lang/gr/normalization-map';\n\nconst whitespaceRegex = new RegExp('[p{Z}p{S}p{P}p{C}]+', 'u');\nconst diacriticsRegex = new RegExp('[\\u0300-\\u036f]', 'g');\nconst greekCharsRegex = new RegExp(\n Object.keys(greekToGreeklishMap).join('|'),\n 'igu'\n);\n\n/**\n * Encode a string containing greek characters to greeklish\n *\n * This function is used to encode and tokenize a string containing greek characters.\n * It replaces greek characters with their latin counterparts, while also removing\n * any diacritics.\n *\n * @param str - The string to encode\n * @returns An array of encoded words\n */\nexport function encodeGreek(str: string) {\n return ('' + str)\n .toLowerCase()\n .normalize('NFD')\n .replace(diacriticsRegex, '')\n .replace(greekCharsRegex, (match) =>\n match in greekToGreeklishMap\n ? greekToGreeklishMap[match as keyof typeof greekToGreeklishMap]\n : match\n )\n .split(whitespaceRegex);\n}\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,2BAA2B;AAEpC,MAAM,kBAAkB,IAAI,OAAO,uBAAuB,GAAG;AAC7D,MAAM,kBAAkB,IAAI,OAAO,mBAAmB,GAAG;AACzD,MAAM,kBAAkB,IAAI;AAAA,EAC1B,OAAO,KAAK,mBAAmB,EAAE,KAAK,GAAG;AAAA,EACzC;AACF;AAYO,SAAS,YAAY,KAAa;AACvC,UAAQ,KAAK,KACV,YAAY,EACZ,UAAU,KAAK,EACf,QAAQ,iBAAiB,EAAE,EAC3B;AAAA,IAAQ;AAAA,IAAiB,CAAC,UACzB,SAAS,sBACL,oBAAoB,KAAyC,IAC7D;AAAA,EACN,EACC,MAAM,eAAe;AAC1B;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
const greekToGreeklishMap = {
|
|
2
|
-
\u0393\u03A7: "GX",
|
|
3
|
-
\u03B3\u03C7: "gx",
|
|
4
|
-
\u03A4\u0398: "T8",
|
|
5
|
-
\u03C4\u03B8: "t8",
|
|
6
|
-
\u03B8\u03B7: "8h",
|
|
7
|
-
\u0398\u03B7: "8h",
|
|
8
|
-
\u0398\u0397: "8H",
|
|
9
|
-
\u03B1\u03C5: "au",
|
|
10
|
-
\u0391\u03C5: "Au",
|
|
11
|
-
\u0391\u03A5: "AY",
|
|
12
|
-
\u03B5\u03C5: "eu",
|
|
13
|
-
\u03B5\u03CD: "eu",
|
|
14
|
-
\u03B5\u03CB: "ey",
|
|
15
|
-
\u03B5\u03B0: "ey",
|
|
16
|
-
\u0395\u03C5: "Eu",
|
|
17
|
-
\u0395\u03CD: "Eu",
|
|
18
|
-
\u0395\u03CB: "Ey",
|
|
19
|
-
\u0395\u03B0: "Ey",
|
|
20
|
-
\u0395\u03A5: "EY",
|
|
21
|
-
\u03BF\u03C5: "ou",
|
|
22
|
-
\u03BF\u03CD: "ou",
|
|
23
|
-
\u03BF\u03CB: "oy",
|
|
24
|
-
\u03BF\u03B0: "oy",
|
|
25
|
-
\u039F\u03C5: "Ou",
|
|
26
|
-
\u039F\u03CD: "Ou",
|
|
27
|
-
\u039F\u03CB: "Oy",
|
|
28
|
-
\u039F\u03B0: "Oy",
|
|
29
|
-
\u039F\u03A5: "OY",
|
|
30
|
-
\u0391: "A",
|
|
31
|
-
\u03B1: "a",
|
|
32
|
-
\u03AC: "a",
|
|
33
|
-
\u0386: "A",
|
|
34
|
-
\u0392: "B",
|
|
35
|
-
\u03B2: "b",
|
|
36
|
-
\u0393: "G",
|
|
37
|
-
\u03B3: "g",
|
|
38
|
-
\u0394: "D",
|
|
39
|
-
\u03B4: "d",
|
|
40
|
-
\u0395: "E",
|
|
41
|
-
\u03B5: "e",
|
|
42
|
-
\u03AD: "e",
|
|
43
|
-
\u0388: "E",
|
|
44
|
-
\u0396: "Z",
|
|
45
|
-
\u03B6: "z",
|
|
46
|
-
\u0397: "H",
|
|
47
|
-
\u03B7: "h",
|
|
48
|
-
\u03AE: "h",
|
|
49
|
-
\u0389: "H",
|
|
50
|
-
\u0398: "TH",
|
|
51
|
-
\u03B8: "th",
|
|
52
|
-
\u0399: "I",
|
|
53
|
-
\u03AA: "I",
|
|
54
|
-
\u03B9: "i",
|
|
55
|
-
\u03AF: "i",
|
|
56
|
-
\u0390: "i",
|
|
57
|
-
\u03CA: "i",
|
|
58
|
-
\u038A: "I",
|
|
59
|
-
\u039A: "K",
|
|
60
|
-
\u03BA: "k",
|
|
61
|
-
\u039B: "L",
|
|
62
|
-
\u03BB: "l",
|
|
63
|
-
\u039C: "M",
|
|
64
|
-
\u03BC: "m",
|
|
65
|
-
\u039D: "N",
|
|
66
|
-
\u03BD: "n",
|
|
67
|
-
\u039E: "KS",
|
|
68
|
-
\u03BE: "ks",
|
|
69
|
-
\u039F: "O",
|
|
70
|
-
\u03BF: "o",
|
|
71
|
-
\u038C: "O",
|
|
72
|
-
\u03CC: "o",
|
|
73
|
-
\u03A0: "P",
|
|
74
|
-
\u03C0: "p",
|
|
75
|
-
\u03A1: "R",
|
|
76
|
-
\u03C1: "r",
|
|
77
|
-
\u03A3: "S",
|
|
78
|
-
\u03C3: "s",
|
|
79
|
-
\u03A4: "T",
|
|
80
|
-
\u03C4: "t",
|
|
81
|
-
\u03A5: "Y",
|
|
82
|
-
\u038E: "Y",
|
|
83
|
-
\u03AB: "Y",
|
|
84
|
-
\u03B0: "y",
|
|
85
|
-
\u03CD: "y",
|
|
86
|
-
\u03CB: "y",
|
|
87
|
-
\u03C5: "y",
|
|
88
|
-
\u03A6: "F",
|
|
89
|
-
\u03C6: "f",
|
|
90
|
-
\u03A7: "X",
|
|
91
|
-
\u03C7: "x",
|
|
92
|
-
\u03A8: "Ps",
|
|
93
|
-
\u03C8: "ps",
|
|
94
|
-
\u03A9: "w",
|
|
95
|
-
\u03C9: "w",
|
|
96
|
-
\u038F: "w",
|
|
97
|
-
\u03CE: "w",
|
|
98
|
-
\u03C2: "s",
|
|
99
|
-
";": "?"
|
|
100
|
-
};
|
|
101
|
-
export {
|
|
102
|
-
greekToGreeklishMap
|
|
103
|
-
};
|
|
104
|
-
//# sourceMappingURL=normalization-map.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../src/search/lang/gr/normalization-map.ts"],
|
|
4
|
-
"sourcesContent": ["export const greekToGreeklishMap = {\n \u0393\u03A7: 'GX',\n \u03B3\u03C7: 'gx',\n \u03A4\u0398: 'T8',\n \u03C4\u03B8: 't8',\n \u03B8\u03B7: '8h',\n \u0398\u03B7: '8h',\n \u0398\u0397: '8H',\n \u03B1\u03C5: 'au',\n \u0391\u03C5: 'Au',\n \u0391\u03A5: 'AY',\n \u03B5\u03C5: 'eu',\n \u03B5\u03CD: 'eu',\n \u03B5\u03CB: 'ey',\n \u03B5\u03B0: 'ey',\n \u0395\u03C5: 'Eu',\n \u0395\u03CD: 'Eu',\n \u0395\u03CB: 'Ey',\n \u0395\u03B0: 'Ey',\n \u0395\u03A5: 'EY',\n \u03BF\u03C5: 'ou',\n \u03BF\u03CD: 'ou',\n \u03BF\u03CB: 'oy',\n \u03BF\u03B0: 'oy',\n \u039F\u03C5: 'Ou',\n \u039F\u03CD: 'Ou',\n \u039F\u03CB: 'Oy',\n \u039F\u03B0: 'Oy',\n \u039F\u03A5: 'OY',\n \u0391: 'A',\n \u03B1: 'a',\n \u03AC: 'a',\n \u0386: 'A',\n \u0392: 'B',\n \u03B2: 'b',\n \u0393: 'G',\n \u03B3: 'g',\n \u0394: 'D',\n \u03B4: 'd',\n \u0395: 'E',\n \u03B5: 'e',\n \u03AD: 'e',\n \u0388: 'E',\n \u0396: 'Z',\n \u03B6: 'z',\n \u0397: 'H',\n \u03B7: 'h',\n \u03AE: 'h',\n \u0389: 'H',\n \u0398: 'TH',\n \u03B8: 'th',\n \u0399: 'I',\n \u03AA: 'I',\n \u03B9: 'i',\n \u03AF: 'i',\n \u0390: 'i',\n \u03CA: 'i',\n \u038A: 'I',\n \u039A: 'K',\n \u03BA: 'k',\n \u039B: 'L',\n \u03BB: 'l',\n \u039C: 'M',\n \u03BC: 'm',\n \u039D: 'N',\n \u03BD: 'n',\n \u039E: 'KS',\n \u03BE: 'ks',\n \u039F: 'O',\n \u03BF: 'o',\n \u038C: 'O',\n \u03CC: 'o',\n \u03A0: 'P',\n \u03C0: 'p',\n \u03A1: 'R',\n \u03C1: 'r',\n \u03A3: 'S',\n \u03C3: 's',\n \u03A4: 'T',\n \u03C4: 't',\n \u03A5: 'Y',\n \u038E: 'Y',\n \u03AB: 'Y',\n \u03B0: 'y',\n \u03CD: 'y',\n \u03CB: 'y',\n \u03C5: 'y',\n \u03A6: 'F',\n \u03C6: 'f',\n \u03A7: 'X',\n \u03C7: 'x',\n \u03A8: 'Ps',\n \u03C8: 'ps',\n \u03A9: 'w',\n \u03C9: 'w',\n \u038F: 'w',\n \u03CE: 'w',\n \u03C2: 's',\n ';': '?',\n};\n"],
|
|
5
|
-
"mappings": "AAAO,MAAM,sBAAsB;AAAA,EACjC,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,cAAI;AAAA,EACJ,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,QAAG;AAAA,EACH,KAAK;AACP;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
package/search/package.json
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { Document } from "flexsearch";
|
|
2
|
-
import { encodeGreek } from "@digigov/text-search/search/lang/gr/encoder";
|
|
3
|
-
import {
|
|
4
|
-
findItemsByIds,
|
|
5
|
-
getAllItemKeys,
|
|
6
|
-
getResultIds
|
|
7
|
-
} from "@digigov/text-search/search/utils";
|
|
8
|
-
class SearchIndex {
|
|
9
|
-
constructor(items, options) {
|
|
10
|
-
this.idKey = "id";
|
|
11
|
-
if (options && options.idKey) {
|
|
12
|
-
this.idKey = options.idKey.replace(".", ":");
|
|
13
|
-
} else if (!(items[0] && "id" in items[0])) {
|
|
14
|
-
items = items.map((item, index) => ({ ...item, id: index }));
|
|
15
|
-
}
|
|
16
|
-
let fields = [];
|
|
17
|
-
if (options && options.fields) {
|
|
18
|
-
fields = options.fields;
|
|
19
|
-
} else if (items[0]) {
|
|
20
|
-
fields = getAllItemKeys(items[0]);
|
|
21
|
-
}
|
|
22
|
-
fields = fields.filter((field) => field !== this.idKey).map((field) => field.replace(".", ":"));
|
|
23
|
-
this.index = new Document({
|
|
24
|
-
document: {
|
|
25
|
-
id: this.idKey,
|
|
26
|
-
index: fields
|
|
27
|
-
},
|
|
28
|
-
tokenize: "forward",
|
|
29
|
-
worker: options?.enableWorker,
|
|
30
|
-
encode: encodeGreek
|
|
31
|
-
});
|
|
32
|
-
this.addAll(items);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Add an item to the index
|
|
36
|
-
*
|
|
37
|
-
* @param item - The item to add to the index
|
|
38
|
-
*/
|
|
39
|
-
add(item) {
|
|
40
|
-
this.index.add(item);
|
|
41
|
-
return this;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Add multiple items to the index
|
|
45
|
-
*
|
|
46
|
-
* @param items - The list of items to add to the index
|
|
47
|
-
*/
|
|
48
|
-
addAll(items) {
|
|
49
|
-
items.forEach((item) => this.index.add(item));
|
|
50
|
-
return this;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Search the index for the given term
|
|
54
|
-
*
|
|
55
|
-
* @param documents - The list of documents to search
|
|
56
|
-
* @param searchTerm - The term to search for
|
|
57
|
-
* @returns The list of items that match the search term
|
|
58
|
-
*/
|
|
59
|
-
search(documents, searchTerm) {
|
|
60
|
-
const res = this.index.search(searchTerm);
|
|
61
|
-
const ids = getResultIds(res);
|
|
62
|
-
return findItemsByIds(
|
|
63
|
-
documents,
|
|
64
|
-
ids,
|
|
65
|
-
this.idKey
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Asynchronously search the index for the given term
|
|
70
|
-
*
|
|
71
|
-
* @param documents - The list of documents to search
|
|
72
|
-
* @param searchTerm - The term to search for
|
|
73
|
-
* @returns The list of items that match the search term
|
|
74
|
-
*/
|
|
75
|
-
async searchAsync(documents, searchTerm) {
|
|
76
|
-
return this.index.searchAsync(searchTerm).then((res) => {
|
|
77
|
-
return getResultIds(res);
|
|
78
|
-
}).then(
|
|
79
|
-
(res) => findItemsByIds(documents, res, this.idKey)
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
export {
|
|
84
|
-
SearchIndex
|
|
85
|
-
};
|
|
86
|
-
//# sourceMappingURL=search-index.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/search/search-index.ts"],
|
|
4
|
-
"sourcesContent": ["import { Document } from 'flexsearch';\nimport { encodeGreek } from '@digigov/text-search/search/lang/gr/encoder';\nimport {\n findItemsByIds,\n getAllItemKeys,\n getResultIds,\n} from '@digigov/text-search/search/utils';\nimport type { SearchIndexOptions } from '@digigov/text-search/types';\n\n/**\n * Wrapper class around third party index and search library\n *\n * @typeParam T - The type of the data that will be indexed\n */\nexport class SearchIndex<T extends Record<string, any>> {\n /**\n * Instance of the third party library's index\n */\n private index: Document<T>;\n private idKey: string;\n\n constructor(items: T[], options?: SearchIndexOptions<T>) {\n this.idKey = 'id';\n if (options && options.idKey) {\n this.idKey = options.idKey.replace('.', ':');\n } else if (!(items[0] && 'id' in items[0])) {\n items = items.map((item, index) => ({ ...item, id: index }));\n }\n\n let fields: string[] = [];\n if (options && options.fields) {\n fields = options.fields;\n } else if (items[0]) {\n fields = getAllItemKeys(items[0]);\n }\n fields = fields\n .filter((field) => field !== this.idKey)\n .map((field) => field.replace('.', ':'));\n\n this.index = new Document<T>({\n document: {\n id: this.idKey,\n index: fields,\n },\n tokenize: 'forward',\n worker: options?.enableWorker,\n encode: encodeGreek,\n });\n\n this.addAll(items);\n }\n\n /**\n * Add an item to the index\n *\n * @param item - The item to add to the index\n */\n public add(item: T) {\n this.index.add(item);\n return this;\n }\n\n /**\n * Add multiple items to the index\n *\n * @param items - The list of items to add to the index\n */\n public addAll(items: T[]) {\n items.forEach((item) => this.index.add(item));\n return this;\n }\n\n /**\n * Search the index for the given term\n *\n * @param documents - The list of documents to search\n * @param searchTerm - The term to search for\n * @returns The list of items that match the search term\n */\n public search(documents: T[], searchTerm: string) {\n const res = this.index.search(searchTerm);\n const ids = getResultIds(res);\n return findItemsByIds(\n documents,\n ids,\n this.idKey as Extract<keyof T, string>\n );\n }\n\n /**\n * Asynchronously search the index for the given term\n *\n * @param documents - The list of documents to search\n * @param searchTerm - The term to search for\n * @returns The list of items that match the search term\n */\n public async searchAsync(documents: T[], searchTerm: string) {\n return this.index\n .searchAsync(searchTerm)\n .then((res) => {\n return getResultIds(res);\n })\n .then((res) =>\n findItemsByIds(documents, res, this.idKey as Extract<keyof T, string>)\n );\n }\n}\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,gBAAgB;AACzB,SAAS,mBAAmB;AAC5B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAQA,MAAM,YAA2C;AAAA,EAOtD,YAAY,OAAY,SAAiC;AACvD,SAAK,QAAQ;AACb,QAAI,WAAW,QAAQ,OAAO;AAC5B,WAAK,QAAQ,QAAQ,MAAM,QAAQ,KAAK,GAAG;AAAA,IAC7C,WAAW,EAAE,MAAM,CAAC,KAAK,QAAQ,MAAM,CAAC,IAAI;AAC1C,cAAQ,MAAM,IAAI,CAAC,MAAM,WAAW,EAAE,GAAG,MAAM,IAAI,MAAM,EAAE;AAAA,IAC7D;AAEA,QAAI,SAAmB,CAAC;AACxB,QAAI,WAAW,QAAQ,QAAQ;AAC7B,eAAS,QAAQ;AAAA,IACnB,WAAW,MAAM,CAAC,GAAG;AACnB,eAAS,eAAe,MAAM,CAAC,CAAC;AAAA,IAClC;AACA,aAAS,OACN,OAAO,CAAC,UAAU,UAAU,KAAK,KAAK,EACtC,IAAI,CAAC,UAAU,MAAM,QAAQ,KAAK,GAAG,CAAC;AAEzC,SAAK,QAAQ,IAAI,SAAY;AAAA,MAC3B,UAAU;AAAA,QACR,IAAI,KAAK;AAAA,QACT,OAAO;AAAA,MACT;AAAA,MACA,UAAU;AAAA,MACV,QAAQ,SAAS;AAAA,MACjB,QAAQ;AAAA,IACV,CAAC;AAED,SAAK,OAAO,KAAK;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,IAAI,MAAS;AAClB,SAAK,MAAM,IAAI,IAAI;AACnB,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,OAAO,OAAY;AACxB,UAAM,QAAQ,CAAC,SAAS,KAAK,MAAM,IAAI,IAAI,CAAC;AAC5C,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASO,OAAO,WAAgB,YAAoB;AAChD,UAAM,MAAM,KAAK,MAAM,OAAO,UAAU;AACxC,UAAM,MAAM,aAAa,GAAG;AAC5B,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA,KAAK;AAAA,IACP;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAa,YAAY,WAAgB,YAAoB;AAC3D,WAAO,KAAK,MACT,YAAY,UAAU,EACtB,KAAK,CAAC,QAAQ;AACb,aAAO,aAAa,GAAG;AAAA,IACzB,CAAC,EACA;AAAA,MAAK,CAAC,QACL,eAAe,WAAW,KAAK,KAAK,KAAiC;AAAA,IACvE;AAAA,EACJ;AACF;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
package/search/utils/index.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
function getResultIds(result) {
|
|
2
|
-
const set = /* @__PURE__ */ new Set();
|
|
3
|
-
result.forEach((item) => {
|
|
4
|
-
item.result.forEach((id) => set.add(id));
|
|
5
|
-
});
|
|
6
|
-
return Array.from(set);
|
|
7
|
-
}
|
|
8
|
-
function findItemsByIds(items, ids, idKey) {
|
|
9
|
-
return items.filter(
|
|
10
|
-
(item) => item && ids.includes(getValueFromNestedKey(item, idKey))
|
|
11
|
-
);
|
|
12
|
-
}
|
|
13
|
-
function getValueFromNestedKey(object, nestedKey) {
|
|
14
|
-
const keys = nestedKey.split(":");
|
|
15
|
-
let result = object;
|
|
16
|
-
for (const keySegment of keys) {
|
|
17
|
-
if (Object.prototype.hasOwnProperty.call(result, keySegment)) {
|
|
18
|
-
result = result[keySegment];
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return result;
|
|
22
|
-
}
|
|
23
|
-
function getAllItemKeys(object, parentKey = "") {
|
|
24
|
-
let keys = [];
|
|
25
|
-
for (const key in object) {
|
|
26
|
-
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
27
|
-
const currentKey = parentKey ? `${parentKey}.${key}` : key;
|
|
28
|
-
if (typeof object[key] === "object" && object[key] !== null) {
|
|
29
|
-
keys = keys.concat(getAllItemKeys(object[key], currentKey));
|
|
30
|
-
} else {
|
|
31
|
-
keys.push(currentKey);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return keys;
|
|
36
|
-
}
|
|
37
|
-
export {
|
|
38
|
-
findItemsByIds,
|
|
39
|
-
getAllItemKeys,
|
|
40
|
-
getResultIds
|
|
41
|
-
};
|
|
42
|
-
//# sourceMappingURL=utils.js.map
|