@orion-js/helpers 3.11.8 → 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
@@ -0,0 +1,78 @@
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;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,101 @@
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
+ });
@@ -0,0 +1 @@
1
+ export declare function shortenMongoId(string: string): string;
@@ -0,0 +1,10 @@
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/helpers",
3
- "version": "3.11.8",
3
+ "version": "3.11.15",
4
4
  "main": "lib/index.js",
5
5
  "author": "nicolaslopezj",
6
6
  "license": "MIT",
@@ -25,5 +25,5 @@
25
25
  "publishConfig": {
26
26
  "access": "public"
27
27
  },
28
- "gitHead": "78d70ac414964ec657cc000cedfad8a5efff5cc3"
28
+ "gitHead": "a5c95d9fd18f779c834644eb757928cdb840643c"
29
29
  }