🔗 Shared Module - TypeScript Functions

Common utilities, services, and components used across the application

📋 Overview

This document provides comprehensive TypeScript function documentation for the Shared module. The Shared module contains common utilities, services, and components that are used across multiple modules in the Angular admin panel application, including loading indicators, toast notifications, utility functions, and shared pipes.

📌 Key Features: Loading management, Toast notifications, Utility functions, Date/time handling, Data formatting, Cross-module communication, Shared components

⏳ LoaderService

File: src/app/shared/services/loader.service.ts

Purpose: Global loading state management for HTTP requests and async operations

🎛️ State Management Methods

show(): void

Purpose: Shows the global loading spinner

/**
 * Shows the global loading spinner
 * Updates loading state to true
 * Displays loading overlay across the application
 * Used for indicating async operations in progress
 * @returns {void}
 */
show(): void
Returns: void
hide(): void

Purpose: Hides the global loading spinner

/**
 * Hides the global loading spinner
 * Updates loading state to false
 * Removes loading overlay from the application
 * Called when async operations complete
 * @returns {void}
 */
hide(): void
Returns: void
getLoadingState(): Observable<boolean>

Purpose: Returns observable of current loading state

/**
 * Returns observable of current loading state
 * Allows components to subscribe to loading changes
 * Provides reactive updates for loading indicators
 * Used by loading component and interceptors
 * @returns {Observable<boolean>} Loading state observable
 */
getLoadingState(): Observable<boolean>
Returns: Observable<boolean> - Loading state stream

🍞 ToastService

File: src/app/shared/services/toast.service.ts

Purpose: Global toast notification system for user feedback

📢 Notification Methods

showSuccess(message: string, title?: string): void

Purpose: Displays success toast notification

message: string - Success message to display
title: string (optional) - Toast title
/**
 * Displays success toast notification
 * Shows green-themed success message to user
 * Automatically dismisses after configured duration
 * Used for confirming successful operations
 * @param {string} message - Success message to display
 * @param {string} title - Optional toast title
 * @returns {void}
 */
showSuccess(message: string, title?: string): void
Returns: void
showError(message: string, title?: string): void

Purpose: Displays error toast notification

message: string - Error message to display
title: string (optional) - Toast title
/**
 * Displays error toast notification
 * Shows red-themed error message to user
 * Longer display duration for error visibility
 * Used for notifying operation failures
 * @param {string} message - Error message to display
 * @param {string} title - Optional toast title
 * @returns {void}
 */
showError(message: string, title?: string): void
Returns: void
showWarning(message: string, title?: string): void

Purpose: Displays warning toast notification

message: string - Warning message to display
title: string (optional) - Toast title
/**
 * Displays warning toast notification
 * Shows yellow-themed warning message to user
 * Used for cautionary messages and validation warnings
 * Intermediate display duration for user attention
 * @param {string} message - Warning message to display
 * @param {string} title - Optional toast title
 * @returns {void}
 */
showWarning(message: string, title?: string): void
Returns: void
showInfo(message: string, title?: string): void

Purpose: Displays informational toast notification

message: string - Information message to display
title: string (optional) - Toast title
/**
 * Displays informational toast notification
 * Shows blue-themed information message to user
 * Used for general information and status updates
 * Standard display duration for informational content
 * @param {string} message - Information message to display
 * @param {string} title - Optional toast title
 * @returns {void}
 */
showInfo(message: string, title?: string): void
Returns: void

🛠️ UtilityService

File: src/app/shared/services/utility.service.ts

Purpose: Common utility functions for data manipulation and formatting

📅 Date and Time Methods

formatDate(date: Date, format: string): string

Purpose: Formats date according to specified format string

date: Date - Date object to format
format: string - Format pattern (YYYY-MM-DD, etc.)
/**
 * Formats date according to specified format string
 * Supports various date formats and localization
 * Handles timezone conversions and null safety
 * Used throughout application for consistent date display
 * @param {Date} date - Date object to format
 * @param {string} format - Format pattern (YYYY-MM-DD, etc.)
 * @returns {string} Formatted date string
 */
formatDate(date: Date, format: string): string
Returns: string - Formatted date string
getCurrentTimezone(): string

Purpose: Gets the current user's timezone

/**
 * Gets the current user's timezone
 * Detects browser timezone automatically
 * Returns timezone identifier string
 * Used for timezone-aware date operations
 * @returns {string} Timezone identifier (e.g., 'America/New_York')
 */
getCurrentTimezone(): string
Returns: string - Timezone identifier

📊 Data Processing Methods

deepClone<T>(obj: T): T

Purpose: Creates a deep copy of an object

obj: T - Object to clone
/**
 * Creates a deep copy of an object
 * Handles nested objects and arrays recursively
 * Prevents reference sharing between objects
 * Used for immutable data operations
 * @param {T} obj - Object to clone
 * @returns {T} Deep cloned object
 */
