@orion-js/helpers 3.11.6 → 3.11.15

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 (42) hide show
  1. package/lib/Errors/OrionError.d.ts +26 -0
  2. package/lib/Errors/OrionError.js +13 -0
  3. package/lib/Errors/OrionError.test.d.ts +1 -0
  4. package/lib/Errors/OrionError.test.js +52 -0
  5. package/lib/Errors/PermissionsError.d.ts +33 -0
  6. package/lib/Errors/PermissionsError.js +33 -0
  7. package/lib/Errors/PermissionsError.test.d.ts +1 -0
  8. package/lib/Errors/PermissionsError.test.js +62 -0
  9. package/lib/Errors/UserError.d.ts +29 -0
  10. package/lib/Errors/UserError.js +29 -0
  11. package/lib/Errors/UserError.test.d.ts +1 -0
  12. package/lib/Errors/UserError.test.js +49 -0
  13. package/lib/Errors/index.d.ts +43 -0
  14. package/lib/Errors/index.js +57 -0
  15. package/lib/Errors/index.test.d.ts +1 -0
  16. package/lib/Errors/index.test.js +56 -0
  17. package/lib/createMap.d.ts +16 -0
  18. package/lib/createMap.js +16 -0
  19. package/lib/createMap.test.d.ts +1 -0
  20. package/lib/createMap.test.js +74 -0
  21. package/lib/createMapArray.d.ts +21 -0
  22. package/lib/createMapArray.js +21 -0
  23. package/lib/createMapArray.test.d.ts +1 -0
  24. package/lib/createMapArray.test.js +101 -0
  25. package/lib/generateUUID.d.ts +1 -0
  26. package/lib/generateUUID.js +5 -1
  27. package/lib/generateUUID.test.js +4 -0
  28. package/lib/index.d.ts +5 -4
  29. package/lib/index.js +12 -7
  30. package/lib/normalize.d.ts +70 -0
  31. package/lib/normalize.js +111 -0
  32. package/lib/normalize.test.d.ts +1 -0
  33. package/lib/normalize.test.js +101 -0
  34. package/lib/retries.d.ts +22 -0
  35. package/lib/retries.js +22 -0
  36. package/lib/searchTokens.d.ts +61 -0
  37. package/lib/searchTokens.js +78 -0
  38. package/lib/searchTokens.test.d.ts +1 -0
  39. package/lib/searchTokens.test.js +101 -0
  40. package/lib/shortenMongoId.d.ts +1 -0
  41. package/lib/shortenMongoId.js +10 -0
  42. package/package.json +2 -2
@@ -1 +1,22 @@
1
+ /**
2
+ * Creates a grouped map from an array of items, using a specified property as the key.
3
+ *
4
+ * This utility transforms an array of objects into a lookup object/dictionary where
5
+ * each value is an array of items sharing the same key value. Unlike createMap,
6
+ * this function preserves all items with the same key by grouping them in arrays.
7
+ *
8
+ * @template T The type of items in the input array
9
+ * @param array - The input array of items to transform into a grouped map
10
+ * @param key - The property name to use as keys in the resulting map (defaults to '_id')
11
+ * @returns A record object where keys are values of the specified property and values are arrays of items
12
+ *
13
+ * @example
14
+ * // Returns { 'category1': [{ id: 1, category: 'category1' }, { id: 3, category: 'category1' }],
15
+ * // 'category2': [{ id: 2, category: 'category2' }] }
16
+ * createMapArray([
17
+ * { id: 1, category: 'category1' },
18
+ * { id: 2, category: 'category2' },
19
+ * { id: 3, category: 'category1' }
20
+ * ], 'category')
21
+ */
1
22
  export default function createMapArray<T>(array: Array<T>, key?: string): Record<string, Array<T>>;
