UNPKG

@gsb-core/core

Version:

GSB core services and classes for platform-independent web applications

246 lines 8.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GsbUtils = exports.GSB_DATE_FIELDS = void 0; exports.getGsbDateSortCols = getGsbDateSortCols; exports.compareObjects = compareObjects; /** * Utility functions for working with GSB entities */ /** * Standard GSB date field names */ exports.GSB_DATE_FIELDS = { CREATE_DATE: 'createDate', LAST_UPDATE_DATE: 'lastUpdateDate', CREATED_BY_ID: 'createdBy_id', LAST_UPDATED_BY_ID: 'lastUpdatedBy_id' }; /** * Gets the correct sorting configuration for GSB entities * @param sortByCreateDate Whether to sort by creation date (true) or last update date (false) * @param ascending Whether to sort in ascending order * @returns The sort columns configuration for GSB queries */ function getGsbDateSortCols(sortByCreateDate = false, ascending = false) { return [ { col: { name: sortByCreateDate ? exports.GSB_DATE_FIELDS.CREATE_DATE : exports.GSB_DATE_FIELDS.LAST_UPDATE_DATE }, sortType: ascending ? 'ASC' : 'DESC' } ]; } /** * Compares two objects for equality, ignoring specified properties * @param obj1 First object to compare * @param obj2 Second object to compare * @param ignoredProps Array of property names to ignore during comparison * @returns True if objects are functionally equal (ignoring specified properties) */ function compareObjects(obj1, obj2, ignoredProps = []) { if (obj1 === obj2) return true; if (!obj1 || !obj2) return false; // Get all unique keys from both objects const allKeys = [...new Set([...Object.keys(obj1), ...Object.keys(obj2)])]; for (const key of allKeys) { // Skip ignored properties if (ignoredProps.includes(key)) continue; // Skip if both values are undefined if (obj1[key] === undefined && obj2[key] === undefined) continue; // Check if one has the property and the other doesn't if ((obj1[key] === undefined) !== (obj2[key] === undefined)) return false; // Check for nested objects (not including Arrays) if (typeof obj1[key] === 'object' && typeof obj2[key] === 'object' && obj1[key] !== null && obj2[key] !== null && !Array.isArray(obj1[key]) && !Array.isArray(obj2[key])) { if (!compareObjects(obj1[key], obj2[key], ignoredProps)) return false; continue; } // Check for array equality if (Array.isArray(obj1[key]) && Array.isArray(obj2[key])) { if (obj1[key].length !== obj2[key].length) return false; // For simple arrays of primitives if (obj1[key].every((item) => typeof item !== 'object' || item === null) && obj2[key].every((item) => typeof item !== 'object' || item === null)) { // Sort and join for comparison if array contains only primitives const sorted1 = [...obj1[key]].sort().join(','); const sorted2 = [...obj2[key]].sort().join(','); if (sorted1 !== sorted2) return false; } else { // For arrays of objects, we need to compare each item // This is a simplification; for complex nested arrays a more sophisticated // comparison would be needed for (let i = 0; i < obj1[key].length; i++) { if (!compareObjects(obj1[key][i], obj2[key][i], ignoredProps)) return false; } } continue; } // Handle dates if (obj1[key] instanceof Date && obj2[key] instanceof Date) { if (obj1[key].getTime() !== obj2[key].getTime()) return false; continue; } // Simple value comparison if (obj1[key] !== obj2[key]) return false; } return true; } /** * Utility class for GSB operations */ class GsbUtils { static newId() { return crypto.randomUUID(); } /** * Checks if an ID is empty (null, undefined, or empty GUID) * @param id The ID to check * @returns true if the ID is empty, false otherwise */ static isIdEmpty(id) { if (!id) return true; // Check for empty GUID (all zeros) const emptyGuidRegex = /^[0-]{36}$/; return emptyGuidRegex.test(id); } /** * Checks if an ID is valid (not empty and not an empty GUID) * @param id The ID to check * @returns true if the ID is valid, false otherwise */ static isValidId(id) { return !this.isIdEmpty(id); } /** * Checks if an array of IDs contains any empty IDs * @param ids Array of IDs to check * @returns true if any ID is empty, false otherwise */ static hasEmptyIds(ids) { return ids.some(id => this.isIdEmpty(id)); } /** * Filters out empty IDs from an array * @param ids Array of IDs to filter * @returns Array of valid IDs */ static filterEmptyIds(ids) { return ids.filter(id => this.isValidId(id)); } /** * Gets the view form mode from the given property * @param prop The property to get the view form mode from * @returns The view form mode or undefined if not found */ static getViewFormMode(prop) { if (!prop) return undefined; if (prop.viewFormMode) { return prop.viewFormMode; } if (prop.formModes) { return (prop.formModes >> 8) & 0xFF; } return undefined; } /** * Gets the create form mode from the given property * @param prop The property to get the create form mode from * @returns The create form mode or undefined if not found */ static getCreateFormMode(prop) { if (!prop) return undefined; if (prop.createFormMode) { return prop.createFormMode; } if (prop.formModes) { return (prop.formModes >> 16) & 0xFF; } return undefined; } /** * Gets the update form mode from the given property * @param prop The property to get the update form mode from * @returns The update form mode or undefined if not found */ static getUpdateFormMode(prop) { if (!prop) return undefined; if (prop.updateFormMode) { return prop.updateFormMode; } if (prop.formModes) { return prop.formModes & 0xFF; } return undefined; } /** * Converts a string to PascalCase * @param text The string to convert * @returns The string in PascalCase format */ static toPascalCase(text) { if (!text) return ""; // Remove special characters, convert spaces to single space const cleanedText = text.replace(/[^a-zA-Z0-9\s]/g, "").trim(); // Convert to PascalCase return cleanedText .split(/\s+/) .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) .join(""); } /** * Converts a string to camelCase * @param title The string to convert * @returns The string in camelCase format */ static toCamelCase(title) { if (!title) return undefined; return title.replace(/\s+/g, '').replace(/\b\w/g, char => char.toUpperCase()); } /** * Checks if the request object contains an entity definition * @param req The request object to check * @returns true if the request object contains an entity definition, false otherwise */ static hasEntityDef(req) { var _a, _b; if (req.entDefName || req.entDefId || ((_a = req.entityDef) === null || _a === void 0 ? void 0 : _a.id) || ((_b = req.entityDef) === null || _b === void 0 ? void 0 : _b.name)) { return true; } return false; } } exports.GsbUtils = GsbUtils; GsbUtils.StringPropertyDefinitionId = "c6c34bf3-f51b-4e69-a689-b09847be74b9"; GsbUtils.NumberPropertyDefinitionId = "35efcf9c-fff0-44d4-8972-73a9a32b93fa"; GsbUtils.BooleanPropertyDefinitionId = "7868afdf-2709-45be-87e3-87de8d35f30f"; GsbUtils.DatePropertyDefinitionId = "12e647e0-ebd2-4ec2-a4e3-82c1dfe07da2"; GsbUtils.ReferencePropertyDefinitionId = "924acba8-58c5-4881-940d-472ec01eba5f"; GsbUtils.EnumPropertyDefinitionId = "7bf08f4f-7de0-469e-bbfb-f4c43762f4d7"; GsbUtils.IDPropertyDefinitionId = "5c0aa76f-9c32-4e7e-a4bc-b56e93877883"; GsbUtils.RichTextPropertyDefinitionId = "e07f578e-2705-49c1-b97f-3ca5963c67c0"; GsbUtils.EmailPropertyDefinitionId = "df7ce94b-d59c-4b67-8519-aa4c98ab477c"; GsbUtils.PasswordPropertyDefinitionId = "7291fbc2-a7cf-4713-a876-0cff085cc035"; //# sourceMappingURL=gsb-utils.js.map