deepClone<T>(obj: T): T
Returns: T - Deep cloned object
sortArray<T>(array: T[], key: string, direction: 'asc' | 'desc'): T[]

Purpose: Sorts array by specified property

array: T[] - Array to sort
key: string - Property name to sort by
direction: 'asc' | 'desc' - Sort direction
/**
 * Sorts array by specified property
 * Supports ascending and descending order
 * Handles string, number, and date comparisons
 * Returns new sorted array without mutating original
 * @param {T[]} array - Array to sort
 * @param {string} key - Property name to sort by
 * @param {'asc' | 'desc'} direction - Sort direction
 * @returns {T[]} Sorted array
 */
sortArray<T>(array: T[], key: string, direction: 'asc' | 'desc'): T[]
Returns: T[] - Sorted array
filterArray<T>(array: T[], searchTerm: string, fields: string[]): T[]

Purpose: Filters array by search term across multiple fields

array: T[] - Array to filter
searchTerm: string - Search term
fields: string[] - Fields to search in
/**
 * Filters array by search term across multiple fields
 * Case-insensitive search across specified properties
 * Supports partial matching and multiple field search
 * Returns filtered array matching search criteria
 * @param {T[]} array - Array to filter
 * @param {string} searchTerm - Search term
 * @param {string[]} fields - Fields to search in
 * @returns {T[]} Filtered array
 */
filterArray<T>(array: T[], searchTerm: string, fields: string[]): T[]
Returns: T[] - Filtered array

🔧 String Utility Methods

truncateText(text: string, maxLength: number): string

Purpose: Truncates text to specified length with ellipsis

text: string - Text to truncate
maxLength: number - Maximum length
/**
 * Truncates text to specified length with ellipsis
 * Adds '...' when text exceeds limit
 * Preserves word boundaries when possible
 * Used for consistent text display in tables and cards
 * @param {string} text - Text to truncate
 * @param {number} maxLength - Maximum length
 * @returns {string} Truncated text with ellipsis
 */
truncateText(text: string, maxLength: number): string
Returns: string - Truncated text
generateRandomString(length: number): string

Purpose: Generates random alphanumeric string

length: number - Length of string to generate
/**
 * Generates random alphanumeric string
 * Uses cryptographically secure random generation
 * Includes uppercase, lowercase, and numbers
 * Used for temporary IDs and unique identifiers
 * @param {number} length - Length of string to generate
 * @returns {string} Random alphanumeric string
 */
generateRandomString(length: number): string
Returns: string - Random string

✅ Validation Methods

validateEmail(email: string): boolean

Purpose: Validates email address format

email: string - Email address to validate
/**
 * Validates email address format
 * Uses RFC-compliant email regex pattern
 * Checks for proper email structure and domain
 * Returns boolean validation result
 * @param {string} email - Email address to validate
 * @returns {boolean} True if email is valid
 */
validateEmail(email: string): boolean
Returns: boolean - Validation result
validatePhoneNumber(phone: string): boolean

Purpose: Validates phone number format

phone: string - Phone number to validate
/**
 * Validates phone number format
 * Supports international and domestic formats
 * Handles various phone number patterns
 * Used for form validation across modules
 * @param {string} phone - Phone number to validate
 * @returns {boolean} True if phone number is valid
 */
validatePhoneNumber(phone: string): boolean
Returns: boolean - Validation result

🔧 Shared Pipes

Purpose: Custom pipes for data transformation and display formatting

📊 Data Transform Pipes

TruncatePipe

Purpose: Pipe for truncating text with configurable length

/**
 * Pipe for truncating text with configurable length
 * Usage: {{ text | truncate:50 }}
 * Adds ellipsis when text exceeds limit
 * Preserves word boundaries when possible
 * @param {string} value - Text to truncate
 * @param {number} limit - Maximum character limit
 * @returns {string} Truncated text
 */
transform(value: string, limit: number = 100): string
Returns: string - Truncated text
SafeHtmlPipe

Purpose: Pipe for safely rendering HTML content

/**
 * Pipe for safely rendering HTML content
 * Usage: {{ htmlContent | safeHtml }}
 * Sanitizes HTML to prevent XSS attacks
 * Allows safe HTML rendering in templates
 * @param {string} value - HTML content to sanitize
 * @returns {SafeHtml} Sanitized HTML content
 */
transform(value: string): SafeHtml
Returns: SafeHtml - Sanitized HTML
PhoneFormatPipe

Purpose: Pipe for formatting phone numbers

/**
 * Pipe for formatting phone numbers
 * Usage: {{ phone | phoneFormat }}
 * Formats numbers according to regional standards
 * Handles international and domestic formats
 * @param {string} value - Phone number to format
 * @param {string} format - Format type (international, domestic)
 * @returns {string} Formatted phone number
 */
transform(value: string, format: string = 'domestic'): string
Returns: string - Formatted phone number