/** * TT-Core Validation Utilities * String similarity and data validation */ /** * Calculate similarity between two strings (0-100%) * @param {string} str1 - First string * @param {string} str2 - Second string * @returns {number} - Similarity percentage */ export function calculateSimilarity(str1, str2) { if (!str1 || !str2) return 0; str1 = ('' + str1).toLowerCase(); str2 = ('' + str2).toLowerCase(); let matchCount = 0; for (let char of str1) { if (str2.includes(char)) matchCount++; } return (matchCount / str1.length) * 100; } /** * Validate data against multiple fields with similarity threshold * @param {string} street - Street name * @param {string} zip - ZIP code * @param {string} city - City name * @param {string} info - Info to validate against * @param {number} threshold - Similarity threshold (default: 90) * @returns {boolean} - Validation result */ export function validateData(street, zip, city, info, threshold = 90) { return !( calculateSimilarity(street, info) < threshold || calculateSimilarity(zip, info) < threshold || calculateSimilarity(city, info) < threshold ); } /** * Validate email format * @param {string} email - Email to validate * @returns {boolean} - Validation result */ export function validateEmail(email) { if (!email) return false; return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); } /** * Generate random password * @param {number} length - Password length * @returns {string} - Generated password */ export function generatePassword(length = 12) { const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; let password = ""; for (let i = 0; i < length; i++) { password += chars.charAt(Math.floor(Math.random() * chars.length)); } return password; }