cry-search 1.0.0

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 (64) hide show
  1. package/CLAUDE.md +285 -0
  2. package/LICENSE.md +67 -0
  3. package/README.md +283 -0
  4. package/UNIVERSE.md +818 -0
  5. package/dist/common/SearchUniverse.d.ts +210 -0
  6. package/dist/common/SearchUniverse.d.ts.map +1 -0
  7. package/dist/common/findInArray.d.ts +37 -0
  8. package/dist/common/findInArray.d.ts.map +1 -0
  9. package/dist/common/findInArrayReturnDataAndMeta.d.ts +52 -0
  10. package/dist/common/findInArrayReturnDataAndMeta.d.ts.map +1 -0
  11. package/dist/common/findInLinkedArrays.d.ts +66 -0
  12. package/dist/common/findInLinkedArrays.d.ts.map +1 -0
  13. package/dist/common/numeric/createSearchArrayMetadata.d.ts +65 -0
  14. package/dist/common/numeric/createSearchArrayMetadata.d.ts.map +1 -0
  15. package/dist/common/numeric/createSearchLinkedMetadata.d.ts +46 -0
  16. package/dist/common/numeric/createSearchLinkedMetadata.d.ts.map +1 -0
  17. package/dist/common/numeric/findInLinkedArrays.d.ts +32 -0
  18. package/dist/common/numeric/findInLinkedArrays.d.ts.map +1 -0
  19. package/dist/common/numeric/index.d.ts +9 -0
  20. package/dist/common/numeric/index.d.ts.map +1 -0
  21. package/dist/common/numeric/searchInData.d.ts +31 -0
  22. package/dist/common/numeric/searchInData.d.ts.map +1 -0
  23. package/dist/common/numeric/updateSearchMetadata.d.ts +60 -0
  24. package/dist/common/numeric/updateSearchMetadata.d.ts.map +1 -0
  25. package/dist/common/string/createSearchArrayMetadata.d.ts +117 -0
  26. package/dist/common/string/createSearchArrayMetadata.d.ts.map +1 -0
  27. package/dist/common/string/createSearchLinkedMetadata.d.ts +36 -0
  28. package/dist/common/string/createSearchLinkedMetadata.d.ts.map +1 -0
  29. package/dist/common/string/index.d.ts +10 -0
  30. package/dist/common/string/index.d.ts.map +1 -0
  31. package/dist/common/string/updateSearchMetadata.d.ts +103 -0
  32. package/dist/common/string/updateSearchMetadata.d.ts.map +1 -0
  33. package/dist/common/syncSearchArrayMetadata.d.ts +64 -0
  34. package/dist/common/syncSearchArrayMetadata.d.ts.map +1 -0
  35. package/dist/common/updateSearchLinkedMetadata.d.ts +129 -0
  36. package/dist/common/updateSearchLinkedMetadata.d.ts.map +1 -0
  37. package/dist/index.cjs +1827 -0
  38. package/dist/index.d.cts +32 -0
  39. package/dist/index.d.ts +32 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +1795 -0
  42. package/dist/types/index.d.ts +300 -0
  43. package/dist/types/index.d.ts.map +1 -0
  44. package/dist/types.d.ts +6 -0
  45. package/dist/types.d.ts.map +1 -0
  46. package/dist/utils/matchToken.d.ts +65 -0
  47. package/dist/utils/matchToken.d.ts.map +1 -0
  48. package/dist/utils/matchTokenNumeric.d.ts +94 -0
  49. package/dist/utils/matchTokenNumeric.d.ts.map +1 -0
  50. package/dist/utils/normalizeDates.d.ts +39 -0
  51. package/dist/utils/normalizeDates.d.ts.map +1 -0
  52. package/dist/utils/prepareStringForSearch.d.ts +28 -0
  53. package/dist/utils/prepareStringForSearch.d.ts.map +1 -0
  54. package/dist/utils/prepareStringForSearchNumeric.d.ts +42 -0
  55. package/dist/utils/prepareStringForSearchNumeric.d.ts.map +1 -0
  56. package/dist/utils/preprocessString.d.ts +26 -0
  57. package/dist/utils/preprocessString.d.ts.map +1 -0
  58. package/dist/utils/sanitiseString.d.ts +26 -0
  59. package/dist/utils/sanitiseString.d.ts.map +1 -0
  60. package/dist/utils/tokenRegistry.d.ts +70 -0
  61. package/dist/utils/tokenRegistry.d.ts.map +1 -0
  62. package/dist/utils/tokenize.d.ts +46 -0
  63. package/dist/utils/tokenize.d.ts.map +1 -0
  64. package/package.json +67 -0
