@orion-js/helpers 3.11.15 → 4.0.0-alpha.3
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/dist/index.cjs +1018 -0
- package/dist/index.d.ts +362 -0
- package/dist/index.js +992 -0
- package/package.json +21 -15
- package/LICENSE +0 -21
- package/jest.config.js +0 -8
- package/lib/Errors/OrionError.d.ts +0 -39
- package/lib/Errors/OrionError.js +0 -23
- package/lib/Errors/OrionError.test.d.ts +0 -1
- package/lib/Errors/OrionError.test.js +0 -52
- package/lib/Errors/PermissionsError.d.ts +0 -37
- package/lib/Errors/PermissionsError.js +0 -57
- package/lib/Errors/PermissionsError.test.d.ts +0 -1
- package/lib/Errors/PermissionsError.test.js +0 -62
- package/lib/Errors/UserError.d.ts +0 -33
- package/lib/Errors/UserError.js +0 -54
- package/lib/Errors/UserError.test.d.ts +0 -1
- package/lib/Errors/UserError.test.js +0 -49
- package/lib/Errors/index.d.ts +0 -43
- package/lib/Errors/index.js +0 -57
- package/lib/Errors/index.test.d.ts +0 -1
- package/lib/Errors/index.test.js +0 -56
- package/lib/composeMiddlewares.d.ts +0 -6
- package/lib/composeMiddlewares.js +0 -44
- package/lib/createMap.d.ts +0 -17
- package/lib/createMap.js +0 -26
- package/lib/createMap.test.d.ts +0 -1
- package/lib/createMap.test.js +0 -74
- package/lib/createMapArray.d.ts +0 -22
- package/lib/createMapArray.js +0 -32
- package/lib/createMapArray.test.d.ts +0 -1
- package/lib/createMapArray.test.js +0 -101
- package/lib/generateId.d.ts +0 -6
- package/lib/generateId.js +0 -54
- package/lib/generateId.test.d.ts +0 -1
- package/lib/generateId.test.js +0 -11
- package/lib/generateUUID.d.ts +0 -2
- package/lib/generateUUID.js +0 -12
- package/lib/generateUUID.test.d.ts +0 -1
- package/lib/generateUUID.test.js +0 -15
- package/lib/hashObject.d.ts +0 -1
- package/lib/hashObject.js +0 -10
- package/lib/hashObject.test.d.ts +0 -1
- package/lib/hashObject.test.js +0 -30
- package/lib/index.d.ts +0 -13
- package/lib/index.js +0 -40
- package/lib/normalize.d.ts +0 -70
- package/lib/normalize.js +0 -111
- package/lib/normalize.test.d.ts +0 -1
- package/lib/normalize.test.js +0 -101
- package/lib/retries.d.ts +0 -23
- package/lib/retries.js +0 -45
- package/lib/retries.test.d.ts +0 -1
- package/lib/retries.test.js +0 -47
- package/lib/searchTokens.d.ts +0 -61
- package/lib/searchTokens.js +0 -78
- package/lib/searchTokens.test.d.ts +0 -1
- package/lib/searchTokens.test.js +0 -101
- package/lib/shortenMongoId.d.ts +0 -1
- package/lib/shortenMongoId.js +0 -10
- package/lib/sleep.d.ts +0 -5
- package/lib/sleep.js +0 -8
- package/tsconfig.json +0 -16
- package/yarn-error.log +0 -710
package/lib/normalize.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
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;
|
package/lib/normalize.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/normalize.test.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
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
|
-
*/
|
|
23
|
-
export declare function executeWithRetries<TFunc extends () => Promise<any>>(fn: TFunc, retries: number, timeout: number): Promise<ReturnType<TFunc>>;
|
package/lib/retries.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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
|
-
*/
|
|
26
|
-
function executeWithRetries(fn, retries, timeout) {
|
|
27
|
-
return new Promise((resolve, reject) => {
|
|
28
|
-
const retry = async (retries) => {
|
|
29
|
-
try {
|
|
30
|
-
const result = await fn();
|
|
31
|
-
resolve(result);
|
|
32
|
-
}
|
|
33
|
-
catch (error) {
|
|
34
|
-
if (retries > 0) {
|
|
35
|
-
setTimeout(() => retry(retries - 1), timeout);
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
reject(error);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
retry(retries);
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
exports.executeWithRetries = executeWithRetries;
|
package/lib/retries.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/retries.test.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const retries_1 = require("./retries");
|
|
4
|
-
describe('Retry helpers', () => {
|
|
5
|
-
it('should retry a function', async () => {
|
|
6
|
-
let counter = 0;
|
|
7
|
-
const fn = async () => {
|
|
8
|
-
counter++;
|
|
9
|
-
if (counter < 3) {
|
|
10
|
-
throw new Error('error');
|
|
11
|
-
}
|
|
12
|
-
return 'ok';
|
|
13
|
-
};
|
|
14
|
-
const result = await (0, retries_1.executeWithRetries)(fn, 3, 0);
|
|
15
|
-
expect(result).toEqual('ok');
|
|
16
|
-
});
|
|
17
|
-
it('should the throw the error if the retries are over', async () => {
|
|
18
|
-
let counter = 0;
|
|
19
|
-
const fn = async () => {
|
|
20
|
-
counter++;
|
|
21
|
-
throw new Error('error');
|
|
22
|
-
};
|
|
23
|
-
expect.assertions(2);
|
|
24
|
-
try {
|
|
25
|
-
await (0, retries_1.executeWithRetries)(fn, 3, 0);
|
|
26
|
-
}
|
|
27
|
-
catch (error) {
|
|
28
|
-
expect(error.message).toEqual('error');
|
|
29
|
-
expect(counter).toEqual(4);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
it('should run only once a function with 0 or undefined retrires', async () => {
|
|
33
|
-
let counter = 0;
|
|
34
|
-
const fn = async () => {
|
|
35
|
-
counter++;
|
|
36
|
-
throw new Error('error');
|
|
37
|
-
};
|
|
38
|
-
expect.assertions(2);
|
|
39
|
-
try {
|
|
40
|
-
await (0, retries_1.executeWithRetries)(fn, null, 0);
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
expect(error.message).toEqual('error');
|
|
44
|
-
expect(counter).toEqual(1);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
});
|
package/lib/searchTokens.d.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
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
|
-
};
|
package/lib/searchTokens.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSearchQueryForTokens = exports.getSearchTokens = void 0;
|
|
4
|
-
const normalize_1 = require("./normalize");
|
|
5
|
-
/**
|
|
6
|
-
* Generates an array of search tokens from input text and optional metadata.
|
|
7
|
-
*
|
|
8
|
-
* This function processes text by:
|
|
9
|
-
* 1. Converting it to an array of strings (if not already)
|
|
10
|
-
* 2. Filtering out falsy values
|
|
11
|
-
* 3. Normalizing each string (removing accents, special characters)
|
|
12
|
-
* 4. Splitting by spaces to create individual tokens
|
|
13
|
-
* 5. Optionally adding metadata tokens in the format "_key:value"
|
|
14
|
-
*
|
|
15
|
-
* @param text - String or array of strings to tokenize
|
|
16
|
-
* @param meta - Optional metadata object where each key-value pair becomes a token
|
|
17
|
-
* @returns Array of normalized search tokens
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* // Returns ['hello', 'world']
|
|
21
|
-
* getSearchTokens('Hello, World!')
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* // Returns ['hello', 'world', '_id:123']
|
|
25
|
-
* getSearchTokens('Hello, World!', { id: '123' })
|
|
26
|
-
*/
|
|
27
|
-
function getSearchTokens(text, meta) {
|
|
28
|
-
const stringArray = Array.isArray(text) ? text : [text];
|
|
29
|
-
const tokens = stringArray
|
|
30
|
-
.filter(Boolean)
|
|
31
|
-
.map(text => String(text))
|
|
32
|
-
.flatMap(word => {
|
|
33
|
-
return (0, normalize_1.normalizeForSearchToken)(word).split(' ').filter(Boolean);
|
|
34
|
-
});
|
|
35
|
-
if (meta) {
|
|
36
|
-
for (const key in meta) {
|
|
37
|
-
tokens.push(`_${key}:${meta[key]}`);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return tokens;
|
|
41
|
-
}
|
|
42
|
-
exports.getSearchTokens = getSearchTokens;
|
|
43
|
-
/**
|
|
44
|
-
* Generates a MongoDB-compatible query object based on the provided parameters.
|
|
45
|
-
*
|
|
46
|
-
* This function:
|
|
47
|
-
* 1. Processes any filter text into RegExp tokens for prefix matching
|
|
48
|
-
* 2. Adds metadata filters based on additional properties in the params object
|
|
49
|
-
* 3. Returns a query object with the $all operator for MongoDB queries
|
|
50
|
-
*
|
|
51
|
-
* @param params - Parameters for generating the search query
|
|
52
|
-
* @param _options - Options for customizing search behavior (reserved for future use)
|
|
53
|
-
* @returns A MongoDB-compatible query object with format { $all: [...tokens] }
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* // Returns { $all: [/^hello/, /^world/] }
|
|
57
|
-
* getSearchQueryForTokens({ filter: 'Hello World' })
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* // Returns { $all: [/^search/, '_category:books'] }
|
|
61
|
-
* getSearchQueryForTokens({ filter: 'search', category: 'books' })
|
|
62
|
-
*/
|
|
63
|
-
function getSearchQueryForTokens(params = {}, _options = {}) {
|
|
64
|
-
const searchTokens = [];
|
|
65
|
-
if (params.filter) {
|
|
66
|
-
const filterTokens = getSearchTokens(params.filter).map(token => new RegExp(`^${token}`));
|
|
67
|
-
searchTokens.push(...filterTokens);
|
|
68
|
-
}
|
|
69
|
-
for (const key in params) {
|
|
70
|
-
if (key === 'filter')
|
|
71
|
-
continue;
|
|
72
|
-
if (!params[key])
|
|
73
|
-
continue;
|
|
74
|
-
searchTokens.push(`_${key}:${params[key]}`);
|
|
75
|
-
}
|
|
76
|
-
return { $all: searchTokens };
|
|
77
|
-
}
|
|
78
|
-
exports.getSearchQueryForTokens = getSearchQueryForTokens;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/searchTokens.test.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const searchTokens_1 = require("./searchTokens");
|
|
4
|
-
describe('Search Tokens Utilities', () => {
|
|
5
|
-
describe('getSearchTokens', () => {
|
|
6
|
-
it('should convert a simple string to tokens', () => {
|
|
7
|
-
const result = (0, searchTokens_1.getSearchTokens)('Hello World');
|
|
8
|
-
expect(result).toEqual(['hello', 'world']);
|
|
9
|
-
});
|
|
10
|
-
it('should handle empty or falsy values', () => {
|
|
11
|
-
expect((0, searchTokens_1.getSearchTokens)('')).toEqual([]);
|
|
12
|
-
expect((0, searchTokens_1.getSearchTokens)(null)).toEqual([]);
|
|
13
|
-
expect((0, searchTokens_1.getSearchTokens)(undefined)).toEqual([]);
|
|
14
|
-
});
|
|
15
|
-
it('should normalize and split strings with special characters', () => {
|
|
16
|
-
const result = (0, searchTokens_1.getSearchTokens)('Héllô-Wôrld! 123');
|
|
17
|
-
expect(result).toEqual(['hello', 'world', '123']);
|
|
18
|
-
});
|
|
19
|
-
it('should process an array of strings', () => {
|
|
20
|
-
const result = (0, searchTokens_1.getSearchTokens)(['Hello', 'World']);
|
|
21
|
-
expect(result).toEqual(['hello', 'world']);
|
|
22
|
-
});
|
|
23
|
-
it('should filter out falsy values in arrays', () => {
|
|
24
|
-
const result = (0, searchTokens_1.getSearchTokens)(['Hello', '', null, undefined, 'World']);
|
|
25
|
-
expect(result).toEqual(['hello', 'world']);
|
|
26
|
-
});
|
|
27
|
-
it('should add metadata tokens when provided', () => {
|
|
28
|
-
const result = (0, searchTokens_1.getSearchTokens)('Hello World', { id: '123', category: 'test' });
|
|
29
|
-
expect(result).toEqual(['hello', 'world', '_id:123', '_category:test']);
|
|
30
|
-
});
|
|
31
|
-
it('should handle strings with multiple spaces correctly', () => {
|
|
32
|
-
const result = (0, searchTokens_1.getSearchTokens)(' Hello World ');
|
|
33
|
-
expect(result).toEqual(['hello', 'world']);
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
describe('getSearchQueryForTokens', () => {
|
|
37
|
-
it('should return empty query when no params provided', () => {
|
|
38
|
-
const result = (0, searchTokens_1.getSearchQueryForTokens)();
|
|
39
|
-
expect(result).toEqual({ $all: [] });
|
|
40
|
-
});
|
|
41
|
-
it('should convert filter string to RegExp tokens', () => {
|
|
42
|
-
const result = (0, searchTokens_1.getSearchQueryForTokens)({ filter: 'hello world' });
|
|
43
|
-
expect(result.$all.length).toBe(2);
|
|
44
|
-
expect(result.$all[0]).toBeInstanceOf(RegExp);
|
|
45
|
-
expect(result.$all[1]).toBeInstanceOf(RegExp);
|
|
46
|
-
// Test RegExp patterns
|
|
47
|
-
expect(result.$all[0].source).toBe('^hello');
|
|
48
|
-
expect(result.$all[1].source).toBe('^world');
|
|
49
|
-
});
|
|
50
|
-
it('should ignore empty filter', () => {
|
|
51
|
-
const result = (0, searchTokens_1.getSearchQueryForTokens)({ filter: '' });
|
|
52
|
-
expect(result).toEqual({ $all: [] });
|
|
53
|
-
});
|
|
54
|
-
it('should add metadata tokens for additional parameters', () => {
|
|
55
|
-
const result = (0, searchTokens_1.getSearchQueryForTokens)({
|
|
56
|
-
filter: 'search',
|
|
57
|
-
category: 'books',
|
|
58
|
-
id: '123'
|
|
59
|
-
});
|
|
60
|
-
// Should have one RegExp token from filter and two metadata tokens
|
|
61
|
-
expect(result.$all.length).toBe(3);
|
|
62
|
-
expect(result.$all[0]).toBeInstanceOf(RegExp);
|
|
63
|
-
expect(result.$all[1]).toBe('_category:books');
|
|
64
|
-
expect(result.$all[2]).toBe('_id:123');
|
|
65
|
-
});
|
|
66
|
-
it('should ignore empty metadata values', () => {
|
|
67
|
-
const result = (0, searchTokens_1.getSearchQueryForTokens)({
|
|
68
|
-
category: 'books',
|
|
69
|
-
author: '',
|
|
70
|
-
id: '123'
|
|
71
|
-
});
|
|
72
|
-
expect(result.$all.length).toBe(2);
|
|
73
|
-
expect(result.$all.includes('_category:books')).toBe(true);
|
|
74
|
-
expect(result.$all.includes('_id:123')).toBe(true);
|
|
75
|
-
expect(result.$all.includes('_author:')).toBe(false);
|
|
76
|
-
});
|
|
77
|
-
it('should handle complex filter with metadata', () => {
|
|
78
|
-
const result = (0, searchTokens_1.getSearchQueryForTokens)({
|
|
79
|
-
filter: 'Héllô Wôrld!',
|
|
80
|
-
status: 'active'
|
|
81
|
-
});
|
|
82
|
-
expect(result.$all.length).toBe(3);
|
|
83
|
-
// Check RegExp patterns
|
|
84
|
-
expect(result.$all[0].source).toBe('^hello');
|
|
85
|
-
expect(result.$all[1].source).toBe('^world');
|
|
86
|
-
// Check metadata token
|
|
87
|
-
expect(result.$all[2]).toBe('_status:active');
|
|
88
|
-
});
|
|
89
|
-
it('should maintain correct order with filter and metadata', () => {
|
|
90
|
-
const result = (0, searchTokens_1.getSearchQueryForTokens)({
|
|
91
|
-
filter: 'search term',
|
|
92
|
-
category: 'books'
|
|
93
|
-
});
|
|
94
|
-
// Filter tokens should come first, then metadata
|
|
95
|
-
expect(result.$all.length).toBe(3);
|
|
96
|
-
expect(result.$all[0]).toBeInstanceOf(RegExp);
|
|
97
|
-
expect(result.$all[1]).toBeInstanceOf(RegExp);
|
|
98
|
-
expect(result.$all[2]).toBe('_category:books');
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
});
|
package/lib/shortenMongoId.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function shortenMongoId(string: string): string;
|
package/lib/shortenMongoId.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.shortenMongoId = void 0;
|
|
4
|
-
function lastOfString(string, last) {
|
|
5
|
-
return string.substring(string.length - last, string.length);
|
|
6
|
-
}
|
|
7
|
-
function shortenMongoId(string) {
|
|
8
|
-
return lastOfString(string, 5).toUpperCase();
|
|
9
|
-
}
|
|
10
|
-
exports.shortenMongoId = shortenMongoId;
|
package/lib/sleep.d.ts
DELETED
package/lib/sleep.js
DELETED
package/tsconfig.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"strict": false,
|
|
4
|
-
"outDir": "./lib",
|
|
5
|
-
"rootDir": "./src",
|
|
6
|
-
"module": "commonjs",
|
|
7
|
-
"target": "es2020",
|
|
8
|
-
"moduleResolution": "node",
|
|
9
|
-
"lib": ["es2020"],
|
|
10
|
-
"declaration": true,
|
|
11
|
-
|
|
12
|
-
"esModuleInterop": true,
|
|
13
|
-
"skipLibCheck": true,
|
|
14
|
-
"forceConsistentCasingInFileNames": true
|
|
15
|
-
}
|
|
16
|
-
}
|