@@ -1,5 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Creates a grouped map from an array of items, using a specified property as the key.
5
+ *
6
+ * This utility transforms an array of objects into a lookup object/dictionary where
7
+ * each value is an array of items sharing the same key value. Unlike createMap,
8
+ * this function preserves all items with the same key by grouping them in arrays.
9
+ *
10
+ * @template T The type of items in the input array
11
+ * @param array - The input array of items to transform into a grouped map
12
+ * @param key - The property name to use as keys in the resulting map (defaults to '_id')
13
+ * @returns A record object where keys are values of the specified property and values are arrays of items
14
+ *
15
+ * @example
16
+ * // Returns { 'category1': [{ id: 1, category: 'category1' }, { id: 3, category: 'category1' }],
17
+ * // 'category2': [{ id: 2, category: 'category2' }] }
18
+ * createMapArray([
19
+ * { id: 1, category: 'category1' },
20
+ * { id: 2, category: 'category2' },
21
+ * { id: 3, category: 'category1' }
22
+ * ], 'category')
23
+ */
3
24
  function createMapArray(array, key = '_id') {
4
25
  const map = {};
5
26
  for (const item of array) {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const createMapArray_1 = __importDefault(require("./createMapArray"));
7
+ describe('createMapArray', () => {
8
+ it('should create a map of arrays using the default _id key', () => {
9
+ const array = [
10
+ { _id: '1', name: 'Item 1' },
11
+ { _id: '2', name: 'Item 2' },
12
+ { _id: '1', name: 'Item 1 Duplicate' }
13
+ ];
14
+ const result = (0, createMapArray_1.default)(array);
15
+ expect(result).toEqual({
16
+ '1': [
17
+ { _id: '1', name: 'Item 1' },
18
+ { _id: '1', name: 'Item 1 Duplicate' }
19
+ ],
20
+ '2': [
21
+ { _id: '2', name: 'Item 2' }
22
+ ]
23
+ });
24
+ });
25
+ it('should create a map of arrays using a custom key', () => {
26
+ const array = [
27
+ { category: 'A', name: 'Item A1' },
28
+ { category: 'B', name: 'Item B1' },
29
+ { category: 'A', name: 'Item A2' },
30
+ { category: 'C', name: 'Item C1' },
31
+ { category: 'B', name: 'Item B2' }
32
+ ];
33
+ const result = (0, createMapArray_1.default)(array, 'category');
34
+ expect(result).toEqual({
35
+ 'A': [
36
+ { category: 'A', name: 'Item A1' },
37
+ { category: 'A', name: 'Item A2' }
38
+ ],
39
+ 'B': [
40
+ { category: 'B', name: 'Item B1' },
41
+ { category: 'B', name: 'Item B2' }
42
+ ],
43
+ 'C': [
44
+ { category: 'C', name: 'Item C1' }
45
+ ]
46
+ });
47
+ });
48
+ it('should handle empty arrays', () => {
49
+ const result = (0, createMapArray_1.default)([]);
50
+ expect(result).toEqual({});
51
+ });
52
+ it('should preserve the original order of items within each group', () => {
53
+ const array = [
54
+ { type: 'fruit', name: 'apple', order: 1 },
55
+ { type: 'vegetable', name: 'carrot', order: 1 },
56
+ { type: 'fruit', name: 'banana', order: 2 },
57
+ { type: 'fruit', name: 'cherry', order: 3 },
58
+ { type: 'vegetable', name: 'broccoli', order: 2 }
59
+ ];
60
+ const result = (0, createMapArray_1.default)(array, 'type');
61
+ // Items should be grouped by type and maintain their original order
62
+ expect(result.fruit.map(item => item.order)).toEqual([1, 2, 3]);
63
+ expect(result.vegetable.map(item => item.order)).toEqual([1, 2]);
64
+ });
65
+ it('should handle various key types', () => {
66
+ const array = [
67
+ { key: 1, value: 'Number key 1' },
68
+ { key: 1, value: 'Number key 2' },
69
+ { key: 'string', value: 'String key 1' },
70
+ { key: 'string', value: 'String key 2' },
71
+ { key: true, value: 'Boolean key' }
72
+ ];
73
+ const result = (0, createMapArray_1.default)(array, 'key');
74
+ expect(result).toEqual({
75
+ '1': [
76
+ { key: 1, value: 'Number key 1' },
77
+ { key: 1, value: 'Number key 2' }
78
+ ],
79
+ 'string': [
80
+ { key: 'string', value: 'String key 1' },
81
+ { key: 'string', value: 'String key 2' }
82
+ ],
83
+ 'true': [
84
+ { key: true, value: 'Boolean key' }
85
+ ]
86
+ });
87
+ });
88
+ it('should handle items with missing keys by using undefined', () => {
89
+ const array = [
90
+ { id: '1', name: 'Has ID' },
91
+ { name: 'No ID' },
92
+ { id: '2', name: 'Has ID 2' }
93
+ ];
94
+ const result = (0, createMapArray_1.default)(array, 'id');
95
+ expect(result).toEqual({
96
+ '1': [{ id: '1', name: 'Has ID' }],
97
+ '2': [{ id: '2', name: 'Has ID 2' }],
98
+ 'undefined': [{ name: 'No ID' }]
99
+ });
100
+ });
101
+ });
@@ -1 +1,2 @@
1
1
  export declare function generateUUID(): any;
2
+ export declare function generateUUIDWithPrefix(prefix: string): string;
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateUUID = void 0;
3
+ exports.generateUUIDWithPrefix = exports.generateUUID = void 0;
4
4
  const uuid_1 = require("uuid");
5
5
  function generateUUID() {
6
6
  return (0, uuid_1.v4)();
7
7
  }
8
8
  exports.generateUUID = generateUUID;
9
+ function generateUUIDWithPrefix(prefix) {
10
+ return `${prefix}-${generateUUID()}`;
11
+ }
12
+ exports.generateUUIDWithPrefix = generateUUIDWithPrefix;
@@ -9,3 +9,7 @@ it('should generate random uuid v4', async () => {
9
9
  expect((0, generateUUID_1.generateUUID)()).not.toBe((0, generateUUID_1.generateUUID)());
10
10
  expect((0, isString_1.default)((0, generateUUID_1.generateUUID)())).toBe(true);
11
11
  });
12
+ it('should generate uuid with prefix', async () => {
13
+ expect((0, generateUUID_1.generateUUIDWithPrefix)('test')).not.toBe((0, generateUUID_1.generateUUIDWithPrefix)('test'));
14
+ expect((0, isString_1.default)((0, generateUUID_1.generateUUIDWithPrefix)('test'))).toBe(true);
15
+ });
package/lib/index.d.ts CHANGED
@@ -2,11 +2,12 @@ import sleep from './sleep';
2
2
  import hashObject from './hashObject';
3
3
  import generateId from './generateId';
4
4
  import createMap from './createMap';
5
- import { OrionError, OrionErrorInformation } from './Errors/OrionError';
6
- import PermissionsError from './Errors/PermissionsError';
7
- import UserError from './Errors/UserError';
8
5
  import createMapArray from './createMapArray';
6
+ import { OrionError, OrionErrorInformation, PermissionsError, UserError, isOrionError, isUserError, isPermissionsError } from './Errors';
9
7
  export * from './composeMiddlewares';
10
8
  export * from './retries';
11
9
  export * from './generateUUID';
12
- export { createMap, createMapArray, generateId, hashObject, sleep, OrionError, PermissionsError, UserError, OrionErrorInformation };
10
+ export * from './normalize';
11
+ export * from './searchTokens';
12
+ export * from './shortenMongoId';
13
+ export { createMap, createMapArray, generateId, hashObject, sleep, OrionError, PermissionsError, UserError, OrionErrorInformation, isOrionError, isUserError, isPermissionsError };
package/lib/index.js CHANGED
@@ -13,7 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  return (mod && mod.__esModule) ? mod : { "default": mod };
14
14
  };
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.UserError = exports.PermissionsError = exports.OrionError = exports.sleep = exports.hashObject = exports.generateId = exports.createMapArray = exports.createMap = void 0;
16
+ exports.isPermissionsError = exports.isUserError = exports.isOrionError = exports.UserError = exports.PermissionsError = exports.OrionError = exports.sleep = exports.hashObject = exports.generateId = exports.createMapArray = exports.createMap = void 0;
17
17
  const sleep_1 = __importDefault(require("./sleep"));
18
18
  exports.sleep = sleep_1.default;
19
19
  const hashObject_1 = __importDefault(require("./hashObject"));
@@ -22,14 +22,19 @@ const generateId_1 = __importDefault(require("./generateId"));
22
22
  exports.generateId = generateId_1.default;
23
23
  const createMap_1 = __importDefault(require("./createMap"));
24
24
  exports.createMap = createMap_1.default;
25
- const OrionError_1 = require("./Errors/OrionError");
26
- Object.defineProperty(exports, "OrionError", { enumerable: true, get: function () { return OrionError_1.OrionError; } });
27
- const PermissionsError_1 = __importDefault(require("./Errors/PermissionsError"));
28
- exports.PermissionsError = PermissionsError_1.default;
29
- const UserError_1 = __importDefault(require("./Errors/UserError"));
30
- exports.UserError = UserError_1.default;
31
25
  const createMapArray_1 = __importDefault(require("./createMapArray"));
32
26
  exports.createMapArray = createMapArray_1.default;
27
+ // Import all error-related exports from the Errors module
28
+ const Errors_1 = require("./Errors");
29
+ Object.defineProperty(exports, "OrionError", { enumerable: true, get: function () { return Errors_1.OrionError; } });
30
+ Object.defineProperty(exports, "PermissionsError", { enumerable: true, get: function () { return Errors_1.PermissionsError; } });
31
+ Object.defineProperty(exports, "UserError", { enumerable: true, get: function () { return Errors_1.UserError; } });
32
+ Object.defineProperty(exports, "isOrionError", { enumerable: true, get: function () { return Errors_1.isOrionError; } });
33
+ Object.defineProperty(exports, "isUserError", { enumerable: true, get: function () { return Errors_1.isUserError; } });
34
+ Object.defineProperty(exports, "isPermissionsError", { enumerable: true, get: function () { return Errors_1.isPermissionsError; } });
33
35
  __exportStar(require("./composeMiddlewares"), exports);
34
36
  __exportStar(require("./retries"), exports);
35
37
  __exportStar(require("./generateUUID"), exports);
38
+ __exportStar(require("./normalize"), exports);
39
+ __exportStar(require("./searchTokens"), exports);
40
+ __exportStar(require("./shortenMongoId"), exports);
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Removes diacritical marks (accents) from text without any other modifications.
3
+ * This is the most basic normalization function that others build upon.
4
+ *
5
+ * @param text - The input string to process
6
+ * @returns String with accents removed but otherwise unchanged
7
+ */
8
+ export declare function removeAccentsOnly(text: string): string;
9
+ /**
10
+ * Normalizes text by removing diacritical marks (accents) and trimming whitespace.
11
+ * Builds on removeAccentsOnly and adds whitespace trimming.
12
+ *
13
+ * @param text - The input string to normalize
14
+ * @returns Normalized string with accents removed and whitespace trimmed
15
+ */
16
+ export declare function removeAccentsAndTrim(text: string): string;
17
+ /**
18
+ * Normalizes text for search purposes by:
19
+ * - Removing diacritical marks (accents)
20
+ * - Converting to lowercase
21
+ * - Trimming whitespace
22
+ *
23
+ * Builds on removeAccentsAndTrim and adds lowercase conversion.
24
+ * Useful for case-insensitive and accent-insensitive text searching.
25
+ *
26
+ * @param text - The input string to normalize for search
27
+ * @returns Search-optimized string in lowercase with accents removed
28
+ */
29
+ export declare function normalizeForSearch(text: string): string;
30
+ /**
31
+ * Normalizes text for search purposes by:
32
+ * - Removing diacritical marks (accents)
33
+ * - Converting to lowercase
34
+ * - Trimming whitespace
35
+ * - Removing all spaces
36
+ *
37
+ * Builds on normalizeForSearch and removes all whitespace.
38
+ * Useful for compact search indexes or when spaces should be ignored in searches.
39
+ *
40
+ * @param text - The input string to normalize for compact search
41
+ * @returns Compact search-optimized string with no spaces
42
+ */
43
+ export declare function normalizeForCompactSearch(text: string): string;
44
+ /**
45
+ * Normalizes text for search token processing by:
46
+ * - Removing diacritical marks (accents)
47
+ * - Converting to lowercase
48
+ * - Trimming whitespace
49
+ * - Replacing all non-alphanumeric characters with spaces
50
+ *
51
+ * Builds on normalizeForSearch and replaces non-alphanumeric characters with spaces.
52
+ * Useful for tokenizing search terms where special characters should be treated as word separators.
53
+ *
54
+ * @param text - The input string to normalize for tokenized search
55
+ * @returns Search token string with only alphanumeric characters and spaces
56
+ */
57
+ export declare function normalizeForSearchToken(text: string): string;
58
+ /**
59
+ * Normalizes a string specifically for use as a file key (e.g., in S3 or other storage systems).
60
+ * Performs the following transformations:
61
+ * - Removes accents/diacritical marks
62
+ * - Replaces special characters with hyphens
63
+ * - Ensures only alphanumeric characters, hyphens, periods, and underscores remain
64
+ * - Replaces multiple consecutive hyphens with a single hyphen
65
+ * - Removes leading/trailing hyphens
66
+ *
67
+ * @param text - The input string to normalize for file key usage
68
+ * @returns A storage-safe string suitable for use as a file key
69
+ */
70
+ export declare function normalizeForFileKey(text: string): string;
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizeForFileKey = exports.normalizeForSearchToken = exports.normalizeForCompactSearch = exports.normalizeForSearch = exports.removeAccentsAndTrim = exports.removeAccentsOnly = void 0;
4
+ /**
5
+ * Removes diacritical marks (accents) from text without any other modifications.
6
+ * This is the most basic normalization function that others build upon.
7
+ *
8
+ * @param text - The input string to process
9
+ * @returns String with accents removed but otherwise unchanged
10
+ */
11
+ function removeAccentsOnly(text) {
12
+ if (!text)
13
+ return '';
14
+ // biome-ignore lint/suspicious/noMisleadingCharacterClass: Removes diacritical marks (accents)
15
+ return text.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
16
+ }
17
+ exports.removeAccentsOnly = removeAccentsOnly;
18
+ /**
19
+ * Normalizes text by removing diacritical marks (accents) and trimming whitespace.
20
+ * Builds on removeAccentsOnly and adds whitespace trimming.
21
+ *
22
+ * @param text - The input string to normalize
23
+ * @returns Normalized string with accents removed and whitespace trimmed
24
+ */
25
+ function removeAccentsAndTrim(text) {
26
+ if (!text)
27
+ return '';
28
+ return removeAccentsOnly(text).trim();
29
+ }
30
+ exports.removeAccentsAndTrim = removeAccentsAndTrim;
31
+ /**
32
+ * Normalizes text for search purposes by:
33
+ * - Removing diacritical marks (accents)
34
+ * - Converting to lowercase
35
+ * - Trimming whitespace
36
+ *
37
+ * Builds on removeAccentsAndTrim and adds lowercase conversion.
38
+ * Useful for case-insensitive and accent-insensitive text searching.
39
+ *
40
+ * @param text - The input string to normalize for search
41
+ * @returns Search-optimized string in lowercase with accents removed
42
+ */
43
+ function normalizeForSearch(text) {
44
+ if (!text)
45
+ return '';
46
+ return removeAccentsAndTrim(text).toLowerCase();
47
+ }
48
+ exports.normalizeForSearch = normalizeForSearch;
49
+ /**
50
+ * Normalizes text for search purposes by:
51
+ * - Removing diacritical marks (accents)
52
+ * - Converting to lowercase
53
+ * - Trimming whitespace
54
+ * - Removing all spaces
55
+ *
56
+ * Builds on normalizeForSearch and removes all whitespace.
57
+ * Useful for compact search indexes or when spaces should be ignored in searches.
58
+ *
59
+ * @param text - The input string to normalize for compact search
60
+ * @returns Compact search-optimized string with no spaces
61
+ */
62
+ function normalizeForCompactSearch(text) {
63
+ if (!text)
64
+ return '';
65
+ return normalizeForSearch(text).replace(/\s/g, '');
66
+ }
67
+ exports.normalizeForCompactSearch = normalizeForCompactSearch;
68
+ /**
69
+ * Normalizes text for search token processing by:
70
+ * - Removing diacritical marks (accents)
71
+ * - Converting to lowercase
72
+ * - Trimming whitespace
73
+ * - Replacing all non-alphanumeric characters with spaces
74
+ *
75
+ * Builds on normalizeForSearch and replaces non-alphanumeric characters with spaces.
76
+ * Useful for tokenizing search terms where special characters should be treated as word separators.
77
+ *
78
+ * @param text - The input string to normalize for tokenized search
79
+ * @returns Search token string with only alphanumeric characters and spaces
80
+ */
81
+ function normalizeForSearchToken(text) {
82
+ if (!text)
83
+ return '';
84
+ return normalizeForSearch(text).replace(/[^0-9a-z]/gi, ' ');
85
+ }
86
+ exports.normalizeForSearchToken = normalizeForSearchToken;
87
+ /**
88
+ * Normalizes a string specifically for use as a file key (e.g., in S3 or other storage systems).
89
+ * Performs the following transformations:
90
+ * - Removes accents/diacritical marks
91
+ * - Replaces special characters with hyphens
92
+ * - Ensures only alphanumeric characters, hyphens, periods, and underscores remain
93
+ * - Replaces multiple consecutive hyphens with a single hyphen
94
+ * - Removes leading/trailing hyphens
95
+ *
96
+ * @param text - The input string to normalize for file key usage
97
+ * @returns A storage-safe string suitable for use as a file key
98
+ */
99
+ function normalizeForFileKey(text) {
100
+ if (!text)
101
+ return '';
102
+ return removeAccentsOnly(text)
103
+ // Replace spaces and unwanted characters with hyphens
104
+ .replace(/[^a-zA-Z0-9-._]/g, '-')
105
+ // Replace multiple consecutive hyphens with single hyphen
106
+ .replace(/-+/g, '-')
107
+ // Remove leading/trailing hyphens
108
+ .trim()
109
+ .replace(/^-+|-+$/g, '');
110
+ }
111
+ exports.normalizeForFileKey = normalizeForFileKey;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const normalize_1 = require("./normalize");
4
+ describe('Text normalization functions', () => {
5
+ describe('removeAccentsAndTrim', () => {
6
+ it('should remove accents and trim whitespace', () => {
7
+ expect((0, normalize_1.removeAccentsAndTrim)(' héllô wôrld ')).toBe('hello world');
8
+ });
9
+ it('should handle empty string', () => {
10
+ expect((0, normalize_1.removeAccentsAndTrim)('')).toBe('');
11
+ });
12
+ it('should handle null or undefined', () => {
13
+ expect((0, normalize_1.removeAccentsAndTrim)(null)).toBe('');
14
+ expect((0, normalize_1.removeAccentsAndTrim)(undefined)).toBe('');
15
+ });
16
+ it('should preserve case', () => {
17
+ expect((0, normalize_1.removeAccentsAndTrim)('HÉLLÔ')).toBe('HELLO');
18
+ });
19
+ });
20
+ describe('normalizeForSearch', () => {
21
+ it('should remove accents, trim whitespace, and convert to lowercase', () => {
22
+ expect((0, normalize_1.normalizeForSearch)(' HÉLLÔ WÔRLD ')).toBe('hello world');
23
+ });
24
+ it('should handle empty string', () => {
25
+ expect((0, normalize_1.normalizeForSearch)('')).toBe('');
26
+ });
27
+ it('should handle null or undefined', () => {
28
+ expect((0, normalize_1.normalizeForSearch)(null)).toBe('');
29
+ expect((0, normalize_1.normalizeForSearch)(undefined)).toBe('');
30
+ });
31
+ });
32
+ describe('normalizeForCompactSearch', () => {
33
+ it('should remove accents, spaces, trim whitespace, and convert to lowercase', () => {
34
+ expect((0, normalize_1.normalizeForCompactSearch)(' HÉLLÔ WÔRLD ')).toBe('helloworld');
35
+ });
36
+ it('should handle empty string', () => {
37
+ expect((0, normalize_1.normalizeForCompactSearch)('')).toBe('');
38
+ });
39
+ it('should handle null or undefined', () => {
40
+ expect((0, normalize_1.normalizeForCompactSearch)(null)).toBe('');
41
+ expect((0, normalize_1.normalizeForCompactSearch)(undefined)).toBe('');
42
+ });
43
+ it('should remove all types of whitespace', () => {
44
+ expect((0, normalize_1.normalizeForCompactSearch)('hello\tworld\nnew\rline')).toBe('helloworldnewline');
45
+ });
46
+ });
47
+ describe('normalizeForFileKey', () => {
48
+ it('should normalize text for file key usage', () => {
49
+ expect((0, normalize_1.normalizeForFileKey)('Héllô Wôrld!')).toBe('Hello-World');
50
+ });
51
+ it('should replace special characters with hyphens', () => {
52
+ expect((0, normalize_1.normalizeForFileKey)('file@name#with$special&chars')).toBe('file-name-with-special-chars');
53
+ });
54
+ it('should handle empty string', () => {
55
+ expect((0, normalize_1.normalizeForFileKey)('')).toBe('');
56
+ });
57
+ it('should handle null or undefined', () => {
58
+ expect((0, normalize_1.normalizeForFileKey)(null)).toBe('');
59
+ expect((0, normalize_1.normalizeForFileKey)(undefined)).toBe('');
60
+ });
61
+ it('should replace multiple consecutive hyphens with a single hyphen', () => {
62
+ expect((0, normalize_1.normalizeForFileKey)('multiple---hyphens')).toBe('multiple-hyphens');
63
+ });
64
+ it('should remove leading and trailing hyphens', () => {
65
+ expect((0, normalize_1.normalizeForFileKey)('-leading-and-trailing-')).toBe('leading-and-trailing');
66
+ });
67
+ it('should preserve periods and underscores', () => {
68
+ expect((0, normalize_1.normalizeForFileKey)('file.name_with.underscore')).toBe('file.name_with.underscore');
69
+ });
70
+ });
71
+ describe('removeAccentsOnly', () => {
72
+ it('should remove accents without trimming whitespace', () => {
73
+ expect((0, normalize_1.removeAccentsOnly)(' héllô wôrld ')).toBe(' hello world ');
74
+ });
75
+ it('should handle empty string', () => {
76
+ expect((0, normalize_1.removeAccentsOnly)('')).toBe('');
77
+ });
78
+ it('should handle null or undefined', () => {
79
+ expect((0, normalize_1.removeAccentsOnly)(null)).toBe('');
80
+ expect((0, normalize_1.removeAccentsOnly)(undefined)).toBe('');
81
+ });
82
+ it('should preserve case', () => {
83
+ expect((0, normalize_1.removeAccentsOnly)('HÉLLÔ')).toBe('HELLO');
84
+ });
85
+ });
86
+ describe('normalizeForSearchToken', () => {
87
+ it('should remove accents, trim, lowercase, and replace non-alphanumeric with spaces', () => {
88
+ expect((0, normalize_1.normalizeForSearchToken)(' HÉLLÔ-WÔRLD! ')).toBe('hello world ');
89
+ });
90
+ it('should handle empty string', () => {
91
+ expect((0, normalize_1.normalizeForSearchToken)('')).toBe('');
92
+ });
93
+ it('should handle null or undefined', () => {
94
+ expect((0, normalize_1.normalizeForSearchToken)(null)).toBe('');
95
+ expect((0, normalize_1.normalizeForSearchToken)(undefined)).toBe('');
96
+ });
97
+ it('should replace all special characters with spaces', () => {
98
+ expect((0, normalize_1.normalizeForSearchToken)('hello@world#123')).toBe('hello world 123');
99
+ });
100
+ });
101
+ });
package/lib/retries.d.ts CHANGED
@@ -1 +1,23 @@
1
+ /**
2
+ * Executes an asynchronous function with automatic retries on failure.
3
+ *
4
+ * This utility attempts to execute the provided function and automatically
5
+ * retries if it fails, with a specified delay between attempts. It will
6
+ * continue retrying until either the function succeeds or the maximum
7
+ * number of retries is reached.
8
+ *
9
+ * @template TFunc Type of the function to execute (must return a Promise)
10
+ * @param fn - The asynchronous function to execute
11
+ * @param retries - The maximum number of retry attempts after the initial attempt
12
+ * @param timeout - The delay in milliseconds between retry attempts
13
+ * @returns A promise that resolves with the result of the function or rejects with the last error
14
+ *
15
+ * @example
16
+ * // Retry an API call up to 3 times with 1 second between attempts
17
+ * const result = await executeWithRetries(
18
+ * () => fetchDataFromApi(),
19
+ * 3,
20
+ * 1000
21
+ * );
22
+ */
1
23
  export declare function executeWithRetries<TFunc extends () => Promise<any>>(fn: TFunc, retries: number, timeout: number): Promise<ReturnType<TFunc>>;
package/lib/retries.js CHANGED
@@ -1,6 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.executeWithRetries = void 0;
4
+ /**
5
+ * Executes an asynchronous function with automatic retries on failure.
6
+ *
7
+ * This utility attempts to execute the provided function and automatically
8
+ * retries if it fails, with a specified delay between attempts. It will
9
+ * continue retrying until either the function succeeds or the maximum
10
+ * number of retries is reached.
11
+ *
12
+ * @template TFunc Type of the function to execute (must return a Promise)
13
+ * @param fn - The asynchronous function to execute
14
+ * @param retries - The maximum number of retry attempts after the initial attempt
15
+ * @param timeout - The delay in milliseconds between retry attempts
16
+ * @returns A promise that resolves with the result of the function or rejects with the last error
17
+ *
18
+ * @example
19
+ * // Retry an API call up to 3 times with 1 second between attempts
20
+ * const result = await executeWithRetries(
21
+ * () => fetchDataFromApi(),
22
+ * 3,
23
+ * 1000
24
+ * );
25
+ */
4
26
  function executeWithRetries(fn, retries, timeout) {
5
27
  return new Promise((resolve, reject) => {
6
28
  const retry = async (retries) => {
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Generates an array of search tokens from input text and optional metadata.
3
+ *
4
+ * This function processes text by:
5
+ * 1. Converting it to an array of strings (if not already)
6
+ * 2. Filtering out falsy values
7
+ * 3. Normalizing each string (removing accents, special characters)
8
+ * 4. Splitting by spaces to create individual tokens
9
+ * 5. Optionally adding metadata tokens in the format "_key:value"
10
+ *
11
+ * @param text - String or array of strings to tokenize
12
+ * @param meta - Optional metadata object where each key-value pair becomes a token
13
+ * @returns Array of normalized search tokens
14
+ *
15
+ * @example
16
+ * // Returns ['hello', 'world']
17
+ * getSearchTokens('Hello, World!')
18
+ *
19
+ * @example
20
+ * // Returns ['hello', 'world', '_id:123']
21
+ * getSearchTokens('Hello, World!', { id: '123' })
22
+ */
23
+ export declare function getSearchTokens(text: string[] | string, meta?: Record<string, string>): string[];
24
+ /**
25
+ * Interface for parameters used in generating search queries from tokens.
26
+ *
27
+ * @property filter - Optional string to filter search results
28
+ * @property [key: string] - Additional key-value pairs for metadata filtering
29
+ */
30
+ export interface SearchQueryForTokensParams {
31
+ filter?: string;
32
+ [key: string]: string;
33
+ }
34
+ /**
35
+ * Options for customizing the search query generation behavior.
36
+ * Currently empty but provided for future extensibility.
37
+ */
38
+ export declare type SearchQueryForTokensOptions = {};
39
+ /**
40
+ * Generates a MongoDB-compatible query object based on the provided parameters.
41
+ *
42
+ * This function:
43
+ * 1. Processes any filter text into RegExp tokens for prefix matching
44
+ * 2. Adds metadata filters based on additional properties in the params object
45
+ * 3. Returns a query object with the $all operator for MongoDB queries
46
+ *
47
+ * @param params - Parameters for generating the search query
48
+ * @param _options - Options for customizing search behavior (reserved for future use)
49
+ * @returns A MongoDB-compatible query object with format { $all: [...tokens] }
50
+ *
51
+ * @example
52
+ * // Returns { $all: [/^hello/, /^world/] }
53
+ * getSearchQueryForTokens({ filter: 'Hello World' })
54
+ *
55
+ * @example
56
+ * // Returns { $all: [/^search/, '_category:books'] }
57
+ * getSearchQueryForTokens({ filter: 'search', category: 'books' })
58
+ */
59
+ export declare function getSearchQueryForTokens(params?: SearchQueryForTokensParams, _options?: SearchQueryForTokensOptions): {
60
+ $all: (string | RegExp)[];
61
+ };