cynx-lib 1.3.45 → 1.3.47

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.
package/utils/utils/ui.js CHANGED
@@ -1,111 +1,111 @@
1
- /**
2
- * cynx/utils — UI and Formatting Helpers (ui.js)
3
- *
4
- * Universal classname merger (cn), clipboard helper, and friendly date/byte formatters.
5
- */
6
-
7
- /**
8
- * Universal class names combiner / merger (zero-dependency clsx replacement).
9
- * Handles strings, arrays, objects, booleans, and null/undefined values.
10
- *
11
- * @param {...any} inputs
12
- * @returns {string} Combined class string
13
- */
14
- export function cn(...inputs) {
15
- const classes = [];
16
-
17
- for (let i = 0; i < inputs.length; i++) {
18
- const arg = inputs[i];
19
- if (!arg) continue;
20
-
21
- const argType = typeof arg;
22
- if (argType === 'string' || argType === 'number') {
23
- classes.push(arg);
24
- } else if (Array.isArray(arg)) {
25
- if (arg.length) {
26
- const inner = cn(...arg);
27
- if (inner) classes.push(inner);
28
- }
29
- } else if (argType === 'object') {
30
- for (const key in arg) {
31
- if (Object.prototype.hasOwnProperty.call(arg, key) && arg[key]) {
32
- classes.push(key);
33
- }
34
- }
35
- }
36
- }
37
-
38
- return classes.join(' ');
39
- }
40
-
41
- /**
42
- * Safely copy text to clipboard with modern Clipboard API and legacy fallback.
43
- *
44
- * @param {string} text
45
- * @returns {Promise<boolean>}
46
- */
47
- export async function copyToClipboard(text) {
48
- if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
49
- try {
50
- await navigator.clipboard.writeText(text);
51
- return true;
52
- } catch {}
53
- }
54
-
55
- if (typeof document !== 'undefined') {
56
- try {
57
- const el = document.createElement('textarea');
58
- el.value = text;
59
- el.setAttribute('readonly', '');
60
- el.style.position = 'absolute';
61
- el.style.left = '-9999px';
62
- document.body.appendChild(el);
63
- el.select();
64
- const successful = document.execCommand('copy');
65
- document.body.removeChild(el);
66
- return successful;
67
- } catch {}
68
- }
69
-
70
- return false;
71
- }
72
-
73
- /**
74
- * Format relative time (e.g. "5s ago", "12m ago", "3h ago", "2d ago").
75
- *
76
- * @param {Date|string|number} d
77
- * @returns {string}
78
- */
79
- export function fmtAgo(d) {
80
- if (!d) return '—';
81
- const s = Math.floor((Date.now() - new Date(d).getTime()) / 1000);
82
- if (isNaN(s)) return '—';
83
- if (s < 60) return `${Math.max(0, s)}s ago`;
84
- if (s < 3600) return `${Math.floor(s / 60)}m ago`;
85
- if (s < 86400) return `${Math.floor(s / 3600)}h ago`;
86
- return `${Math.floor(s / 86400)}d ago`;
87
- }
88
-
89
- /**
90
- * Format localized date or return fallback dash.
91
- *
92
- * @param {Date|string|number} d
93
- * @param {string} [locale='ru-RU']
94
- * @returns {string}
95
- */
96
- export function fmtDate(d, locale = 'ru-RU') {
97
- if (!d) return '—';
98
- try {
99
- const date = new Date(d);
100
- return isNaN(date.getTime()) ? '—' : date.toLocaleString(locale);
101
- } catch {
102
- return '—';
103
- }
104
- }
105
-
106
- export default {
107
- cn,
108
- copyToClipboard,
109
- fmtAgo,
110
- fmtDate,
111
- };
1
+ /**
2
+ * cynx/utils — UI and Formatting Helpers (ui.js)
3
+ *
4
+ * Universal classname merger (cn), clipboard helper, and friendly date/byte formatters.
5
+ */
6
+
7
+ /**
8
+ * Universal class names combiner / merger (zero-dependency clsx replacement).
9
+ * Handles strings, arrays, objects, booleans, and null/undefined values.
10
+ *
11
+ * @param {...any} inputs
12
+ * @returns {string} Combined class string
13
+ */
14
+ export function cn(...inputs) {
15
+ const classes = [];
16
+
17
+ for (let i = 0; i < inputs.length; i++) {
18
+ const arg = inputs[i];
19
+ if (!arg) continue;
20
+
21
+ const argType = typeof arg;
22
+ if (argType === 'string' || argType === 'number') {
23
+ classes.push(arg);
24
+ } else if (Array.isArray(arg)) {
25
+ if (arg.length) {
26
+ const inner = cn(...arg);
27
+ if (inner) classes.push(inner);
28
+ }
29
+ } else if (argType === 'object') {
30
+ for (const key in arg) {
31
+ if (Object.prototype.hasOwnProperty.call(arg, key) && arg[key]) {
32
+ classes.push(key);
33
+ }
34
+ }
35
+ }
36
+ }
37
+
38
+ return classes.join(' ');
39
+ }
40
+
41
+ /**
42
+ * Safely copy text to clipboard with modern Clipboard API and legacy fallback.
43
+ *
44
+ * @param {string} text
45
+ * @returns {Promise<boolean>}
46
+ */
47
+ export async function copyToClipboard(text) {
48
+ if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
49
+ try {
50
+ await navigator.clipboard.writeText(text);
51
+ return true;
52
+ } catch {}
53
+ }
54
+
55
+ if (typeof document !== 'undefined') {
56
+ try {
57
+ const el = document.createElement('textarea');
58
+ el.value = text;
59
+ el.setAttribute('readonly', '');
60
+ el.style.position = 'absolute';
61
+ el.style.left = '-9999px';
62
+ document.body.appendChild(el);
63
+ el.select();
64
+ const successful = document.execCommand('copy');
65
+ document.body.removeChild(el);
66
+ return successful;
67
+ } catch {}
68
+ }
69
+
70
+ return false;
71
+ }
72
+
73
+ /**
74
+ * Format relative time (e.g. "5s ago", "12m ago", "3h ago", "2d ago").
75
+ *
76
+ * @param {Date|string|number} d
77
+ * @returns {string}
78
+ */
79
+ export function fmtAgo(d) {
80
+ if (!d) return '—';
81
+ const s = Math.floor((Date.now() - new Date(d).getTime()) / 1000);
82
+ if (isNaN(s)) return '—';
83
+ if (s < 60) return `${Math.max(0, s)}s ago`;
84
+ if (s < 3600) return `${Math.floor(s / 60)}m ago`;
85
+ if (s < 86400) return `${Math.floor(s / 3600)}h ago`;
86
+ return `${Math.floor(s / 86400)}d ago`;
87
+ }
88
+
89
+ /**
90
+ * Format localized date or return fallback dash.
91
+ *
92
+ * @param {Date|string|number} d
93
+ * @param {string} [locale='ru-RU']
94
+ * @returns {string}
95
+ */
96
+ export function fmtDate(d, locale = 'ru-RU') {
97
+ if (!d) return '—';
98
+ try {
99
+ const date = new Date(d);
100
+ return isNaN(date.getTime()) ? '—' : date.toLocaleString(locale);
101
+ } catch {
102
+ return '—';
103
+ }
104
+ }
105
+
106
+ export default {
107
+ cn,
108
+ copyToClipboard,
109
+ fmtAgo,
110
+ fmtDate,
111
+ };