@megha-ui/react 1.2.135 → 1.2.137

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.
@@ -3,8 +3,8 @@ 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", options) => {
7
+ const formatter = new Intl.NumberFormat(locale, options);
8
8
  return formatter.format(value);
9
9
  };
10
10
  // ISO-like string including local time (no timezone)
@@ -107,16 +107,62 @@ const formatDate = (date, locale = "sv-SE", options) => {
107
107
  const formatter = new Intl.DateTimeFormat(locale, options);
108
108
  return formatter.format(date);
109
109
  };
110
- // Function to format currency
111
- const formatCurrency = (value, locale = "sv-SE", currency = "SEK", options = {}) => {
112
- const formatter = new Intl.NumberFormat(locale, Object.assign({ style: "currency", currency: currency }, options));
113
- return formatter.format(value);
110
+ // Helpers for currency formatting
111
+ const isValidCurrencyCode = (code) => typeof code === "string" && /^[A-Z]{3}$/.test(code);
112
+ const buildCurrencyOptions = (currency, options) => {
113
+ const { currencyDisplay, currencySign, notation, compactDisplay, minimumFractionDigits, maximumFractionDigits, minimumSignificantDigits, maximumSignificantDigits, useGrouping, roundingMode, roundingIncrement, } = options !== null && options !== void 0 ? options : {};
114
+ return {
115
+ style: "currency",
116
+ currency,
117
+ currencyDisplay,
118
+ currencySign,
119
+ notation,
120
+ compactDisplay,
121
+ minimumFractionDigits,
122
+ maximumFractionDigits,
123
+ minimumSignificantDigits,
124
+ maximumSignificantDigits,
125
+ useGrouping,
126
+ // The following are newer options; some engines ignore them, some may throw.
127
+ // We'll include them here, but handle failures via try/catch and fallback.
128
+ // @ts-ignore
129
+ roundingMode,
130
+ // @ts-ignore
131
+ roundingIncrement,
132
+ };
133
+ };
134
+ // Function to format currency with robust fallbacks
135
+ const formatCurrency = (value, locale = "sv-SE", currency = "SEK", options) => {
136
+ const code = isValidCurrencyCode(currency) ? currency : "SEK";
137
+ // 1) Try as requested
138
+ try {
139
+ const fmt = new Intl.NumberFormat(locale, buildCurrencyOptions(code, options));
140
+ return fmt.format(value);
141
+ }
142
+ catch (_) {
143
+ // 2) Fallback if currencyDisplay: 'name' is problematic or other options unsupported
144
+ try {
145
+ const safe = Object.assign({}, (options !== null && options !== void 0 ? options : {}));
146
+ if (safe.currencyDisplay === "name")
147
+ safe.currencyDisplay = "code";
148
+ // Drop less-supported options that may throw
149
+ delete safe.roundingMode;
150
+ delete safe.roundingIncrement;
151
+ const fmt = new Intl.NumberFormat(locale, buildCurrencyOptions(code, safe));
152
+ return fmt.format(value);
153
+ }
154
+ catch (_a) {
155
+ // 3) Final fallback: minimal currency formatting
156
+ const fmt = new Intl.NumberFormat(locale, { style: "currency", currency: code });
157
+ return fmt.format(value);
158
+ }
159
+ }
114
160
  };
115
161
  // Dynamic formatter function
116
162
  const formatValue = (value, type = "number", locale = "sv-SE", options) => {
117
163
  if (type === "number") {
118
164
  const num = parseFloat(value.toString());
119
- return formatNumber(num, locale);
165
+ return formatNumber(num, locale, { useGrouping: false });
120
166
  }
121
167
  else if (type === "date") {
122
168
  let date = "";
@@ -157,20 +203,8 @@ const formatValue = (value, type = "number", locale = "sv-SE", options) => {
157
203
  }
158
204
  else if (type === "currency") {
159
205
  const num = parseFloat(value.toString());
160
- const { currency, currencyDisplay, currencySign, notation, compactDisplay, minimumFractionDigits, maximumFractionDigits, minimumSignificantDigits, maximumSignificantDigits, useGrouping, roundingMode, roundingIncrement, } = options !== null && options !== void 0 ? 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
- });
206
+ const { currency } = options !== null && options !== void 0 ? options : {};
207
+ return formatCurrency(num, locale, currency, options);
174
208
  }
175
209
  else {
176
210
  return value.toString();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@megha-ui/react",
3
- "version": "1.2.135",
3
+ "version": "1.2.137",
4
4
  "description": "A collection of reusable UI components for React applications, built with TypeScript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",