amaannawab923 commented on code in PR #36416:
URL: https://github.com/apache/superset/pull/36416#discussion_r2665755385
##########
superset-frontend/packages/superset-ui-core/src/currency-format/CurrencyFormatter.ts:
##########
@@ -41,6 +45,38 @@ export const getCurrencySymbol = (currency:
Partial<Currency>) =>
.formatToParts(1)
.find(x => x.type === 'currency')?.value;
+/** Normalize currency to ISO 4217 format (e.g., "USD"). Returns null if
invalid. */
+export function normalizeCurrency(
+ value: string | null | undefined,
+): string | null {
+ if (!value) return null;
+
+ const str = value.toString().trim();
+ if (!str) return null;
+
+ const upper = str.toUpperCase();
+
+ if (/^[A-Z]{3}$/.test(upper)) return upper;
+
+ return null;
+}
+
+/** Check if array contains multiple distinct currencies (after
normalization). */
+export function hasMixedCurrencies(
+ currencies: (string | null | undefined)[],
+): boolean {
+ // Filter out null/undefined and normalize
+ const normalized = currencies
+ .map(c => normalizeCurrency(c))
+ .filter((c): c is string => c !== null);
+
+ if (normalized.length === 0) return false;
+
+ // Check if all normalized currencies are the same
+ const first = normalized[0];
+ return !normalized.every(c => c === first);
+}
Review Comment:
Just a suggestion , this function can be imrpoved with an early exit , See
if it fits fine
export function hasMixedCurrencies(
currencies: (string | null | undefined)[],
): boolean {
let first: string | null = null;
for (const c of currencies) {
const normalized = normalizeCurrency(c);
if (normalized === null) continue;
if (first === null) {
first = normalized;
} else if (normalized !== first) {
return true;
}
}
return false;
}
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]