Rewild Your Web
web
browser
dweb
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 return (
10 str.includes(".") &&
11 !str.includes(" ") &&
12 (str.startsWith("file://") ||
13 str.startsWith("http://") ||
14 str.startsWith("https://") ||
15 /^[a-zA-Z0-9][-a-zA-Z0-9]*\.[a-zA-Z]{2,}/.test(str))
16 );
17}
18
19/**
20 * Normalize a URL by adding https:// if no protocol is present
21 * @param {string} url - The URL to normalize
22 * @returns {string} - The normalized URL
23 */
24export function normalizeUrl(url) {
25 if (
26 !url.startsWith("file://") &&
27 !url.startsWith("http://") &&
28 !url.startsWith("https://")
29 ) {
30 return "https://" + url;
31 }
32 return url;
33}
34
35/**
36 * Create a debounced version of a function
37 * @param {Function} fn - The function to debounce
38 * @param {number} delay - The delay in milliseconds
39 * @returns {Function} - The debounced function
40 */
41export function debounce(fn, delay) {
42 let timeout;
43 return (...args) => {
44 clearTimeout(timeout);
45 timeout = setTimeout(() => fn(...args), delay);
46 };
47}
48
49/**
50 * Group consecutive results by provider
51 * @param {Array} results - The results to group
52 * @returns {Array} - The grouped results
53 */
54export function groupResults(results) {
55 const groups = [];
56 let currentGroup = null;
57
58 for (const result of results) {
59 if (!currentGroup || currentGroup.provider !== result.provider) {
60 currentGroup = {
61 provider: result.provider,
62 providerIcon: result.providerIcon,
63 items: [],
64 };
65 groups.push(currentGroup);
66 }
67 currentGroup.items.push(result);
68 }
69
70 return groups;
71}