UNPKG

@zenithcore/ost-compression

Version:

Okaily-Srivastava-Tbakhi (OST) encoding algorithm for textual data compression, optimized for JavaScript/TypeScript and ZenithKernel bundles

193 lines (144 loc) โ€ข 5.44 kB
# @zenithcore/ost-compression **Okaily-Srivastava-Tbakhi (OST) Encoding Algorithm** for textual data compression, specifically optimized for JavaScript/TypeScript source code and ZenithKernel bundles. ## ๐ŸŽ“ About the Algorithm The OST encoding algorithm is named after its researchers: - **Anas Al-okaily** (King Hussein Cancer Center, Jordan) - **Pramod Srivastava** - **Abdelghani Tbakhi** (King Hussein Cancer Center, Jordan) Based on the research paper: *"A Novel Encoding Algorithm for Textual Data Compression"* ## ๐ŸŒŸ Features ### ๐Ÿงฌ Advanced Pattern Recognition - Adaptive dictionary building with sliding window approach - Context-aware pattern extraction - Frequency-based pattern optimization ### โšก JavaScript/TypeScript Optimization - Syntax-aware preprocessing - Keyword compression using Unicode symbols - Operator optimization for common patterns ### ๐Ÿ“Š Superior Compression - Outperforms traditional algorithms on structured text - Optimized for programming languages - Lossless compression with exact restoration ### ๐Ÿ”ง Configurable Parameters - Adjustable window sizes and pattern lengths - Dictionary size optimization - Syntax optimization toggles ## ๐Ÿš€ Installation ```bash npm install @zenithcore/ost-compression ``` ## ๐Ÿ“– Usage ### Basic Compression ```typescript import { compress, decompress } from '@zenithcore/ost-compression'; const sourceCode = ` function fibonacci(n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); } `; // Compress const result = await compress(sourceCode, { method: 'huffman', labelLength: 3, windowLength: 512, enableOptimizations: true }); console.log('Compression ratio:', result.ratio + 'x'); console.log('Original size:', sourceCode.length); console.log('Compressed size:', result.compressed.length); // Decompress const restored = await decompress(result.compressed, result.metadata); console.log('Restored:', restored === sourceCode); // true ``` ### Advanced Usage with OST Encoder ```typescript import { OSTEncoder, OSTDecoder } from '@zenithcore/ost-compression'; const encoder = new OSTEncoder({ windowSize: 1024, labelLength: 4, minPatternLength: 3, maxPatternLength: 64, dictionarySize: 8192, enableSyntaxOptimization: true }); // Encode const encoded = await encoder.encode(sourceCode); console.log('Dictionary patterns:', encoded.dictionary.size); console.log('Compression ratio:', encoded.metadata.compressionRatio); // Decode const decoder = new OSTDecoder(); const decoded = await decoder.decode(encoded); console.log('Successfully restored:', decoded.length, 'characters'); ``` ### Configuration Options ```typescript interface OSTConfig { windowSize: number; // Sliding window size (default: 512) labelLength: number; // Pattern label length (default: 3) minPatternLength: number; // Minimum pattern length (default: 2) maxPatternLength: number; // Maximum pattern length (default: 32) dictionarySize: number; // Maximum dictionary size (default: 4096) enableSyntaxOptimization: boolean; // Enable JS/TS optimizations (default: true) } ``` ## ๐Ÿ”ฌ Algorithm Details ### Pattern Extraction The OST algorithm uses a sliding window approach to extract recurring patterns: 1. **Window Scanning**: Slides through text with configurable window size 2. **Pattern Frequency**: Counts occurrence of all possible substrings 3. **Benefit Calculation**: Ranks patterns by `frequency ร— length` 4. **Dictionary Building**: Selects top patterns for compression dictionary ### Syntax Optimization For JavaScript/TypeScript code, OST applies preprocessing: - `function` โ†’ `ฦ’` (Unicode function symbol) - `=>` โ†’ `โ†’` (Arrow symbol) - `const` โ†’ `โ„‚` (Complex numbers symbol) - `===` โ†’ `โ‰ก` (Identical to symbol) - And many more optimizations... ### Encoding Process 1. **Preprocessing**: Apply syntax optimizations 2. **Dictionary Building**: Extract and rank patterns 3. **Encoding**: Replace patterns with dictionary references 4. **Output**: Compressed data + dictionary + metadata ## ๐Ÿ“Š Performance ### Compression Ratios (typical) - **JavaScript/TypeScript**: 3-8x compression - **JSON data**: 4-12x compression - **HTML/XML**: 5-15x compression - **General text**: 2-6x compression ### Benchmarks ``` Source Type | Size (KB) | Compressed (KB) | Ratio | Time (ms) ---------------------|-----------|-----------------|-------|---------- React Component | 45.2 | 8.1 | 5.6x | 12 TypeScript Module | 128.7 | 18.3 | 7.0x | 28 JSON Configuration | 67.4 | 5.2 | 13.0x | 8 ``` ## ๐Ÿงช Testing ```bash # Run tests bun test # Run benchmarks bun run benchmark # Type checking bun run typecheck ``` ## ๐Ÿ”ฌ Research & Citations If you use this implementation in academic work, please cite: ```bibtex @article{okaily2023ost, title={A Novel Encoding Algorithm for Textual Data Compression}, author={Al-okaily, Anas and Srivastava, Pramod and Tbakhi, Abdelghani}, journal={[Journal Name]}, year={2023}, publisher={[Publisher]} } ``` ## ๐Ÿค Contributing Contributions are welcome! Please read our contributing guidelines and submit pull requests. ## ๐Ÿ“„ License MIT License - see the [LICENSE](../../LICENSE) file for details. --- **Part of the ZenithKernel ecosystem** ๐ŸŒŠ *Implementing cutting-edge compression research for modern web development*