@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,13 +1,39 @@
1
+ /**
2
+ * Interface representing the standardized error information structure for Orion errors.
3
+ * This is used by the getInfo method to provide consistent error reporting.
4
+ */
1
5
  export interface OrionErrorInformation {
6
+ /** The error code or identifier */
2
7
  error: string;
8
+ /** Human-readable error message */
3
9
  message: string;
10
+ /** Additional error metadata or context */
4
11
  extra: any;
12
+ /** The sub-type of error. For example for permissions errors it could be 'read', 'write', 'admin' */
13
+ type?: string;
5
14
  }
15
+ /**
16
+ * Base error class for all Orion-specific errors.
17
+ *
18
+ * This abstract class provides common properties and methods for all error types
19
+ * used in the Orion framework. It's extended by more specific error classes
20
+ * like UserError and PermissionsError.
21
+ *
22
+ * @property isOrionError - Flag indicating this is an Orion error (always true)
23
+ * @property isUserError - Flag indicating if this is a user-facing error
24
+ * @property isPermissionsError - Flag indicating if this is a permissions-related error
25
+ * @property code - Error code for identifying the error type
26
+ * @property extra - Additional error context or metadata
27
+ */
6
28
  export declare class OrionError extends Error {
7
29
  isOrionError: boolean;
8
30
  isUserError: boolean;
9
31
  isPermissionsError: boolean;
10
32
  code: string;
11
33
  extra: any;
34
+ /**
35
+ * Returns a standardized representation of the error information.
36
+ * @returns An object containing error details in a consistent format
37
+ */
12
38
  getInfo: () => OrionErrorInformation;
13
39
  }