@@ -0,0 +1,60 @@
1
+ import type { Id, NumericSearchMetadata, SearchableObject, SearchableObjectSpec, UpdateSearchMetadataBatchItem } from '../../types';
2
+ /**
3
+ * Updates numeric search metadata for a single object.
4
+ *
5
+ * This function handles updates, additions, and removals:
6
+ * - If the object is `null` or `undefined`, removes it from metadata
7
+ * - If the object is `_deleted` or `_blocked`, removes it from metadata
8
+ * - Otherwise, computes new tokens and updates/adds to metadata
9
+ *
10
+ * @param metadata - The NumericSearchMetadata map to update (mutated in place)
11
+ * @param objectId - The ID of the object to update
12
+ * @param obj - The object (may be null, undefined, deleted, or blocked)
13
+ * @param spec - Optional specification for how to extract searchable text
14
+ * @param tokensMap - Map for string->number conversion
15
+ * @returns true if metadata was added/updated, false if removed
16
+ */
17
+ export declare function updateSearchMetadataNumeric<T extends SearchableObject, C = unknown>(metadata: NumericSearchMetadata, objectId: Id, obj: T | null | undefined, spec: SearchableObjectSpec<T, C> | undefined, tokensMap: Map<string, number>): boolean;
18
+ /**
19
+ * Updates numeric search metadata for multiple objects in a batch.
20
+ *
21
+ * Creates a temporary tokensMap for the duration of the batch operation,
22
+ * then clears it after use to free memory.
23
+ *
24
+ * @param metadata - The NumericSearchMetadata map to update (mutated in place)
25
+ * @param batch - Array of objects to update
26
+ * @param spec - Optional specification for how to extract searchable text
27
+ * @returns Object with counts of added/updated and removed items
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const batch = [
32
+ * { objectId: '1', obj: { _id: '1', name: 'Product 1' } },
33
+ * { objectId: '2', obj: { _id: '2', name: 'Product 2', _deleted: new Date() } },
34
+ * ];
35
+ *
36
+ * const result = updateSearchMetadataBatchNumeric(metadata, batch, spec);
37
+ * // result: { updated: 1, removed: 1 }
38
+ * ```
39
+ */
40
+ export declare function updateSearchMetadataBatchNumeric<T extends SearchableObject, C = unknown>(metadata: NumericSearchMetadata, batch: UpdateSearchMetadataBatchItem<T>[], spec?: SearchableObjectSpec<T, C>): {
41
+ updated: number;
42
+ removed: number;
43
+ };
44
+ /**
45
+ * Removes an object from numeric search metadata by ID.
46
+ *
47
+ * @param metadata - The NumericSearchMetadata map to update (mutated in place)
48
+ * @param objectId - The ID of the object to remove
49
+ * @returns true if the object was found and removed, false if it wasn't in metadata
50
+ */
51
+ export declare function removeFromSearchMetadataNumeric(metadata: NumericSearchMetadata, objectId: Id): boolean;
52
+ /**
53
+ * Removes multiple objects from numeric search metadata by IDs.
54
+ *
55
+ * @param metadata - The NumericSearchMetadata map to update (mutated in place)
56
+ * @param objectIds - Array of IDs to remove
57
+ * @returns Number of objects that were actually removed
58
+ */
59
+ export declare function removeFromSearchMetadataBatchNumeric(metadata: NumericSearchMetadata, objectIds: Id[]): number;
60
+ //# sourceMappingURL=updateSearchMetadata.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"updateSearchMetadata.d.ts","sourceRoot":"","sources":["../../../src/common/numeric/updateSearchMetadata.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,EAAE,EACF,qBAAqB,EACrB,gBAAgB,EAChB,oBAAoB,EACpB,6BAA6B,EAC9B,MAAM,aAAa,CAAC;AAIrB;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,2BAA2B,CAAC,CAAC,SAAS,gBAAgB,EAAE,CAAC,GAAG,OAAO,EACjF,QAAQ,EAAE,qBAAqB,EAC/B,QAAQ,EAAE,EAAE,EACZ,GAAG,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EACzB,IAAI,EAAE,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,EAC5C,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAC7B,OAAO,CAUT;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,gCAAgC,CAAC,CAAC,SAAS,gBAAgB,EAAE,CAAC,GAAG,OAAO,EACtF,QAAQ,EAAE,qBAAqB,EAC/B,KAAK,EAAE,6BAA6B,CAAC,CAAC,CAAC,EAAE,EACzC,IAAI,CAAC,EAAE,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,GAChC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CA0BtC;AAED;;;;;;GAMG;AACH,wBAAgB,+BAA+B,CAC7C,QAAQ,EAAE,qBAAqB,EAC/B,QAAQ,EAAE,EAAE,GACX,OAAO,CAET;AAED;;;;;;GAMG;AACH,wBAAgB,oCAAoC,CAClD,QAAQ,EAAE,qBAAqB,EAC/B,SAAS,EAAE,EAAE,EAAE,GACd,MAAM,CAYR"}
@@ -0,0 +1,117 @@
1
+ import type { MatchMode, SearchMetadata, SearchableObject, SearchableData, SearchableObjectSpec, SearchTokenSortedList, SearchArrayMetadata } from '../../types';
2
+ /**
3
+ * Prefix characters for per-field match modes.
4
+ * Tokens with these prefixes use the specified mode instead of global SearchOpts.
5
+ */
6
+ export declare const MATCH_MODE_PREFIXES: Record<MatchMode, string>;
7
+ /**
8
+ * Reverse mapping from prefix to match mode.
9
+ */
10
+ export declare const PREFIX_TO_MATCH_MODE: Record<string, MatchMode>;
11
+ /**
12
+ * Checks if an object is deleted or blocked and should be excluded from search.
13
+ *
14
+ * Objects with `_deleted` or `_blocked` fields set are excluded from search results.
15
+ *
16
+ * @param obj - The searchable object to check
17
+ * @returns True if the object has _deleted or _blocked set
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * isDeletedOrBlocked({ _id: '1', name: 'Test' }) // false
22
+ * isDeletedOrBlocked({ _id: '1', name: 'Test', _deleted: new Date() }) // true
23
+ * ```
24
+ */
25
+ export declare function isDeletedOrBlocked(obj: SearchableObject): boolean;
26
+ /**
27
+ * Default function to extract searchable string from an object.
28
+ *
29
+ * Concatenates all string/number values from specified fields (or all fields if not specified).
30
+ * System fields (_id, _deleted, _blocked) are excluded by default.
31
+ * Supports dot notation paths for nested fields (e.g., 'customer.firstName').
32
+ * Recursively extracts values from nested objects and arrays.
33
+ *
34
+ * @param obj - The searchable object to extract text from
35
+ * @param spec - Optional specification for field extraction
36
+ * @returns undefined if object is _deleted or _blocked, otherwise the searchable string
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * const obj = { _id: '1', name: 'John', age: 30 };
41
+ * extractSearchableStringDefault(obj)
42
+ * // Returns: 'John 30'
43
+ *
44
+ * const obj2 = { _id: '1', customer: { firstName: 'John' } };
45
+ * extractSearchableStringDefault(obj2, { searchInFields: ['customer.firstName'] })
46
+ * // Returns: 'John'
47
+ * ```
48
+ */
49
+ export declare function extractSearchableStringDefault<T extends SearchableObject, C = unknown>(obj: T, spec?: SearchableObjectSpec<T, C>): string | undefined;
50
+ /**
51
+ * Prepares an object for search by extracting searchable text and tokenizing it.
52
+ *
53
+ * If `fieldMatchModes` is specified in the spec, tokens from those fields will have
54
+ * match mode prefixes (e.g., `!` for whole, `+` for startEnd).
55
+ *
56
+ * Supports dot notation in `searchInFields` for targeting specific nested fields.
57
+ * Recursively extracts values from nested objects and arrays.
58
+ *
59
+ * @param obj - The searchable object to prepare
60
+ * @param spec - Optional specification for extraction and match modes
61
+ * @returns undefined if object is _deleted or _blocked, otherwise sorted token list
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * const obj = { _id: '1', name: 'John Doe', barcode: '123456' };
66
+ * prepareObjectForSearch(obj, {
67
+ * searchInFields: ['name', 'barcode'],
68
+ * fieldMatchModes: { barcode: 'whole' }
69
+ * })
70
+ * // Returns: ['!123456', 'doe', 'john']
71
+ * ```
72
+ */
73
+ export declare function prepareObjectForSearch<T extends SearchableObject, C = unknown>(obj: T, spec?: SearchableObjectSpec<T, C>): SearchTokenSortedList | undefined;
74
+ /**
75
+ * Creates search metadata for a collection of items.
76
+ *
77
+ * Pre-processes all searchable text into sorted tokens for fast searching.
78
+ * Items with `_deleted` or `_blocked` fields are automatically excluded.
79
+ *
80
+ * @param data - Map of items by ID (SearchableData)
81
+ * @param spec - Optional specification for how to extract searchable text
82
+ * @returns SearchMetadata map (Id → sorted tokens)
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * const items = arrayToSearchableData([
87
+ * { _id: '1', name: 'John Doe', barcode: '123456' },
88
+ * { _id: '2', name: 'Jane Smith', barcode: '789012' },
89
+ * ]);
90
+ *
91
+ * const metadata = createSearchArrayMetadata(items, {
92
+ * searchInFields: ['name', 'barcode'],
93
+ * fieldMatchModes: { barcode: 'startEnd' },
94
+ * });
95
+ *
96
+ * // metadata.get('1') → ['+123456', 'doe', 'john']
97
+ * ```
98
+ */
99
+ export declare function createSearchArrayMetadata<T extends SearchableObject, C = unknown>(data: SearchableData<T>, spec?: SearchableObjectSpec<T, C>): SearchMetadata;
100
+ export type { SearchMetadata, SearchableObject, SearchableData, SearchableObjectSpec, SearchArrayMetadata, SearchableItem, SearchItemMetadata, } from '../../types';
101
+ /**
102
+ * Helper to wrap new SearchMetadata in legacy SearchArrayMetadata format.
103
+ *
104
+ * @param metadata - The SearchMetadata to wrap
105
+ * @returns Legacy SearchArrayMetadata object
106
+ * @deprecated Use SearchMetadata directly instead
107
+ */
108
+ export declare function wrapMetadataLegacy(metadata: SearchMetadata): SearchArrayMetadata;
109
+ /**
110
+ * Helper to unwrap legacy SearchArrayMetadata to new SearchMetadata format.
111
+ *
112
+ * @param legacy - The legacy SearchArrayMetadata to unwrap
113
+ * @returns The SearchMetadata map
114
+ * @deprecated Use SearchMetadata directly instead
115
+ */
116
+ export declare function unwrapMetadataLegacy(legacy: SearchArrayMetadata): SearchMetadata;
117
+ //# sourceMappingURL=createSearchArrayMetadata.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createSearchArrayMetadata.d.ts","sourceRoot":"","sources":["../../../src/common/string/createSearchArrayMetadata.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,oBAAoB,EACpB,qBAAqB,EACrB,mBAAmB,EACpB,MAAM,aAAa,CAAC;AAGrB;;;GAGG;AACH,eAAO,MAAM,mBAAmB,EAAE,MAAM,CAAC,SAAS,EAAE,MAAM,CAMzD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAS1D,CAAC;AAmHF;;;;;;;;;;;;;GAaG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAEjE;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,8BAA8B,CAAC,CAAC,SAAS,gBAAgB,EAAE,CAAC,GAAG,OAAO,EACpF,GAAG,EAAE,CAAC,EACN,IAAI,CAAC,EAAE,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,GAChC,MAAM,GAAG,SAAS,CA8BpB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,gBAAgB,EAAE,CAAC,GAAG,OAAO,EAC5E,GAAG,EAAE,CAAC,EACN,IAAI,CAAC,EAAE,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,GAChC,qBAAqB,GAAG,SAAS,CAkBnC;AAgFD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,SAAS,gBAAgB,EAAE,CAAC,GAAG,OAAO,EAC/E,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,EACvB,IAAI,CAAC,EAAE,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,GAChC,cAAc,CAYhB;AAGD,YAAY,EACV,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,oBAAoB,EACpB,mBAAmB,EACnB,cAAc,EACd,kBAAkB,GACnB,MAAM,aAAa,CAAC;AAErB;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,cAAc,GAAG,mBAAmB,CAEhF;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,mBAAmB,GAAG,cAAc,CAEhF"}
@@ -0,0 +1,36 @@
1
+ import type { Id, SearchMetadata, SearchableObject, SearchableData, SearchLinkedMetadataWithData } from '../../types';
2
+ export type { SearchLinkedMetadataWithData } from '../../types';
3
+ /**
4
+ * Creates linked metadata for searching across two related data sets.
5
+ *
6
+ * This function builds a reverse index from primary items to their linked items,
7
+ * enabling efficient combined searches (e.g., searching for "Krajnik Rex" finds
8
+ * the customer "Krajnik" with pet "Rex").
9
+ *
10
+ * The linked items are associated with primary items via a foreign key (e.g., `stranka_id`).
11
+ * No data is duplicated - the function stores references and indexes only.
12
+ *
13
+ * @typeParam TPrimary - Type of primary items (e.g., Stranka)
14
+ * @typeParam TLinked - Type of linked items (e.g., Pacient)
15
+ *
16
+ * @param primaryData - Primary data map (e.g., stranke)
17
+ * @param primaryMetadata - Pre-computed metadata for primary items
18
+ * @param linkedData - Linked/secondary data map (e.g., pacienti)
19
+ * @param linkedMetadata - Pre-computed metadata for linked items
20
+ * @param foreignKeyGetter - Function to extract the primary ID from a linked item
21
+ * @returns Linked metadata for combined searching
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * const linkedMeta = createSearchLinkedMetadata(
26
+ * strankeData, strankeMeta,
27
+ * pacientiData, pacientiMeta,
28
+ * (pacient) => pacient.stranka_id
29
+ * );
30
+ *
31
+ * // Now use findInLinkedArrays to search across both collections
32
+ * const results = findInLinkedArrays('krajnik rex', linkedMeta, 10);
33
+ * ```
34
+ */
35
+ export declare function createSearchLinkedMetadata<TPrimary extends SearchableObject, TLinked extends SearchableObject>(primaryData: SearchableData<TPrimary>, primaryMetadata: SearchMetadata, linkedData: SearchableData<TLinked>, linkedMetadata: SearchMetadata, foreignKeyGetter: (linked: TLinked) => Id | undefined): SearchLinkedMetadataWithData<TPrimary, TLinked>;
36
+ //# sourceMappingURL=createSearchLinkedMetadata.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createSearchLinkedMetadata.d.ts","sourceRoot":"","sources":["../../../src/common/string/createSearchLinkedMetadata.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,EAAE,EACF,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,4BAA4B,EAC7B,MAAM,aAAa,CAAC;AAGrB,YAAY,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,0BAA0B,CACxC,QAAQ,SAAS,gBAAgB,EACjC,OAAO,SAAS,gBAAgB,EAEhC,WAAW,EAAE,cAAc,CAAC,QAAQ,CAAC,EACrC,eAAe,EAAE,cAAc,EAC/B,UAAU,EAAE,cAAc,CAAC,OAAO,CAAC,EACnC,cAAc,EAAE,cAAc,EAC9B,gBAAgB,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,EAAE,GAAG,SAAS,GACpD,4BAA4B,CAAC,QAAQ,EAAE,OAAO,CAAC,CAoBjD"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * String token implementation - original string-based search
3
+ */
4
+ export { createSearchArrayMetadata, prepareObjectForSearch, extractSearchableStringDefault, isDeletedOrBlocked, MATCH_MODE_PREFIXES, PREFIX_TO_MATCH_MODE, wrapMetadataLegacy, unwrapMetadataLegacy, } from './createSearchArrayMetadata';
5
+ export { createSearchLinkedMetadata, } from './createSearchLinkedMetadata';
6
+ export { updateSearchMetadata, updateSearchMetadataBatch, removeFromSearchMetadata, removeFromSearchMetadataBatch, } from './updateSearchMetadata';
7
+ export type { SearchMetadata, SearchableObject, SearchableData, SearchableObjectSpec, SearchArrayMetadata, SearchableItem, SearchItemMetadata, } from './createSearchArrayMetadata';
8
+ export type { SearchLinkedMetadataWithData } from './createSearchLinkedMetadata';
9
+ export type { UpdateSearchMetadataBatchItem } from './updateSearchMetadata';
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/common/string/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,yBAAyB,EACzB,sBAAsB,EACtB,8BAA8B,EAC9B,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,6BAA6B,CAAC;AAErC,OAAO,EACL,0BAA0B,GAC3B,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EACL,oBAAoB,EACpB,yBAAyB,EACzB,wBAAwB,EACxB,6BAA6B,GAC9B,MAAM,wBAAwB,CAAC;AAGhC,YAAY,EACV,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,oBAAoB,EACpB,mBAAmB,EACnB,cAAc,EACd,kBAAkB,GACnB,MAAM,6BAA6B,CAAC;AAErC,YAAY,EAAE,4BAA4B,EAAE,MAAM,8BAA8B,CAAC;AACjF,YAAY,EAAE,6BAA6B,EAAE,MAAM,wBAAwB,CAAC"}
@@ -0,0 +1,103 @@
1
+ import type { Id, SearchMetadata, SearchableObject, SearchableObjectSpec, UpdateSearchMetadataBatchItem } from '../../types';
2
+ export type { UpdateSearchMetadataBatchItem } from '../../types';
3
+ /**
4
+ * Updates search metadata for a single object.
5
+ *
6
+ * This function handles updates, additions, and removals:
7
+ * - If the object is `null` or `undefined`, removes it from metadata
8
+ * - If the object is `_deleted` or `_blocked`, removes it from metadata
9
+ * - Otherwise, computes new tokens and updates/adds to metadata
10
+ *
11
+ * The metadata map is mutated in place for efficiency.
12
+ *
13
+ * @typeParam T - Type of the searchable object
14
+ * @typeParam C - Type of the search context
15
+ *
16
+ * @param metadata - The SearchMetadata map to update (mutated in place)
17
+ * @param objectId - The ID of the object to update
18
+ * @param obj - The object (may be null, undefined, deleted, or blocked)
19
+ * @param spec - Optional specification for how to extract searchable text
20
+ * @returns true if metadata was added/updated, false if removed
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * // Update an existing item
25
+ * const product = { _id: '1', name: 'Updated Product' };
26
+ * updateSearchMetadata(metadata, '1', product, spec);
27
+ *
28
+ * // Mark an item as deleted (removes from search)
29
+ * const deletedProduct = { _id: '1', name: 'Product', _deleted: new Date() };
30
+ * updateSearchMetadata(metadata, '1', deletedProduct, spec); // returns false
31
+ *
32
+ * // Remove by passing null/undefined
33
+ * updateSearchMetadata(metadata, '1', null, spec); // returns false
34
+ * ```
35
+ */
36
+ export declare function updateSearchMetadata<T extends SearchableObject, C = unknown>(metadata: SearchMetadata, objectId: Id, obj: T | null | undefined, spec?: SearchableObjectSpec<T, C>): boolean;
37
+ /**
38
+ * Updates search metadata for multiple objects in a batch.
39
+ *
40
+ * More efficient than calling {@link updateSearchMetadata} repeatedly
41
+ * when you have multiple objects to update at once.
42
+ *
43
+ * - For objects that are `_deleted` or `_blocked`, removes them from metadata
44
+ * - For other objects, updates or adds their tokens to metadata
45
+ *
46
+ * @typeParam T - Type of the searchable objects
47
+ * @typeParam C - Type of the search context
48
+ *
49
+ * @param metadata - The SearchMetadata map to update (mutated in place)
50
+ * @param batch - Array of objects to update
51
+ * @param spec - Optional specification for how to extract searchable text
52
+ * @returns Object with counts of added/updated and removed items
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * const batch = [
57
+ * { objectId: '1', obj: { _id: '1', name: 'Product 1' } },
58
+ * { objectId: '2', obj: { _id: '2', name: 'Product 2', _deleted: new Date() } },
59
+ * ];
60
+ *
61
+ * const result = updateSearchMetadataBatch(metadata, batch, spec);
62
+ * // result: { updated: 1, removed: 1 }
63
+ * ```
64
+ */
65
+ export declare function updateSearchMetadataBatch<T extends SearchableObject, C = unknown>(metadata: SearchMetadata, batch: UpdateSearchMetadataBatchItem<T>[], spec?: SearchableObjectSpec<T, C>): {
66
+ updated: number;
67
+ removed: number;
68
+ };
69
+ /**
70
+ * Removes an object from search metadata by ID.
71
+ *
72
+ * Use this when you want to explicitly remove an object without having the full object.
73
+ * For updates where you have the object (including deleting by setting `_deleted`),
74
+ * use {@link updateSearchMetadata} instead.
75
+ *
76
+ * @param metadata - The SearchMetadata map to update (mutated in place)
77
+ * @param objectId - The ID of the object to remove
78
+ * @returns true if the object was found and removed, false if it wasn't in metadata
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * removeFromSearchMetadata(metadata, 'object-id-to-remove');
83
+ * ```
84
+ */
85
+ export declare function removeFromSearchMetadata(metadata: SearchMetadata, objectId: Id): boolean;
86
+ /**
87
+ * Removes multiple objects from search metadata by IDs.
88
+ *
89
+ * More efficient than calling {@link removeFromSearchMetadata} repeatedly
90
+ * when you have multiple IDs to remove.
91
+ *
92
+ * @param metadata - The SearchMetadata map to update (mutated in place)
93
+ * @param objectIds - Array of IDs to remove
94
+ * @returns Number of objects that were actually removed (may be less than objectIds.length)
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * const removed = removeFromSearchMetadataBatch(metadata, ['id1', 'id2', 'id3']);
99
+ * console.log(`Removed ${removed} items from metadata`);
100
+ * ```
101
+ */
102
+ export declare function removeFromSearchMetadataBatch(metadata: SearchMetadata, objectIds: Id[]): number;
103
+ //# sourceMappingURL=updateSearchMetadata.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"updateSearchMetadata.d.ts","sourceRoot":"","sources":["../../../src/common/string/updateSearchMetadata.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,EAAE,EACF,cAAc,EACd,gBAAgB,EAChB,oBAAoB,EACpB,6BAA6B,EAC9B,MAAM,aAAa,CAAC;AAIrB,YAAY,EAAE,6BAA6B,EAAE,MAAM,aAAa,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,gBAAgB,EAAE,CAAC,GAAG,OAAO,EAC1E,QAAQ,EAAE,cAAc,EACxB,QAAQ,EAAE,EAAE,EACZ,GAAG,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EACzB,IAAI,CAAC,EAAE,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,GAChC,OAAO,CAYT;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,SAAS,gBAAgB,EAAE,CAAC,GAAG,OAAO,EAC/E,QAAQ,EAAE,cAAc,EACxB,KAAK,EAAE,6BAA6B,CAAC,CAAC,CAAC,EAAE,EACzC,IAAI,CAAC,EAAE,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,GAChC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAkBtC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,cAAc,EACxB,QAAQ,EAAE,EAAE,GACX,OAAO,CAET;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,6BAA6B,CAC3C,QAAQ,EAAE,cAAc,EACxB,SAAS,EAAE,EAAE,EAAE,GACd,MAAM,CAYR"}
@@ -0,0 +1,64 @@
1
+ import type { SearchMetadata, SearchableData, SearchableObject, SearchableObjectSpec, SyncResult, SyncOptions } from '../types';
2
+ export type { SyncResult, SyncOptions } from '../types';
3
+ /**
4
+ * Syncs search metadata with new data.
5
+ *
6
+ * This function performs a full sync of metadata against the provided data:
7
+ * - Adds metadata for new items
8
+ * - Removes metadata for items no longer in data (or marked `_deleted`/`_blocked`)
9
+ * - Updates metadata for items whose tokens have changed
10
+ *
11
+ * Use this when you have completely new data to sync with. If you have both
12
+ * previous and new data available, use {@link syncSearchArrayMetadataWithPrevious}
13
+ * for more efficient diffing.
14
+ *
15
+ * @typeParam T - Type of the searchable objects
16
+ * @typeParam C - Type of the search context
17
+ *
18
+ * @param metadata - The SearchMetadata map to update (mutated in place)
19
+ * @param newData - The new data to sync with
20
+ * @param spec - Optional specification for how to extract searchable text
21
+ * @returns SyncResult with counts and IDs of added/updated/removed items
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * const newProducts = arrayToSearchableData(freshProductsFromServer);
26
+ * const result = syncSearchArrayMetadata(metadata, newProducts, spec);
27
+ *
28
+ * console.log(`Added: ${result.added}, Updated: ${result.updated}, Removed: ${result.removed}`);
29
+ * ```
30
+ */
31
+ export declare function syncSearchArrayMetadata<T extends SearchableObject, C = unknown>(metadata: SearchMetadata, newData: SearchableData<T>, spec?: SearchableObjectSpec<T, C>): SyncResult;
32
+ /**
33
+ * Syncs search metadata using previous and new data for efficient diffing.
34
+ *
35
+ * More efficient than {@link syncSearchArrayMetadata} when you have both the
36
+ * previous and new data available. Uses the previous data to determine
37
+ * what was removed without iterating through all metadata keys.
38
+ *
39
+ * @typeParam T - Type of the searchable objects
40
+ * @typeParam C - Type of the search context
41
+ *
42
+ * @param metadata - The SearchMetadata map to update (mutated in place)
43
+ * @param previousData - The previous data (used to determine what was removed)
44
+ * @param newData - The new data to sync with
45
+ * @param options - Optional sync options (e.g., skipUpdateCheck)
46
+ * @param spec - Optional specification for how to extract searchable text
47
+ * @returns SyncResult with counts and IDs of added/updated/removed items
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * const previousProducts = currentData;
52
+ * const newProducts = arrayToSearchableData(freshProductsFromServer);
53
+ *
54
+ * const result = syncSearchArrayMetadataWithPrevious(
55
+ * metadata,
56
+ * previousProducts,
57
+ * newProducts,
58
+ * { skipUpdateCheck: false },
59
+ * spec
60
+ * );
61
+ * ```
62
+ */
63
+ export declare function syncSearchArrayMetadataWithPrevious<T extends SearchableObject, C = unknown>(metadata: SearchMetadata, previousData: SearchableData<T>, newData: SearchableData<T>, options?: SyncOptions, spec?: SearchableObjectSpec<T, C>): SyncResult;
64
+ //# sourceMappingURL=syncSearchArrayMetadata.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"syncSearchArrayMetadata.d.ts","sourceRoot":"","sources":["../../src/common/syncSearchArrayMetadata.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,gBAAgB,EAChB,oBAAoB,EAEpB,UAAU,EACV,WAAW,EACZ,MAAM,UAAU,CAAC;AAIlB,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAkBxD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,SAAS,gBAAgB,EAAE,CAAC,GAAG,OAAO,EAC7E,QAAQ,EAAE,cAAc,EACxB,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,EAC1B,IAAI,CAAC,EAAE,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,GAChC,UAAU,CAiDZ;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,mCAAmC,CAAC,CAAC,SAAS,gBAAgB,EAAE,CAAC,GAAG,OAAO,EACzF,QAAQ,EAAE,cAAc,EACxB,YAAY,EAAE,cAAc,CAAC,CAAC,CAAC,EAC/B,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,EAC1B,OAAO,CAAC,EAAE,WAAW,EACrB,IAAI,CAAC,EAAE,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,GAChC,UAAU,CAmDZ"}
@@ -0,0 +1,129 @@
1
+ import type { Id, SearchableData, SearchableObject, SearchableObjectSpec, SearchLinkedMetadataWithData, UpsertResult, SyncLinkedResult } from '../types';
2
+ export type { UpsertResult, SyncLinkedResult } from '../types';
3
+ /**
4
+ * Upserts a primary item into the linked metadata.
5
+ *
6
+ * - If the item doesn't exist, adds it to data, metadata, and initializes primaryToLinked index
7
+ * - If the item exists, updates data and metadata
8
+ * - Handles `_deleted`/`_blocked` items by removing their metadata
9
+ *
10
+ * @typeParam TPrimary - Type of primary items
11
+ * @typeParam TLinked - Type of linked items
12
+ * @typeParam C - Type of the search context
13
+ *
14
+ * @param linked - The linked metadata to update (mutated in place)
15
+ * @param item - The primary item to upsert
16
+ * @param spec - Optional specification for how to extract searchable text
17
+ * @returns UpsertResult indicating if item was added or updated
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * const stranka = { _id: 's1', ime: 'Janez', priimek: 'Novak' };
22
+ * const result = upsertPrimaryItem(linkedMeta, stranka, spec);
23
+ * // result.added === true if stranka was new
24
+ * ```
25
+ */
26
+ export declare function upsertPrimaryItem<TPrimary extends SearchableObject, TLinked extends SearchableObject, C = unknown>(linked: SearchLinkedMetadataWithData<TPrimary, TLinked>, item: TPrimary, spec?: SearchableObjectSpec<TPrimary, C>): UpsertResult;
27
+ /**
28
+ * Removes a primary item from the linked metadata.
29
+ *
30
+ * Removes the item from data, metadata, and the primaryToLinked index.
31
+ * Note: This does NOT automatically remove linked items - they will become orphans.
32
+ * If you want to also remove linked items, call {@link removeLinkedItem} for each.
33
+ *
34
+ * @typeParam TPrimary - Type of primary items
35
+ * @typeParam TLinked - Type of linked items
36
+ *
37
+ * @param linked - The linked metadata to update (mutated in place)
38
+ * @param primaryId - The ID of the primary item to remove
39
+ * @returns true if item was found and removed, false if not found
40
+ */
41
+ export declare function removePrimaryItem<TPrimary extends SearchableObject, TLinked extends SearchableObject>(linked: SearchLinkedMetadataWithData<TPrimary, TLinked>, primaryId: Id): boolean;
42
+ /**
43
+ * Upserts a linked item into the linked metadata.
44
+ *
45
+ * - If the item doesn't exist, adds it
46
+ * - If the item exists, updates it
47
+ * - Handles foreign key changes by updating the primaryToLinked index
48
+ * (removes from old primary's list, adds to new primary's list)
49
+ * - Handles `_deleted`/`_blocked` items by removing their metadata
50
+ *
51
+ * @typeParam TPrimary - Type of primary items
52
+ * @typeParam TLinked - Type of linked items
53
+ * @typeParam C - Type of the search context
54
+ *
55
+ * @param linked - The linked metadata to update (mutated in place)
56
+ * @param item - The linked item to upsert
57
+ * @param foreignKeyGetter - Function to get the primary ID from a linked item
58
+ * @param spec - Optional specification for how to extract searchable text
59
+ * @returns UpsertResult indicating if item was added or updated, and previous primary ID if FK changed
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const pacient = { _id: 'p1', ime: 'Rex', stranka_id: 's1' };
64
+ * const result = upsertLinkedItem(linkedMeta, pacient, (p) => p.stranka_id, spec);
65
+ *
66
+ * // If the pacient moved to a different stranka:
67
+ * pacient.stranka_id = 's2';
68
+ * const result2 = upsertLinkedItem(linkedMeta, pacient, (p) => p.stranka_id, spec);
69
+ * // result2.previousPrimaryId === 's1'
70
+ * ```
71
+ */
72
+ export declare function upsertLinkedItem<TPrimary extends SearchableObject, TLinked extends SearchableObject, C = unknown>(linked: SearchLinkedMetadataWithData<TPrimary, TLinked>, item: TLinked, foreignKeyGetter: (item: TLinked) => Id | undefined, spec?: SearchableObjectSpec<TLinked, C>): UpsertResult;
73
+ /**
74
+ * Removes a linked item from the linked metadata.
75
+ *
76
+ * Removes the item from data, metadata, and the primaryToLinked index.
77
+ *
78
+ * @typeParam TPrimary - Type of primary items
79
+ * @typeParam TLinked - Type of linked items
80
+ *
81
+ * @param linked - The linked metadata to update (mutated in place)
82
+ * @param linkedId - The ID of the linked item to remove
83
+ * @param foreignKeyGetter - Function to get the primary ID from a linked item
84
+ * @returns true if item was found and removed, false if not found
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * removeLinkedItem(linkedMeta, 'pacient-id', (p) => p.stranka_id);
89
+ * ```
90
+ */
91
+ export declare function removeLinkedItem<TPrimary extends SearchableObject, TLinked extends SearchableObject>(linked: SearchLinkedMetadataWithData<TPrimary, TLinked>, linkedId: Id, foreignKeyGetter: (item: TLinked) => Id | undefined): boolean;
92
+ /**
93
+ * Syncs all linked items for a specific primary item.
94
+ *
95
+ * This function performs a full sync of linked items belonging to a single primary:
96
+ * - Removes linked items that are no longer in the new data
97
+ * - Updates existing linked items if their tokens changed
98
+ * - Adds new linked items
99
+ * - Only processes items whose foreign key points to the given primary
100
+ *
101
+ * Use this when you have a complete new set of linked items for a primary
102
+ * and want to update the metadata to match.
103
+ *
104
+ * @typeParam TPrimary - Type of primary items
105
+ * @typeParam TLinked - Type of linked items
106
+ * @typeParam C - Type of the search context
107
+ *
108
+ * @param linked - The linked metadata to update (mutated in place)
109
+ * @param primaryId - The primary ID to sync linked items for
110
+ * @param newLinkedItems - The new linked items (only items with matching FK will be processed)
111
+ * @param foreignKeyGetter - Function to get the primary ID from a linked item
112
+ * @param spec - Optional specification for how to extract searchable text
113
+ * @returns SyncLinkedResult with counts of added/updated/removed items
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * const newPacienti = arrayToSearchableData(freshPacientiFromServer);
118
+ * const result = syncLinkedItemsForPrimary(
119
+ * linkedMeta,
120
+ * 'stranka-1',
121
+ * newPacienti,
122
+ * (p) => p.stranka_id,
123
+ * spec
124
+ * );
125
+ * console.log(`Added: ${result.added}, Updated: ${result.updated}, Removed: ${result.removed}`);
126
+ * ```
127
+ */
128
+ export declare function syncLinkedItemsForPrimary<TPrimary extends SearchableObject, TLinked extends SearchableObject, C = unknown>(linked: SearchLinkedMetadataWithData<TPrimary, TLinked>, primaryId: Id, newLinkedItems: SearchableData<TLinked>, foreignKeyGetter: (item: TLinked) => Id | undefined, spec?: SearchableObjectSpec<TLinked, C>): SyncLinkedResult;
129
+ //# sourceMappingURL=updateSearchLinkedMetadata.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"updateSearchLinkedMetadata.d.ts","sourceRoot":"","sources":["../../src/common/updateSearchLinkedMetadata.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,EAAE,EACF,cAAc,EACd,gBAAgB,EAChB,oBAAoB,EACpB,4BAA4B,EAC5B,YAAY,EACZ,gBAAgB,EACjB,MAAM,UAAU,CAAC;AAIlB,YAAY,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAkB/D;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,SAAS,gBAAgB,EACjC,OAAO,SAAS,gBAAgB,EAChC,CAAC,GAAG,OAAO,EAEX,MAAM,EAAE,4BAA4B,CAAC,QAAQ,EAAE,OAAO,CAAC,EACvD,IAAI,EAAE,QAAQ,EACd,IAAI,CAAC,EAAE,oBAAoB,CAAC,QAAQ,EAAE,CAAC,CAAC,GACvC,YAAY,CAqBd;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,SAAS,gBAAgB,EACjC,OAAO,SAAS,gBAAgB,EAChC,MAAM,EAAE,4BAA4B,CAAC,QAAQ,EAAE,OAAO,CAAC,EAAE,SAAS,EAAE,EAAE,GAAG,OAAO,CAUjF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,SAAS,gBAAgB,EACjC,OAAO,SAAS,gBAAgB,EAChC,CAAC,GAAG,OAAO,EAEX,MAAM,EAAE,4BAA4B,CAAC,QAAQ,EAAE,OAAO,CAAC,EACvD,IAAI,EAAE,OAAO,EACb,gBAAgB,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,EAAE,GAAG,SAAS,EACnD,IAAI,CAAC,EAAE,oBAAoB,CAAC,OAAO,EAAE,CAAC,CAAC,GACtC,YAAY,CAuDd;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,SAAS,gBAAgB,EACjC,OAAO,SAAS,gBAAgB,EAEhC,MAAM,EAAE,4BAA4B,CAAC,QAAQ,EAAE,OAAO,CAAC,EACvD,QAAQ,EAAE,EAAE,EACZ,gBAAgB,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,EAAE,GAAG,SAAS,GAClD,OAAO,CAyBT;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,yBAAyB,CACvC,QAAQ,SAAS,gBAAgB,EACjC,OAAO,SAAS,gBAAgB,EAChC,CAAC,GAAG,OAAO,EAEX,MAAM,EAAE,4BAA4B,CAAC,QAAQ,EAAE,OAAO,CAAC,EACvD,SAAS,EAAE,EAAE,EACb,cAAc,EAAE,cAAc,CAAC,OAAO,CAAC,EACvC,gBAAgB,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,EAAE,GAAG,SAAS,EACnD,IAAI,CAAC,EAAE,oBAAoB,CAAC,OAAO,EAAE,CAAC,CAAC,GACtC,gBAAgB,CA2DlB"}