@megha-ui/react 1.2.125 → 1.2.126
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 +1 @@
|
|
|
1
|
-
export declare const formatValue: (value: string | number | Date, type: string, locale?: string, options?:
|
|
1
|
+
export declare const formatValue: (value: string | number | Date, type: string, locale?: string, options?: any) => string | number;
|
|
@@ -3,11 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.formatValue = void 0;
|
|
4
4
|
const regexUtils_1 = require("../components/grid/utils/regexUtils");
|
|
5
5
|
// Function to format numbers
|
|
6
|
-
const formatNumber = (value, locale = "sv-SE"
|
|
7
|
-
const formatter = new Intl.NumberFormat(locale
|
|
6
|
+
const formatNumber = (value, locale = "sv-SE") => {
|
|
7
|
+
const formatter = new Intl.NumberFormat(locale);
|
|
8
8
|
return formatter.format(value);
|
|
9
9
|
};
|
|
10
|
-
|
|
10
|
+
// ISO-like string including local time (no timezone)
|
|
11
|
+
const toISODateTime = (d) => {
|
|
12
|
+
const y = d.getFullYear();
|
|
13
|
+
const m = String(d.getMonth() + 1).padStart(2, "0");
|
|
14
|
+
const day = String(d.getDate()).padStart(2, "0");
|
|
15
|
+
const hh = String(d.getHours()).padStart(2, "0");
|
|
16
|
+
const mm = String(d.getMinutes()).padStart(2, "0");
|
|
17
|
+
const ss = String(d.getSeconds()).padStart(2, "0");
|
|
18
|
+
return `${y}-${m}-${day}T${hh}:${mm}:${ss}`;
|
|
19
|
+
};
|
|
11
20
|
const parseSupportedDate = (input) => {
|
|
12
21
|
if (input instanceof Date) {
|
|
13
22
|
return isNaN(input.getTime()) ? null : input;
|
|
@@ -21,6 +30,28 @@ const parseSupportedDate = (input) => {
|
|
|
21
30
|
const str = input.trim();
|
|
22
31
|
if (!str)
|
|
23
32
|
return null;
|
|
33
|
+
// DD-MM-YYYY HH:mm[:ss] or DD/MM/YYYY HH:mm[:ss]
|
|
34
|
+
if (/^\d{2}[-\/]\d{2}[-\/]\d{4} \d{2}:\d{2}(:\d{2})?$/.test(str)) {
|
|
35
|
+
const [datePart, timePart] = str.split(" ");
|
|
36
|
+
const sep = datePart.includes("-") ? "-" : "/";
|
|
37
|
+
const [p1s, p2s, ys] = datePart.split(sep);
|
|
38
|
+
const [hs, mins, ss = "0"] = timePart.split(":");
|
|
39
|
+
const part1 = Number(p1s);
|
|
40
|
+
const part2 = Number(p2s);
|
|
41
|
+
const year = Number(ys);
|
|
42
|
+
const day = part1 > 12 ? part1 : part2;
|
|
43
|
+
const month = part1 > 12 ? part2 : part1;
|
|
44
|
+
const hour = Number(hs);
|
|
45
|
+
const minute = Number(mins);
|
|
46
|
+
const second = Number(ss);
|
|
47
|
+
const d = new Date(year, month - 1, day, hour, minute, second);
|
|
48
|
+
return d.getFullYear() === year &&
|
|
49
|
+
d.getMonth() + 1 === month &&
|
|
50
|
+
d.getDate() === day
|
|
51
|
+
? d
|
|
52
|
+
: null;
|
|
53
|
+
}
|
|
54
|
+
// DD-MM-YYYY or DD/MM/YYYY
|
|
24
55
|
if (/^\d{2}[-\/]\d{2}[-\/]\d{4}$/.test(str)) {
|
|
25
56
|
const sep = str.includes("-") ? "-" : "/";
|
|
26
57
|
const [p1s, p2s, ys] = str.split(sep);
|
|
@@ -36,6 +67,26 @@ const parseSupportedDate = (input) => {
|
|
|
36
67
|
? d
|
|
37
68
|
: null;
|
|
38
69
|
}
|
|
70
|
+
// YYYY-MM-DD HH:mm[:ss] or YYYY/MM/DD HH:mm[:ss]
|
|
71
|
+
if (/^\d{4}[-\/]\d{2}[-\/]\d{2} \d{2}:\d{2}(:\d{2})?$/.test(str)) {
|
|
72
|
+
const [datePart, timePart] = str.split(" ");
|
|
73
|
+
const sep = datePart.includes("-") ? "-" : "/";
|
|
74
|
+
const [ys, ms, ds] = datePart.split(sep);
|
|
75
|
+
const [hs, mins, ss = "0"] = timePart.split(":");
|
|
76
|
+
const year = Number(ys);
|
|
77
|
+
const month = Number(ms);
|
|
78
|
+
const day = Number(ds);
|
|
79
|
+
const hour = Number(hs);
|
|
80
|
+
const minute = Number(mins);
|
|
81
|
+
const second = Number(ss);
|
|
82
|
+
const d = new Date(year, month - 1, day, hour, minute, second);
|
|
83
|
+
return d.getFullYear() === year &&
|
|
84
|
+
d.getMonth() + 1 === month &&
|
|
85
|
+
d.getDate() === day
|
|
86
|
+
? d
|
|
87
|
+
: null;
|
|
88
|
+
}
|
|
89
|
+
// YYYY-MM-DD or YYYY/MM/DD or YYYY.MM.DD
|
|
39
90
|
if (/^\d{4}[-\/.]\d{2}[-\/.]\d{2}$/.test(str)) {
|
|
40
91
|
const sep = str.includes("-") ? "-" : str.includes("/") ? "/" : ".";
|
|
41
92
|
const [ys, ms, ds] = str.split(sep);
|
|
@@ -52,7 +103,7 @@ const parseSupportedDate = (input) => {
|
|
|
52
103
|
const d = new Date(str);
|
|
53
104
|
return isNaN(d.getTime()) ? null : d;
|
|
54
105
|
};
|
|
55
|
-
const formatDate = (date, locale = "sv-SE", options
|
|
106
|
+
const formatDate = (date, locale = "sv-SE", options) => {
|
|
56
107
|
const formatter = new Intl.DateTimeFormat(locale, options);
|
|
57
108
|
return formatter.format(date);
|
|
58
109
|
};
|
|
@@ -62,36 +113,64 @@ const formatCurrency = (value, locale = "sv-SE", currency = "SEK", options = {})
|
|
|
62
113
|
return formatter.format(value);
|
|
63
114
|
};
|
|
64
115
|
// Dynamic formatter function
|
|
65
|
-
const formatValue = (value, type = "number", locale = "sv-SE", options
|
|
66
|
-
currency: "SEK",
|
|
67
|
-
}) => {
|
|
116
|
+
const formatValue = (value, type = "number", locale = "sv-SE", options) => {
|
|
68
117
|
if (type === "number") {
|
|
69
118
|
const num = parseFloat(value.toString());
|
|
70
|
-
return formatNumber(num, locale
|
|
119
|
+
return formatNumber(num, locale);
|
|
71
120
|
}
|
|
72
121
|
else if (type === "date") {
|
|
73
122
|
let date = "";
|
|
74
123
|
if (value instanceof Date) {
|
|
75
|
-
date = isNaN(value.getTime()) ? "" :
|
|
124
|
+
date = isNaN(value.getTime()) ? "" : toISODateTime(value);
|
|
76
125
|
}
|
|
77
126
|
if (typeof value === "number") {
|
|
78
127
|
const d = parseSupportedDate(value);
|
|
79
|
-
date = d ?
|
|
128
|
+
date = d ? toISODateTime(d) : "";
|
|
80
129
|
}
|
|
81
130
|
if (typeof value === "string") {
|
|
82
131
|
if (!(0, regexUtils_1.isValidDateFormat)(value))
|
|
83
132
|
return value;
|
|
84
133
|
const d = parseSupportedDate(value);
|
|
85
|
-
date = d ?
|
|
134
|
+
date = d ? toISODateTime(d) : "";
|
|
86
135
|
}
|
|
87
136
|
if (date) {
|
|
88
|
-
|
|
137
|
+
const { dateStyle, timeStyle, weekday, era, year, month, day, hour, minute, second, hour12, hourCycle, timeZone, timeZoneName, fractionalSecondDigits, } = options;
|
|
138
|
+
return formatDate(new Date(date).getTime(), locale, {
|
|
139
|
+
dateStyle,
|
|
140
|
+
timeStyle,
|
|
141
|
+
weekday,
|
|
142
|
+
era,
|
|
143
|
+
year,
|
|
144
|
+
month,
|
|
145
|
+
day,
|
|
146
|
+
hour,
|
|
147
|
+
minute,
|
|
148
|
+
second,
|
|
149
|
+
hour12,
|
|
150
|
+
hourCycle,
|
|
151
|
+
timeZone,
|
|
152
|
+
timeZoneName,
|
|
153
|
+
fractionalSecondDigits,
|
|
154
|
+
});
|
|
89
155
|
}
|
|
90
156
|
return value.toString();
|
|
91
157
|
}
|
|
92
158
|
else if (type === "currency") {
|
|
93
159
|
const num = parseFloat(value.toString());
|
|
94
|
-
|
|
160
|
+
const { currency, currencyDisplay, currencySign, notation, compactDisplay, minimumFractionDigits, maximumFractionDigits, minimumSignificantDigits, maximumSignificantDigits, useGrouping, roundingMode, roundingIncrement, } = options;
|
|
161
|
+
return formatCurrency(num, locale, currency, {
|
|
162
|
+
currencyDisplay,
|
|
163
|
+
currencySign,
|
|
164
|
+
notation,
|
|
165
|
+
compactDisplay,
|
|
166
|
+
minimumFractionDigits,
|
|
167
|
+
maximumFractionDigits,
|
|
168
|
+
minimumSignificantDigits,
|
|
169
|
+
maximumSignificantDigits,
|
|
170
|
+
useGrouping,
|
|
171
|
+
roundingMode,
|
|
172
|
+
roundingIncrement,
|
|
173
|
+
});
|
|
95
174
|
}
|
|
96
175
|
else {
|
|
97
176
|
return value.toString();
|
package/package.json
CHANGED