Skip to content

Commit 54c72ed

Browse files
committed
Update useUtils.js
1 parent 5b314f0 commit 54c72ed

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

resources/js/composables/useUtils.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,39 @@ import { useRouter } from "vue-router";
33
export function useUtils() {
44
const router = useRouter();
55

6+
const normalizeDomain = (input = "") => {
7+
let d = String(input).trim().toLowerCase();
8+
d = d.replace(/^https?:\/\//i, "");
9+
d = d.replace(/^[a-z]+:\/\//i, "");
10+
d = d.replace(/@.*/, "");
11+
d = d.replace(/\/.*$/, "");
12+
d = d.replace(/\?.*$/, "");
13+
d = d.replace(/#.*$/, "");
14+
d = d.replace(/:\d+$/, "");
15+
d = d.replace(/^\.+|\.+$/g, "");
16+
return d;
17+
};
18+
19+
const isValidDomain = (domain) => {
20+
if (!domain) return false;
21+
if (domain.length > 253) return false;
22+
if (domain.includes("..")) return false;
23+
24+
const labels = domain.split(".");
25+
if (labels.length < 2) return false;
26+
27+
const labelRe = /^(?!-)(?:[a-z0-9-]{1,63})(?<!-)$/i;
28+
29+
for (const label of labels) {
30+
if (!labelRe.test(label)) return false;
31+
if (label.includes("_")) return false;
32+
}
33+
34+
if (/^\d+$/.test(labels[labels.length - 1])) return false;
35+
36+
return true;
37+
};
38+
639
const formatNumber = (num) => {
740
return new Intl.NumberFormat().format(num);
841
};
@@ -57,6 +90,18 @@ export function useUtils() {
5790
return start + separator + end;
5891
};
5992

93+
const textTruncate = (text, maxLength = 50, suffix = "...") => {
94+
if (!text) {
95+
return "";
96+
}
97+
98+
if (text.length <= maxLength) {
99+
return text;
100+
}
101+
102+
return text.slice(0, maxLength) + suffix;
103+
};
104+
60105
const formatTimeAgo = (dateString, style = "narrow", locale = "en") => {
61106
const now = new Date();
62107

@@ -189,6 +234,8 @@ export function useUtils() {
189234
};
190235

191236
return {
237+
normalizeDomain,
238+
isValidDomain,
192239
formatNumber,
193240
formatCount,
194241
formatDate,
@@ -198,6 +245,7 @@ export function useUtils() {
198245
formatTimeAgo,
199246
formatDuration,
200247
formatBytes,
248+
textTruncate,
201249
goBack,
202250
};
203251
}

0 commit comments

Comments
 (0)