Files
P4RS3LT0NGV3/src/transformers/encoding/base32.js
T
Dustin Farley dc10a90851 refactor: migrate to modular tool-based architecture
- Implement tool registry system with individual tool modules
- Reorganize transformers into categorized source modules
- Remove emojiLibrary.js, consolidate into EmojiUtils and emojiData
- Fix mobile close button and tooltip functionality
- Add build system for transforms and emoji data
- Migrate from Python backend to pure JavaScript
- Add comprehensive documentation and testing
- Improve code organization and maintainability
- Ignore generated files (transforms-bundle.js, emojiData.js)
2025-12-02 20:26:32 -08:00

85 lines
2.7 KiB
JavaScript

// base32 transform
import BaseTransformer from '../BaseTransformer.js';
export default new BaseTransformer({
name: 'Base32',
priority: 280,
// Detector: Only Base32 characters (A-Z, 2-7, =)
detector: function(text) {
const cleaned = text.trim().replace(/\s/g, '');
return cleaned.length >= 8 && /^[A-Z2-7=]+$/.test(cleaned);
},
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
func: function(text) {
if (!text) return '';
// Convert text to bytes
const bytes = new TextEncoder().encode(text);
let result = '';
let bits = 0;
let value = 0;
for (let i = 0; i < bytes.length; i++) {
value = (value << 8) | bytes[i];
bits += 8;
while (bits >= 5) {
bits -= 5;
result += this.alphabet[(value >> bits) & 0x1F];
}
}
// Handle remaining bits
if (bits > 0) {
result += this.alphabet[(value << (5 - bits)) & 0x1F];
}
// Add padding
while (result.length % 8 !== 0) {
result += '=';
}
return result;
},
preview: function(text) {
if (!text) return '[base32]';
const full = this.func(text);
return full.substring(0, 16) + (full.length > 16 ? '...' : '');
},
reverse: function(text) {
if (!text) return '';
// Remove padding and whitespace
text = text.replace(/\s+/g, '').replace(/=+$/, '');
if (text.length === 0) return '';
// Create reverse map
const revMap = {};
for (let i = 0; i < this.alphabet.length; i++) {
revMap[this.alphabet[i]] = i;
}
const bytes = [];
let bits = 0;
let value = 0;
for (let i = 0; i < text.length; i++) {
const char = text[i].toUpperCase();
if (revMap[char] === undefined) continue; // Skip invalid characters
value = (value << 5) | revMap[char];
bits += 5;
while (bits >= 8) {
bits -= 8;
bytes.push((value >> bits) & 0xFF);
}
}
// Use TextDecoder to properly handle UTF-8 multi-byte characters
return new TextDecoder().decode(new Uint8Array(bytes));
}
});