@@ -1,6 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OrionError = void 0;
4
+ /**
5
+ * Base error class for all Orion-specific errors.
6
+ *
7
+ * This abstract class provides common properties and methods for all error types
8
+ * used in the Orion framework. It's extended by more specific error classes
9
+ * like UserError and PermissionsError.
10
+ *
11
+ * @property isOrionError - Flag indicating this is an Orion error (always true)
12
+ * @property isUserError - Flag indicating if this is a user-facing error
13
+ * @property isPermissionsError - Flag indicating if this is a permissions-related error
14
+ * @property code - Error code for identifying the error type
15
+ * @property extra - Additional error context or metadata
16
+ */
4
17
  class OrionError extends Error {
5
18
  constructor() {
6
19
  super(...arguments);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const OrionError_1 = require("./OrionError");
4
+ // Since OrionError is an abstract class with unimplemented methods,
5
+ // we'll create a concrete implementation for testing
6
+ class TestOrionError extends OrionError_1.OrionError {
7
+ constructor(message, code = 'test_error', extra = {}) {
8
+ super(message);
9
+ this.code = code;
10
+ this.extra = extra;
11
+ this.isUserError = false;
12
+ this.isPermissionsError = false;
13
+ this.getInfo = () => {
14
+ return {
15
+ error: this.code,
16
+ message: this.message,
17
+ extra: this.extra
18
+ };
19
+ };
20
+ }
21
+ }
22
+ describe('OrionError', () => {
23
+ it('should be an instance of Error', () => {
24
+ const error = new TestOrionError('Test error message');
25
+ expect(error).toBeInstanceOf(Error);
26
+ });
27
+ it('should have OrionError properties', () => {
28
+ const error = new TestOrionError('Test error message');
29
+ expect(error.isOrionError).toBe(true);
30
+ expect(error.isUserError).toBe(false);
31
+ expect(error.isPermissionsError).toBe(false);
32
+ expect(error.code).toBe('test_error');
33
+ expect(error.message).toBe('Test error message');
34
+ });
35
+ it('should have a getInfo method that returns the correct structure', () => {
36
+ const extraData = { userId: '123', context: 'testing' };
37
+ const error = new TestOrionError('Test error message', 'custom_code', extraData);
38
+ const info = error.getInfo();
39
+ expect(info).toEqual({
40
+ error: 'custom_code',
41
+ message: 'Test error message',
42
+ extra: extraData
43
+ });
44
+ });
45
+ it('should be extendable with custom properties', () => {
46
+ const error = new TestOrionError('Test error message');
47
+ error.customProp = 'custom value';
48
+ expect(error.customProp).toBe('custom value');
49
+ expect(error.message).toBe('Test error message');
50
+ expect(error.isOrionError).toBe(true);
51
+ });
52
+ });
@@ -1,4 +1,37 @@
1
1
  import { OrionError } from './OrionError';
2
+ /**
3
+ * Error class for permission-related errors in the Orion framework.
4
+ *
5
+ * PermissionsError represents authorization failures where a user or client
6
+ * attempts to perform an action they don't have permission to execute.
7
+ * This is used to distinguish security/permissions errors from other types
8
+ * of errors for proper error handling and user feedback.
9
+ *
10
+ * @extends OrionError
11
+ */
2
12
  export default class PermissionsError extends OrionError {
13
+ /**
14
+ * Creates a new PermissionsError instance.
15
+ *
16
+ * @param permissionErrorType - Identifies the specific permission that was violated
17
+ * (e.g., 'read', 'write', 'admin')
18
+ * @param extra - Additional error context or metadata. Can include a custom message
19
+ * via the message property.
20
+ *
21
+ * @example
22
+ * // Basic usage
23
+ * throw new PermissionsError('delete_document')
24
+ *
25
+ * @example
26
+ * // With custom message
27
+ * throw new PermissionsError('access_admin', { message: 'Admin access required' })
28
+ *
29
+ * @example
30
+ * // With additional context
31
+ * throw new PermissionsError('edit_user', {
32
+ * userId: 'user123',
33
+ * requiredRole: 'admin'
34
+ * })
35
+ */
3
36
  constructor(permissionErrorType: any, extra?: any);
4
37
  }
@@ -1,7 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const OrionError_1 = require("./OrionError");
4
+ /**
5
+ * Error class for permission-related errors in the Orion framework.
6
+ *
7
+ * PermissionsError represents authorization failures where a user or client
8
+ * attempts to perform an action they don't have permission to execute.
9
+ * This is used to distinguish security/permissions errors from other types
10
+ * of errors for proper error handling and user feedback.
11
+ *
12
+ * @extends OrionError
13
+ */
4
14
  class PermissionsError extends OrionError_1.OrionError {
15
+ /**
16
+ * Creates a new PermissionsError instance.
17
+ *
18
+ * @param permissionErrorType - Identifies the specific permission that was violated
19
+ * (e.g., 'read', 'write', 'admin')
20
+ * @param extra - Additional error context or metadata. Can include a custom message
21
+ * via the message property.
22
+ *
23
+ * @example
24
+ * // Basic usage
25
+ * throw new PermissionsError('delete_document')
26
+ *
27
+ * @example
28
+ * // With custom message
29
+ * throw new PermissionsError('access_admin', { message: 'Admin access required' })
30
+ *
31
+ * @example
32
+ * // With additional context
33
+ * throw new PermissionsError('edit_user', {
34
+ * userId: 'user123',
35
+ * requiredRole: 'admin'
36
+ * })
37
+ */
5
38
  constructor(permissionErrorType, extra = {}) {
6
39
  // Calling parent constructor of base Error class.
7
40
  const message = extra.message || `Client is not allowed to perform this action [${permissionErrorType}]`;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,62 @@
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 PermissionsError_1 = __importDefault(require("./PermissionsError"));
7
+ const OrionError_1 = require("./OrionError");
8
+ describe('PermissionsError', () => {
9
+ it('should extend OrionError', () => {
10
+ const error = new PermissionsError_1.default('read_document');
11
+ expect(error).toBeInstanceOf(OrionError_1.OrionError);
12
+ });
13
+ it('should set isPermissionsError to true', () => {
14
+ const error = new PermissionsError_1.default('read_document');
15
+ expect(error.isPermissionsError).toBe(true);
16
+ expect(error.isOrionError).toBe(true);
17
+ expect(error.isUserError).toBeUndefined();
18
+ });
19
+ it('should set code to "PermissionsError"', () => {
20
+ const error = new PermissionsError_1.default('read_document');
21
+ expect(error.code).toBe('PermissionsError');
22
+ });
23
+ it('should generate a default message with permissionErrorType', () => {
24
+ const error = new PermissionsError_1.default('update_user');
25
+ expect(error.message).toBe('Client is not allowed to perform this action [update_user]');
26
+ });
27
+ it('should support custom message in extra', () => {
28
+ const error = new PermissionsError_1.default('delete_document', {
29
+ message: 'You need admin rights to delete this document'
30
+ });
31
+ expect(error.message).toBe('You need admin rights to delete this document');
32
+ });
33
+ it('should store extra data', () => {
34
+ const extraData = {
35
+ documentId: '123',
36
+ requiredRole: 'admin'
37
+ };
38
+ const error = new PermissionsError_1.default('read_document', extraData);
39
+ expect(error.extra).toEqual(extraData);
40
+ });
41
+ it('should have a getInfo method that returns the correct structure', () => {
42
+ const extraData = { documentId: '123', requiredRole: 'admin' };
43
+ const error = new PermissionsError_1.default('read_document', extraData);
44
+ const info = error.getInfo();
45
+ expect(info).toEqual({
46
+ ...extraData,
47
+ error: 'PermissionsError',
48
+ message: 'Client is not allowed to perform this action [read_document]',
49
+ type: 'read_document'
50
+ });
51
+ });
52
+ it('should include the permission type in getInfo', () => {
53
+ const error = new PermissionsError_1.default('admin_action');
54
+ const info = error.getInfo();
55
+ expect(info.type).toBe('admin_action');
56
+ });
57
+ it('should have proper stack trace', () => {
58
+ const error = new PermissionsError_1.default('read_document');
59
+ expect(error.stack).toBeDefined();
60
+ expect(error.stack.includes('PermissionsError.test.ts')).toBe(true);
61
+ });
62
+ });
@@ -1,4 +1,33 @@
1
1
  import { OrionError } from './OrionError';
2
+ /**
3
+ * Error class for user-facing errors in the Orion framework.
4
+ *
5
+ * UserError is designed to represent errors that should be displayed to end users,
6
+ * as opposed to system errors or unexpected failures. These errors typically represent
7
+ * validation issues, business rule violations, or other expected error conditions.
8
+ *
9
+ * @extends OrionError
10
+ */
2
11
  export default class UserError extends OrionError {
12
+ /**
13
+ * Creates a new UserError instance.
14
+ *
15
+ * @param code - Error code identifier. If only one parameter is provided,
16
+ * this will be used as the message and code will default to 'error'.
17
+ * @param message - Human-readable error message. Optional if code is provided.
18
+ * @param extra - Additional error context or metadata.
19
+ *
20
+ * @example
21
+ * // Basic usage
22
+ * throw new UserError('invalid_input', 'The provided email is invalid')
23
+ *
24
+ * @example
25
+ * // Using only a message (code will be 'error')
26
+ * throw new UserError('Input validation failed')
27
+ *
28
+ * @example
29
+ * // With extra metadata
30
+ * throw new UserError('rate_limit', 'Too many requests', { maxRequests: 100 })
31
+ */
3
32
  constructor(code: string, message?: string, extra?: any);
4
33
  }
@@ -1,7 +1,36 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const OrionError_1 = require("./OrionError");
4
+ /**
5
+ * Error class for user-facing errors in the Orion framework.
6
+ *
7
+ * UserError is designed to represent errors that should be displayed to end users,
8
+ * as opposed to system errors or unexpected failures. These errors typically represent
9
+ * validation issues, business rule violations, or other expected error conditions.
10
+ *
11
+ * @extends OrionError
12
+ */
4
13
  class UserError extends OrionError_1.OrionError {
14
+ /**
15
+ * Creates a new UserError instance.
16
+ *
17
+ * @param code - Error code identifier. If only one parameter is provided,
18
+ * this will be used as the message and code will default to 'error'.
19
+ * @param message - Human-readable error message. Optional if code is provided.
20
+ * @param extra - Additional error context or metadata.
21
+ *
22
+ * @example
23
+ * // Basic usage
24
+ * throw new UserError('invalid_input', 'The provided email is invalid')
25
+ *
26
+ * @example
27
+ * // Using only a message (code will be 'error')
28
+ * throw new UserError('Input validation failed')
29
+ *
30
+ * @example
31
+ * // With extra metadata
32
+ * throw new UserError('rate_limit', 'Too many requests', { maxRequests: 100 })
33
+ */
5
34
  constructor(code, message, extra) {
6
35
  if (!message && code) {
7
36
  message = code;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,49 @@
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 UserError_1 = __importDefault(require("./UserError"));
7
+ const OrionError_1 = require("./OrionError");
8
+ describe('UserError', () => {
9
+ it('should extend OrionError', () => {
10
+ const error = new UserError_1.default('test_code', 'Test error message');
11
+ expect(error).toBeInstanceOf(OrionError_1.OrionError);
12
+ });
13
+ it('should set isUserError to true', () => {
14
+ const error = new UserError_1.default('test_code', 'Test error message');
15
+ expect(error.isUserError).toBe(true);
16
+ expect(error.isOrionError).toBe(true);
17
+ expect(error.isPermissionsError).toBeUndefined();
18
+ });
19
+ it('should set code and message correctly', () => {
20
+ const error = new UserError_1.default('test_code', 'Test error message');
21
+ expect(error.code).toBe('test_code');
22
+ expect(error.message).toBe('Test error message');
23
+ });
24
+ it('should support providing only a message (code defaults to "error")', () => {
25
+ const error = new UserError_1.default('Test error message');
26
+ expect(error.code).toBe('error');
27
+ expect(error.message).toBe('Test error message');
28
+ });
29
+ it('should support extra data', () => {
30
+ const extraData = { field: 'username', constraint: 'required' };
31
+ const error = new UserError_1.default('validation_error', 'Validation failed', extraData);
32
+ expect(error.extra).toEqual(extraData);
33
+ });
34
+ it('should have a getInfo method that returns the correct structure', () => {
35
+ const extraData = { field: 'email', constraint: 'format' };
36
+ const error = new UserError_1.default('invalid_email', 'Invalid email format', extraData);
37
+ const info = error.getInfo();
38
+ expect(info).toEqual({
39
+ error: 'invalid_email',
40
+ message: 'Invalid email format',
41
+ extra: extraData
42
+ });
43
+ });
44
+ it('should have proper stack trace', () => {
45
+ const error = new UserError_1.default('test_code', 'Test error message');
46
+ expect(error.stack).toBeDefined();
47
+ expect(error.stack.includes('UserError.test.ts')).toBe(true);
48
+ });
49
+ });
@@ -0,0 +1,43 @@
1
+ /**
2
+ * @file Exports all error classes used in the Orion framework
3
+ */
4
+ import { OrionError, OrionErrorInformation } from './OrionError';
5
+ import PermissionsError from './PermissionsError';
6
+ import UserError from './UserError';
7
+ /**
8
+ * Re-export all error types for convenient importing
9
+ */
10
+ export { OrionError, OrionErrorInformation, PermissionsError, UserError };
11
+ /**
12
+ * Type guard to check if an error is an OrionError
13
+ *
14
+ * @param error - Any error object to test
15
+ * @returns True if the error is an OrionError instance
16
+ *
17
+ * @example
18
+ * try {
19
+ * // some code that might throw
20
+ * } catch (error) {
21
+ * if (isOrionError(error)) {
22
+ * // Handle Orion-specific error
23
+ * console.log(error.code, error.getInfo())
24
+ * } else {
25
+ * // Handle general error
26
+ * }
27
+ * }
28
+ */
29
+ export declare function isOrionError(error: any): error is OrionError;
30
+ /**
31
+ * Type guard to check if an error is a UserError
32
+ *
33
+ * @param error - Any error object to test
34
+ * @returns True if the error is a UserError instance
35
+ */
36
+ export declare function isUserError(error: any): error is UserError;
37
+ /**
38
+ * Type guard to check if an error is a PermissionsError
39
+ *
40
+ * @param error - Any error object to test
41
+ * @returns True if the error is a PermissionsError instance
42
+ */
43
+ export declare function isPermissionsError(error: any): error is PermissionsError;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ /**
3
+ * @file Exports all error classes used in the Orion framework
4
+ */
5
+ var __importDefault = (this && this.__importDefault) || function (mod) {
6
+ return (mod && mod.__esModule) ? mod : { "default": mod };
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.isPermissionsError = exports.isUserError = exports.isOrionError = exports.UserError = exports.PermissionsError = exports.OrionError = void 0;
10
+ const OrionError_1 = require("./OrionError");
11
+ Object.defineProperty(exports, "OrionError", { enumerable: true, get: function () { return OrionError_1.OrionError; } });
12
+ const PermissionsError_1 = __importDefault(require("./PermissionsError"));
13
+ exports.PermissionsError = PermissionsError_1.default;
14
+ const UserError_1 = __importDefault(require("./UserError"));
15
+ exports.UserError = UserError_1.default;
16
+ /**
17
+ * Type guard to check if an error is an OrionError
18
+ *
19
+ * @param error - Any error object to test
20
+ * @returns True if the error is an OrionError instance
21
+ *
22
+ * @example
23
+ * try {
24
+ * // some code that might throw
25
+ * } catch (error) {
26
+ * if (isOrionError(error)) {
27
+ * // Handle Orion-specific error
28
+ * console.log(error.code, error.getInfo())
29
+ * } else {
30
+ * // Handle general error
31
+ * }
32
+ * }
33
+ */
34
+ function isOrionError(error) {
35
+ return Boolean(error && typeof error === 'object' && error.isOrionError === true);
36
+ }
37
+ exports.isOrionError = isOrionError;
38
+ /**
39
+ * Type guard to check if an error is a UserError
40
+ *
41
+ * @param error - Any error object to test
42
+ * @returns True if the error is a UserError instance
43
+ */
44
+ function isUserError(error) {
45
+ return Boolean(error && typeof error === 'object' && error.isOrionError === true && error.isUserError === true);
46
+ }
47
+ exports.isUserError = isUserError;
48
+ /**
49
+ * Type guard to check if an error is a PermissionsError
50
+ *
51
+ * @param error - Any error object to test
52
+ * @returns True if the error is a PermissionsError instance
53
+ */
54
+ function isPermissionsError(error) {
55
+ return Boolean(error && typeof error === 'object' && error.isOrionError === true && error.isPermissionsError === true);
56
+ }
57
+ exports.isPermissionsError = isPermissionsError;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const index_1 = require("./index");
4
+ describe('Error type guards', () => {
5
+ // Mock concrete implementation of OrionError for testing
6
+ class TestOrionError extends index_1.OrionError {
7
+ constructor() {
8
+ super('Test error');
9
+ this.code = 'test_error';
10
+ this.extra = {};
11
+ this.isOrionError = true;
12
+ this.isUserError = false;
13
+ this.isPermissionsError = false;
14
+ this.getInfo = () => ({
15
+ error: this.code,
16
+ message: this.message,
17
+ extra: this.extra
18
+ });
19
+ }
20
+ }
21
+ describe('isOrionError', () => {
22
+ it('should return true for OrionError instances', () => {
23
+ expect((0, index_1.isOrionError)(new TestOrionError())).toBe(true);
24
+ expect((0, index_1.isOrionError)(new index_1.UserError('test'))).toBe(true);
25
+ expect((0, index_1.isOrionError)(new index_1.PermissionsError('test'))).toBe(true);
26
+ });
27
+ it('should return false for non-OrionError objects', () => {
28
+ expect((0, index_1.isOrionError)(new Error('Regular error'))).toBe(false);
29
+ expect((0, index_1.isOrionError)({ message: 'Not an error' })).toBe(false);
30
+ expect((0, index_1.isOrionError)(null)).toBe(false);
31
+ expect((0, index_1.isOrionError)(undefined)).toBe(false);
32
+ });
33
+ });
34
+ describe('isUserError', () => {
35
+ it('should return true for UserError instances', () => {
36
+ expect((0, index_1.isUserError)(new index_1.UserError('test'))).toBe(true);
37
+ });
38
+ it('should return false for other error types', () => {
39
+ expect((0, index_1.isUserError)(new TestOrionError())).toBe(false);
40
+ expect((0, index_1.isUserError)(new index_1.PermissionsError('test'))).toBe(false);
41
+ expect((0, index_1.isUserError)(new Error('Regular error'))).toBe(false);
42
+ expect((0, index_1.isUserError)(null)).toBe(false);
43
+ });
44
+ });
45
+ describe('isPermissionsError', () => {
46
+ it('should return true for PermissionsError instances', () => {
47
+ expect((0, index_1.isPermissionsError)(new index_1.PermissionsError('test'))).toBe(true);
48
+ });
49
+ it('should return false for other error types', () => {
50
+ expect((0, index_1.isPermissionsError)(new TestOrionError())).toBe(false);
51
+ expect((0, index_1.isPermissionsError)(new index_1.UserError('test'))).toBe(false);
52
+ expect((0, index_1.isPermissionsError)(new Error('Regular error'))).toBe(false);
53
+ expect((0, index_1.isPermissionsError)(null)).toBe(false);
54
+ });
55
+ });
56
+ });
@@ -1 +1,17 @@
1
+ /**
2
+ * Creates a map (object) 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 item in the array becomes a value in the map, indexed by the specified property.
6
+ * If multiple items have the same key value, only the last one will be preserved.
7
+ *
8
+ * @template T The type of items in the input array
9
+ * @param array - The input array of items to transform into a 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 the original items
12
+ *
13
+ * @example
14
+ * // Returns { '1': { id: 1, name: 'Item 1' }, '2': { id: 2, name: 'Item 2' } }
15
+ * createMap([{ id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }], 'id')
16
+ */
1
17
  export default function createMap<T>(array: Array<T>, key?: string): Record<string, T>;
package/lib/createMap.js CHANGED
@@ -1,5 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Creates a map (object) 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 item in the array becomes a value in the map, indexed by the specified property.
8
+ * If multiple items have the same key value, only the last one will be preserved.
9
+ *
10
+ * @template T The type of items in the input array
11
+ * @param array - The input array of items to transform into a 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 the original items
14
+ *
15
+ * @example
16
+ * // Returns { '1': { id: 1, name: 'Item 1' }, '2': { id: 2, name: 'Item 2' } }
17
+ * createMap([{ id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }], 'id')
18
+ */
3
19
  function createMap(array, key = '_id') {
4
20
  const map = {};
5
21
  for (const item of array) {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,74 @@
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 createMap_1 = __importDefault(require("./createMap"));
7
+ describe('createMap', () => {
8
+ it('should create a map using the default _id key', () => {
9
+ const array = [
10
+ { _id: '1', name: 'Item 1' },
11
+ { _id: '2', name: 'Item 2' },
12
+ { _id: '3', name: 'Item 3' }
13
+ ];
14
+ const result = (0, createMap_1.default)(array);
15
+ expect(result).toEqual({
16
+ '1': { _id: '1', name: 'Item 1' },
17
+ '2': { _id: '2', name: 'Item 2' },
18
+ '3': { _id: '3', name: 'Item 3' }
19
+ });
20
+ });
21
+ it('should create a map using a custom key', () => {
22
+ const array = [
23
+ { id: 'a', name: 'Item A' },
24
+ { id: 'b', name: 'Item B' },
25
+ { id: 'c', name: 'Item C' }
26
+ ];
27
+ const result = (0, createMap_1.default)(array, 'id');
28
+ expect(result).toEqual({
29
+ 'a': { id: 'a', name: 'Item A' },
30
+ 'b': { id: 'b', name: 'Item B' },
31
+ 'c': { id: 'c', name: 'Item C' }
32
+ });
33
+ });
34
+ it('should handle empty arrays', () => {
35
+ const result = (0, createMap_1.default)([]);
36
+ expect(result).toEqual({});
37
+ });
38
+ it('should handle objects with property references', () => {
39
+ const array = [
40
+ { id: 'u1', message: 'Hello' },
41
+ { id: 'u2', message: 'World' }
42
+ ];
43
+ const result = (0, createMap_1.default)(array, 'id');
44
+ expect(result).toEqual({
45
+ 'u1': { id: 'u1', message: 'Hello' },
46
+ 'u2': { id: 'u2', message: 'World' }
47
+ });
48
+ });
49
+ it('should overwrite items with duplicate keys', () => {
50
+ const array = [
51
+ { id: 'dup', value: 'First' },
52
+ { id: 'dup', value: 'Second' },
53
+ { id: 'dup', value: 'Third' }
54
+ ];
55
+ const result = (0, createMap_1.default)(array, 'id');
56
+ // Only the last item with a given key should be preserved
57
+ expect(result).toEqual({
58
+ 'dup': { id: 'dup', value: 'Third' }
59
+ });
60
+ });
61
+ it('should handle various key types', () => {
62
+ const array = [
63
+ { key: 1, value: 'Number key' },
64
+ { key: 'string', value: 'String key' },
65
+ { key: true, value: 'Boolean key' }
66
+ ];
67
+ const result = (0, createMap_1.default)(array, 'key');
68
+ expect(result).toEqual({
69
+ '1': { key: 1, value: 'Number key' },
70
+ 'string': { key: 'string', value: 'String key' },
71
+ 'true': { key: true, value: 'Boolean key' }
72
+ });
73
+ });
74
+ });