Rewild Your Web
at main 76 lines 1.8 kB view raw
1// SPDX-License-Identifier: AGPL-3.0-or-later 2 3/** 4 * Simple URL detection - checks if a string looks like a URL 5 * @param {string} str - The string to check 6 * @returns {boolean} - True if it looks like a URL 7 */ 8export function isUrl(str) { 9 if (URL.parse(str) != null) { 10 return true; 11 } 12 13 return ( 14 str.includes(".") && 15 !str.includes(" ") && 16 URL.parse(`https://${str}`) != null 17 ); 18} 19 20/** 21 * Normalize a URL by adding https:// if no protocol is present 22 * @param {string} url - The URL to normalize 23 * @returns {string} - The normalized URL 24 */ 25export function normalizeUrl(url) { 26 if ( 27 !url.startsWith("about:") && 28 !url.startsWith("at:") && 29 !url.startsWith("data:") && 30 !url.startsWith("file://") && 31 !url.startsWith("http://") && 32 !url.startsWith("https://") && 33 !url.startsWith("trusted:") 34 ) { 35 return "https://" + url; 36 } 37 return url; 38} 39 40/** 41 * Create a debounced version of a function 42 * @param {Function} fn - The function to debounce 43 * @param {number} delay - The delay in milliseconds 44 * @returns {Function} - The debounced function 45 */ 46export function debounce(fn, delay) { 47 let timeout; 48 return (...args) => { 49 clearTimeout(timeout); 50 timeout = setTimeout(() => fn(...args), delay); 51 }; 52} 53 54/** 55 * Group consecutive results by provider 56 * @param {Array} results - The results to group 57 * @returns {Array} - The grouped results 58 */ 59export function groupResults(results) { 60 const groups = []; 61 let currentGroup = null; 62 63 for (const result of results) { 64 if (!currentGroup || currentGroup.provider !== result.provider) { 65 currentGroup = { 66 provider: result.provider, 67 providerIcon: result.providerIcon, 68 items: [], 69 }; 70 groups.push(currentGroup); 71 } 72 currentGroup.items.push(result); 73 } 74 75 return groups; 76}