From 267e6073cb9863f8bcef8ea0cf0c07d086d5792f Mon Sep 17 00:00:00 2001 From: 7w1 Date: Thu, 19 Mar 2026 22:09:41 -0500 Subject: [PATCH 1/3] stricter regex --- src/app/utils/pronouns.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/utils/pronouns.ts b/src/app/utils/pronouns.ts index 943547a0b..0af478d7f 100644 --- a/src/app/utils/pronouns.ts +++ b/src/app/utils/pronouns.ts @@ -89,7 +89,8 @@ export function getParsedPronouns(rawName: string, parseSetting: boolean) { } // match text like (she/her) or [he/they/them] but not (hi) - const regex = /[([]?([a-zA-Z]+\/[a-zA-Z]+(?:\/[a-zA-Z]+)?)[)\]]?/; + const regex = + /(?:\(([a-zA-Z]+\/[a-zA-Z]+(?:\/[a-zA-Z]+)?)\)|\[([a-zA-Z]+\/[a-zA-Z]+(?:\/[a-zA-Z]+)?)\])/; const match = rawName.match(regex); let result: { cleanedDisplayName: string; inlinePronoun: string | null } = { From 6334cce92cad7e83c9aee71a408fa8c7db9af892 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Thu, 19 Mar 2026 22:16:11 -0500 Subject: [PATCH 2/3] various checks and things --- src/app/utils/pronouns.ts | 80 +++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/src/app/utils/pronouns.ts b/src/app/utils/pronouns.ts index 0af478d7f..257b2a6a8 100644 --- a/src/app/utils/pronouns.ts +++ b/src/app/utils/pronouns.ts @@ -4,8 +4,9 @@ export type PronounSet = { grammatical_gender?: string; }; -// helper function to convert a comma-separated pronouns string into an array of objects with summary and optional language -export function parsePronounsInput(pronouns: string): { summary: string; language?: string }[] { +export function parsePronounsInput( + pronouns?: string | null +): { summary: string; language?: string }[] { if (!pronouns || typeof pronouns !== 'string') return []; return pronouns @@ -17,61 +18,51 @@ export function parsePronounsInput(pronouns: string): { summary: string; languag if (parts.length === 1) { return { - summary: (parts[0] || '').slice(0, 16), + summary: (parts[0] ?? '').trim().slice(0, 16), language: 'en', }; } const [language, summary] = parts; - return { - language: (language || 'en').trim() || 'en', - summary: (summary || '').trim().slice(0, 16), + language: (language ?? 'en').trim().toLowerCase() || 'en', + summary: (summary ?? '').trim().slice(0, 16), }; }); } -export function parsePronounsStringToPronounsSetArray(pronouns: string): PronounSet[] { - return parsePronounsInput(pronouns) as unknown as PronounSet[]; +export function parsePronounsStringToPronounsSetArray(pronouns?: string | null): PronounSet[] { + return parsePronounsInput(pronouns) as PronounSet[]; } -// helper function to filter a list of pronouns based on the user's language settings export function filterPronounsByLanguage( - pronouns: { summary: string; language?: string }[], - enabled: boolean, - languages: string[] + pronouns?: { summary: string; language?: string }[] | null, + enabled?: boolean, + languages?: (string | null | undefined)[] ): { summary: string; language?: string }[] { if (!Array.isArray(pronouns)) return []; + const sanitize = (p: { summary: string; language?: string }) => ({ + ...p, + summary: (p?.summary ?? '').slice(0, 16), + }); + if (!enabled) { - return pronouns.map((p) => ({ - ...p, - summary: (p?.summary || '').slice(0, 16), - })); + return pronouns.map(sanitize); } - const normalizedLanguages = (languages || []) - .filter(Boolean) + const normalizedLanguages = (languages ?? []) + .filter((lang): lang is string => typeof lang === 'string') .map((lang) => lang.trim().toLowerCase()); const filteredPronouns = pronouns .filter((p) => { - const lang = (p?.language || 'en').trim().toLowerCase(); + const lang = (p?.language ?? 'en').trim().toLowerCase(); return normalizedLanguages.includes(lang); }) - .map((p) => ({ - ...p, - summary: (p?.summary || '').slice(0, 16), - })); + .map(sanitize); - if (filteredPronouns.length === 0) { - return pronouns.map((p) => ({ - ...p, - summary: (p?.summary || '').slice(0, 16), - })); - } - - return filteredPronouns; + return filteredPronouns.length > 0 ? filteredPronouns : pronouns.map(sanitize); } const pronounParseCache = new Map< @@ -79,33 +70,32 @@ const pronounParseCache = new Map< { cleanedDisplayName: string; inlinePronoun: string | null } >(); -export function getParsedPronouns(rawName: string, parseSetting: boolean) { - if (!parseSetting || !rawName) { - return { cleanedDisplayName: rawName, inlinePronoun: null }; +export function getParsedPronouns(rawName?: string | null, parseSetting?: boolean) { + if (!parseSetting || !rawName || typeof rawName !== 'string') { + return { cleanedDisplayName: rawName ?? '', inlinePronoun: null }; } - if (pronounParseCache.has(rawName)) { - return pronounParseCache.get(rawName)!; - } + const cached = pronounParseCache.get(rawName); + if (cached) return cached; - // match text like (she/her) or [he/they/them] but not (hi) const regex = /(?:\(([a-zA-Z]+\/[a-zA-Z]+(?:\/[a-zA-Z]+)?)\)|\[([a-zA-Z]+\/[a-zA-Z]+(?:\/[a-zA-Z]+)?)\])/; const match = rawName.match(regex); let result: { cleanedDisplayName: string; inlinePronoun: string | null } = { - cleanedDisplayName: rawName, + cleanedDisplayName: rawName.trim(), inlinePronoun: null, }; if (match) { - let strippedName = rawName.replace(match[0], '').trim(); - if (!strippedName || strippedName === '') { - strippedName = rawName; - } + const fullMatch = match[0]; + const capturedPronoun = (match[1] ?? match[2] ?? '').toLowerCase().slice(0, 16); + + const strippedName = rawName.replace(fullMatch, '').trim(); + result = { - cleanedDisplayName: strippedName, - inlinePronoun: match[1].toLowerCase().slice(0, 16), + cleanedDisplayName: strippedName || rawName, + inlinePronoun: capturedPronoun || null, }; } From 1cd3850a26b3416f034e13375044dc6970414df3 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Thu, 19 Mar 2026 22:18:52 -0500 Subject: [PATCH 3/3] lint stuff --- src/app/components/user-profile/UserRoomProfile.tsx | 2 +- src/app/utils/pronouns.test.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/app/components/user-profile/UserRoomProfile.tsx b/src/app/components/user-profile/UserRoomProfile.tsx index 5d0f9e7e3..e11deaa83 100644 --- a/src/app/components/user-profile/UserRoomProfile.tsx +++ b/src/app/components/user-profile/UserRoomProfile.tsx @@ -95,7 +95,7 @@ function UserExtendedSection({ const languagesToFilterFor = getSettings().filterPronounsLanguages ?? ['en']; const pronouns = filterPronounsByLanguage( - profile.pronouns!, + profile.pronouns, languageFilterEnabled, languagesToFilterFor ) diff --git a/src/app/utils/pronouns.test.ts b/src/app/utils/pronouns.test.ts index e0471ce64..d31361ba5 100644 --- a/src/app/utils/pronouns.test.ts +++ b/src/app/utils/pronouns.test.ts @@ -83,7 +83,6 @@ describe('filterPronounsByLanguage', () => { }); it('returns empty array for non-array input', () => { - // @ts-expect-error — testing runtime safety expect(filterPronounsByLanguage(null, true, ['en'])).toEqual([]); }); });