Skip to content

Commit be73ee3

Browse files
calvarezgclaude
andcommitted
triage: guard against non-date bar values in sentiment transform (#3594141060)
A non-date `bar` string (e.g. a metadata/"N/A" row) sliced to junk that dateToIsoWeek turned into a "NaN-WNaN" map key, surfacing as a phantom weeklyTrends entry (weekNumber/year 0). Validate the day against YYYY-MM-DD before dateToIsoWeek in both the data and line loops via a shared isoDayOf helper. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent a1209a3 commit be73ee3

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/support/elements/definitions/sentiment-overview.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ function parseIsoWeekParts(weekStr) {
103103

104104
const SENTIMENT_BUCKETS = ['positive', 'neutral', 'negative'];
105105

106+
// Guard against a non-date `bar` value (e.g. a metadata / "N/A" row from the upstream
107+
// element): an invalid day slices to junk that dateToIsoWeek turns into a "NaN-WNaN"
108+
// key, which would surface as a phantom weeklyTrends entry (weekNumber/year 0). Only a
109+
// real YYYY-MM-DD prefix is allowed through.
110+
const YMD_RE = /^\d{4}-\d{2}-\d{2}$/;
111+
function isoDayOf(bar) {
112+
const day = typeof bar === 'string' ? bar.slice(0, 10) : '';
113+
return YMD_RE.test(day) ? day : null;
114+
}
115+
106116
/**
107117
* Transforms the raw Sentiment element response into the legacy Brand Presence
108118
* `sentiment-overview` contract so the sentiment chart is drop-in compatible:
@@ -156,7 +166,7 @@ export function transformSentimentOverviewResponse(raw) {
156166
};
157167

158168
rows.forEach((row) => {
159-
const day = typeof row?.bar === 'string' ? row.bar.slice(0, 10) : null;
169+
const day = isoDayOf(row?.bar);
160170
const bucket = typeof row?.legend === 'string' ? row.legend.toLowerCase().trim() : '';
161171
if (!day || !SENTIMENT_BUCKETS.includes(bucket)) {
162172
return;
@@ -168,7 +178,7 @@ export function transformSentimentOverviewResponse(raw) {
168178

169179
// The total-prompts line is a separate per-day series; fold it into the same weeks.
170180
lineRows.forEach((row) => {
171-
const day = typeof row?.bar === 'string' ? row.bar.slice(0, 10) : null;
181+
const day = isoDayOf(row?.bar);
172182
if (!day) {
173183
return;
174184
}

0 commit comments

Comments
 (0)