@megha-ui/react 1.2.107 → 1.2.109
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.
|
@@ -1,12 +1,57 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.formatValue = void 0;
|
|
4
|
+
const regexUtils_1 = require("../components/grid/utils/regexUtils");
|
|
4
5
|
// Function to format numbers
|
|
5
6
|
const formatNumber = (value, locale = "sv-SE", options = {}) => {
|
|
6
7
|
const formatter = new Intl.NumberFormat(locale, options);
|
|
7
8
|
return formatter.format(value);
|
|
8
9
|
};
|
|
9
|
-
|
|
10
|
+
const toISODate = (d) => d.toISOString().slice(0, 10);
|
|
11
|
+
const parseSupportedDate = (input) => {
|
|
12
|
+
if (input instanceof Date) {
|
|
13
|
+
return isNaN(input.getTime()) ? null : input;
|
|
14
|
+
}
|
|
15
|
+
if (typeof input === "number") {
|
|
16
|
+
const d = new Date(input);
|
|
17
|
+
return isNaN(d.getTime()) ? null : d;
|
|
18
|
+
}
|
|
19
|
+
if (typeof input !== "string")
|
|
20
|
+
return null;
|
|
21
|
+
const str = input.trim();
|
|
22
|
+
if (!str)
|
|
23
|
+
return null;
|
|
24
|
+
if (/^\d{2}[-\/]\d{2}[-\/]\d{4}$/.test(str)) {
|
|
25
|
+
const sep = str.includes("-") ? "-" : "/";
|
|
26
|
+
const [p1s, p2s, ys] = str.split(sep);
|
|
27
|
+
const part1 = Number(p1s);
|
|
28
|
+
const part2 = Number(p2s);
|
|
29
|
+
const year = Number(ys);
|
|
30
|
+
const day = part1 > 12 ? part1 : part2;
|
|
31
|
+
const month = part1 > 12 ? part2 : part1;
|
|
32
|
+
const d = new Date(year, month - 1, day);
|
|
33
|
+
return d.getFullYear() === year &&
|
|
34
|
+
d.getMonth() + 1 === month &&
|
|
35
|
+
d.getDate() === day
|
|
36
|
+
? d
|
|
37
|
+
: null;
|
|
38
|
+
}
|
|
39
|
+
if (/^\d{4}[-\/.]\d{2}[-\/.]\d{2}$/.test(str)) {
|
|
40
|
+
const sep = str.includes("-") ? "-" : str.includes("/") ? "/" : ".";
|
|
41
|
+
const [ys, ms, ds] = str.split(sep);
|
|
42
|
+
const year = Number(ys);
|
|
43
|
+
const month = Number(ms);
|
|
44
|
+
const day = Number(ds);
|
|
45
|
+
const d = new Date(year, month - 1, day);
|
|
46
|
+
return d.getFullYear() === year &&
|
|
47
|
+
d.getMonth() + 1 === month &&
|
|
48
|
+
d.getDate() === day
|
|
49
|
+
? d
|
|
50
|
+
: null;
|
|
51
|
+
}
|
|
52
|
+
const d = new Date(str);
|
|
53
|
+
return isNaN(d.getTime()) ? null : d;
|
|
54
|
+
};
|
|
10
55
|
const formatDate = (date, locale = "sv-SE", options = {}) => {
|
|
11
56
|
const formatter = new Intl.DateTimeFormat(locale, options);
|
|
12
57
|
return formatter.format(date);
|
|
@@ -18,23 +63,38 @@ const formatCurrency = (value, locale = "sv-SE", currency = "SEK", options = {})
|
|
|
18
63
|
};
|
|
19
64
|
// Dynamic formatter function
|
|
20
65
|
const formatValue = (value, type = "number", locale = "sv-SE", options = {
|
|
21
|
-
currency: "SEK"
|
|
66
|
+
currency: "SEK",
|
|
22
67
|
}) => {
|
|
23
|
-
var _a, _b;
|
|
24
|
-
const date = value instanceof Date ? value : new Date(value);
|
|
25
68
|
if (type === "number") {
|
|
26
69
|
const num = parseFloat(value.toString());
|
|
27
70
|
return formatNumber(num, locale, options);
|
|
28
71
|
}
|
|
29
|
-
else if (type === "date"
|
|
30
|
-
|
|
72
|
+
else if (type === "date") {
|
|
73
|
+
let date = "";
|
|
74
|
+
if (value instanceof Date) {
|
|
75
|
+
date = isNaN(value.getTime()) ? "" : toISODate(value);
|
|
76
|
+
}
|
|
77
|
+
if (typeof value === "number") {
|
|
78
|
+
const d = parseSupportedDate(value);
|
|
79
|
+
date = d ? toISODate(d) : "";
|
|
80
|
+
}
|
|
81
|
+
if (typeof value === "string") {
|
|
82
|
+
if (!(0, regexUtils_1.isValidDateFormat)(value))
|
|
83
|
+
return value;
|
|
84
|
+
const d = parseSupportedDate(value);
|
|
85
|
+
date = d ? toISODate(d) : value;
|
|
86
|
+
}
|
|
87
|
+
if (date) {
|
|
88
|
+
return formatDate(new Date(date).getTime(), locale, options);
|
|
89
|
+
}
|
|
90
|
+
return value.toString();
|
|
31
91
|
}
|
|
32
92
|
else if (type === "currency") {
|
|
33
93
|
const num = parseFloat(value.toString());
|
|
34
94
|
return formatCurrency(num, locale, options.currency, options);
|
|
35
95
|
}
|
|
36
96
|
else {
|
|
37
|
-
return
|
|
97
|
+
return value.toString();
|
|
38
98
|
}
|
|
39
99
|
};
|
|
40
100
|
exports.formatValue = formatValue;
|
package/package.json
CHANGED