@asaleh37/ui-base 1.2.19 → 1.2.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,3 +21,33 @@ export function isValidEmail(email) {
21
21
  const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
22
22
  return re.test(email);
23
23
  }
24
+
25
+ export function timeAgo(dateInput, appDirection: "ltr" | "rtl") {
26
+ const date = new Date(dateInput);
27
+ const now = new Date();
28
+ const diffInSeconds = Math.floor((date - now) / 1000); // Note: future is positive
29
+ const absDiff = Math.abs(diffInSeconds);
30
+
31
+ const locale = appDirection === "rtl" ? "ar" : "en";
32
+
33
+ const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
34
+
35
+ const units = [
36
+ { unit: "year", seconds: 31536000 },
37
+ { unit: "month", seconds: 2592000 },
38
+ { unit: "week", seconds: 604800 },
39
+ { unit: "day", seconds: 86400 },
40
+ { unit: "hour", seconds: 3600 },
41
+ { unit: "minute", seconds: 60 },
42
+ { unit: "second", seconds: 1 },
43
+ ];
44
+
45
+ for (const { unit, seconds } of units) {
46
+ const delta = Math.floor(diffInSeconds / seconds);
47
+ if (Math.abs(delta) > 1) {
48
+ return rtf.format(delta, unit);
49
+ }
50
+ }
51
+
52
+ return locale === "ar" ? "الآن" : "just now";
53
+ }
Binary file