codeant-ai-for-open-source[bot] commented on code in PR #37229:
URL: https://github.com/apache/superset/pull/37229#discussion_r4333121713
##########
superset-frontend/plugins/plugin-chart-echarts/src/utils/forecast.ts:
##########
@@ -26,6 +26,147 @@ import {
} from '../types';
import { sanitizeHtml } from './series';
+/**
+ * Escapes RegExp metacharacters in a string so it can be safely used in a
+ * dynamically created regular expression.
+ *
+ * @param value - The raw string to escape
+ * @returns The escaped string safe for use in `new RegExp(...)`
+ */
+const escapeRegex = (value: string) =>
+ value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+
+/**
+ * Replaces a label inside a compound key only if it appears as a complete
+ * word/token and contains at least one alphabetic character.
+ *
+ * @param key - The source string (typically a compound field name)
+ * @param label - The label to search for as a standalone token
+ * @param replacement - The human-readable value to replace the label with
+ * @returns The transformed key if a valid match exists, otherwise the
original key
+ */
+const replaceLabelIfExists = (
+ key: string,
+ label: string,
+ replacement: string,
+) => {
+ /**
+ * Logic:
+ *
+ * This function is intentionally stricter than a simple substring replace:
+ * - The label must NOT be part of a larger word (e.g. "account" will NOT
match
+ * "testing_account").
+ * - Underscores (`_`) are treated as part of the word.
+ * - Numeric-only matches are ignored (e.g. "12" will NOT match "123").
+ *
+ * If the label is found, only the matched portion is replaced; otherwise,
+ * the original key is returned unchanged.
+ *
+ * Examples:
+ * - replaceLabelIfExists("testing_account 123", "testing_account",
"Account")
+ * → "Account 123"
+ * - replaceLabelIfExists("testing_account 123", "account", "Account")
+ * → "testing_account 123"
+ * - replaceLabelIfExists("123", "12", "X")
+ * → "123"
+ */
+
+ if (key === label) {
+ return replacement;
+ }
+
+ const escapedLabel = escapeRegex(label);
+ const regex = new RegExp(`(?<!\\w)${escapedLabel}(?!\\w)`, 'g');
+ return regex.test(key) ? key.replace(regex, replacement) : key;
Review Comment:
✅ **CodeAnt verified this suggestion was addressed in subsequent commits and
marked this thread resolved** as of `69888e6`.
The replacement-behavior documentation no longer claims numeric-only matches
are ignored.
<sub>If that's not right, unresolve this thread and CodeAnt will leave it
open.</sub>
<!-- codeant-auto-resolve-reply -->
--
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]