๐ ๏ธ Utility Functions
This directory contains utility functions and helpers used throughout the idling.app application. Each utility is designed to be modular, reusable, and well-tested.
๐ Directory Structureโ
src/lib/utils/
โโโ README.md # This file - overview of all utilities
โโโ array/ # Array manipulation utilities
โ โโโ README.md # Array utilities documentation
โ โโโ dedupe-string-array.ts # Remove duplicates from string arrays
โโโ parsers/ # Content parsing utilities
โ โโโ README.md # Parser utilities documentation
โ โโโ emoji-parser.ts # Emoji processing and rendering
โ โโโ image-parser.ts # Image URL and metadata parsing
โ โโโ markdown-parser.ts # Markdown to HTML conversion
โ โโโ rich-text-parser.ts # Rich text content processing
โโโ string/ # String manipulation utilities
โ โโโ README.md # String utilities documentation
โ โโโ index.ts # String utilities exports
โ โโโ make-id.ts # Generate unique identifiers
โ โโโ tag-regex.ts # Regular expressions for tags
โ โโโ tag-utils.ts # Tag processing utilities
โโโ cache-manager.ts # Application cache management
โโโ content-parsers.ts # Advanced content parsing and tokenization
โโโ hard-reset-manager.ts # Application state reset utilities
โโโ os-detection.ts # Operating system detection
โโโ privacy.ts # Privacy and data protection utilities
โโโ requestIdentifier.ts # Request identification and tracking
โโโ scroll-highlight-demo.ts # Scroll-based highlighting demo
โโโ scroll-position.ts # Scroll position management
โโโ server-logger.ts # Server-side logging utilities
โโโ service-worker-cleanup.ts # Service worker management and cleanup
โโโ social-sharing.ts # Social media sharing utilities
โโโ text-extraction.ts # Text parsing and extraction
โโโ time-utils.ts # Time and date utilities
โโโ timeFormatting.ts # Time formatting and display
๐ Core Utilitiesโ
Text Processingโ
- text-extraction.ts - Extract hashtags, mentions, URLs, and emojis from text
- content-parsers.ts - Advanced content parsing with tokenization
- parsers/ - Specialized content parsers (emoji, image, markdown, rich text)
System Managementโ
- service-worker-cleanup.ts - Browser service worker debugging and cleanup
- cache-manager.ts - Application cache management and optimization
- hard-reset-manager.ts - Complete application state reset
Data Utilitiesโ
- string/ - String manipulation and ID generation
- array/ - Array processing and deduplication
- time-utils.ts & timeFormatting.ts - Time and date handling
UI & Interactionโ
- scroll-position.ts - Scroll position tracking and management
- scroll-highlight-demo.ts - Scroll-based highlighting effects
- os-detection.ts - Operating system and browser detection
Infrastructureโ
- server-logger.ts - Server-side logging and monitoring
- requestIdentifier.ts - Request tracking and identification
- privacy.ts - Privacy protection and data handling
- social-sharing.ts - Social media integration
๐ Usage Patternsโ
Importing Utilitiesโ
// Import specific utilities
import { TextExtractor } from '@lib/utils/text-extraction';
import { CacheManager } from '@lib/utils/cache-manager';
// Import from subdirectories
import { makeId } from '@lib/utils/string/make-id';
import { EmojiParser } from '@lib/utils/parsers/emoji-parser';
Common Patternsโ
// Text processing pipeline
import { TextExtractor } from '@lib/utils/text-extraction';
import { ContentParser } from '@lib/utils/content-parsers';
const userInput = 'Check out #coding with @[dev|user123] ๐';
const extracted = TextExtractor.extractAll(userInput);
const parsed = ContentParser.parse(userInput);
// Cache management
import { CacheManager } from '@lib/utils/cache-manager';
await CacheManager.set('user-data', userData, { ttl: 3600 });
const cachedData = await CacheManager.get('user-data');
// System utilities
import { OSDetection } from '@lib/utils/os-detection';
import { Logger } from '@lib/utils/server-logger';
const userOS = OSDetection.detect();
Logger.info('User connected', { os: userOS });
๐ง Development Guidelinesโ
Creating New Utilitiesโ
- Single Responsibility - Each utility should have a clear, focused purpose
- Type Safety - Use TypeScript interfaces and proper type definitions
- Error Handling - Include comprehensive error handling and validation
- Documentation - Add JSDoc comments and usage examples
- Testing - Write unit tests for all public functions
File Organizationโ
- Root level - General-purpose utilities used across the application
- Subdirectories - Related utilities grouped by domain (string, array, parsers)
- README.md - Documentation for each directory and major utility
Naming Conventionsโ
- Files - kebab-case (e.g.,
text-extraction.ts
) - Classes - PascalCase (e.g.,
TextExtractor
) - Functions - camelCase (e.g.,
extractHashtags
) - Constants - UPPER_SNAKE_CASE (e.g.,
MAX_CACHE_SIZE
)
๐งช Testingโ
Each utility should include comprehensive tests:
// Example test structure
describe('TextExtractor', () => {
describe('extractHashtags', () => {
it('should extract hashtags from text', () => {
const text = 'Love #coding and #javascript!';
const hashtags = TextExtractor.extractHashtags(text);
expect(hashtags).toEqual(['#coding', '#javascript']);
});
});
});
๐ Related Documentationโ
- Libraries Documentation - Higher-level library documentation
- Development Guide - Development setup and practices
- API Documentation - API endpoint documentation
These utilities form the foundation of idling.app's functionality. They are designed to be reliable, performant, and easy to use across the entire application.