fix: improve updatehostineventname function (#21734)
Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com>
This commit is contained in:
co-authored by
Syed Ali Shahbaz
parent
dd37caf32c
commit
f0f168e920
@@ -2,6 +2,7 @@ import type { TFunction } from "i18next";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import * as event from "./event";
|
||||
import { updateHostInEventName } from "./event";
|
||||
|
||||
describe("event tests", () => {
|
||||
describe("fn: getEventName", () => {
|
||||
@@ -423,4 +424,128 @@ describe("event tests", () => {
|
||||
expect(event.validateCustomEventName("foo{nonsenseField}bar")).toBe("{nonsenseField}");
|
||||
});
|
||||
});
|
||||
|
||||
describe("fn: updateHostInEventName", () => {
|
||||
const oldHost = "John Doe";
|
||||
const newHost = "Jane Smith";
|
||||
|
||||
it("should replace full host name with spaces", () => {
|
||||
const eventName = "Meeting with John Doe";
|
||||
const result = updateHostInEventName(eventName, oldHost, newHost);
|
||||
expect(result).toBe("Meeting with Jane Smith");
|
||||
});
|
||||
|
||||
it("should replace full host name with dots", () => {
|
||||
const eventName = "Meeting with John.Doe";
|
||||
const result = updateHostInEventName(eventName, oldHost, newHost);
|
||||
expect(result).toBe("Meeting with Jane.Smith");
|
||||
});
|
||||
|
||||
it("should replace full host name with hyphens", () => {
|
||||
const eventName = "Meeting with John-Doe";
|
||||
const result = updateHostInEventName(eventName, oldHost, newHost);
|
||||
expect(result).toBe("Meeting with Jane-Smith");
|
||||
});
|
||||
|
||||
it("should replace full host name with underscores", () => {
|
||||
const eventName = "Meeting with John_Doe";
|
||||
const result = updateHostInEventName(eventName, oldHost, newHost);
|
||||
expect(result).toBe("Meeting with Jane_Smith");
|
||||
});
|
||||
|
||||
it("should replace first name only", () => {
|
||||
const eventName = "John's presentation";
|
||||
const result = updateHostInEventName(eventName, oldHost, newHost);
|
||||
expect(result).toBe("Jane's presentation");
|
||||
});
|
||||
|
||||
it("should replace first name at the beginning", () => {
|
||||
const eventName = "John will present tomorrow";
|
||||
const result = updateHostInEventName(eventName, oldHost, newHost);
|
||||
expect(result).toBe("Jane will present tomorrow");
|
||||
});
|
||||
|
||||
it("should replace first name at the end", () => {
|
||||
const eventName = "Presentation by John";
|
||||
const result = updateHostInEventName(eventName, oldHost, newHost);
|
||||
expect(result).toBe("Presentation by Jane");
|
||||
});
|
||||
|
||||
it("should handle different cases", () => {
|
||||
const eventName = "meeting with john.doe";
|
||||
const result = updateHostInEventName(eventName, oldHost, newHost);
|
||||
expect(result).toBe("meeting with Jane.Smith");
|
||||
});
|
||||
|
||||
it("should handle mixed cases", () => {
|
||||
const eventName = "Meeting with JOHN DOE";
|
||||
const result = updateHostInEventName(eventName, oldHost, newHost);
|
||||
expect(result).toBe("Meeting with Jane Smith");
|
||||
});
|
||||
|
||||
it("should not replace partial matches", () => {
|
||||
const eventName = "Meeting with Johnson";
|
||||
const result = updateHostInEventName(eventName, oldHost, newHost);
|
||||
expect(result).toBe("Meeting with Johnson");
|
||||
});
|
||||
|
||||
it("should handle empty event name", () => {
|
||||
const result = updateHostInEventName("", oldHost, newHost);
|
||||
expect(result).toBe("");
|
||||
});
|
||||
|
||||
it("should handle event name without host", () => {
|
||||
const eventName = "Team meeting";
|
||||
const result = updateHostInEventName(eventName, oldHost, newHost);
|
||||
expect(result).toBe("Team meeting");
|
||||
});
|
||||
|
||||
it("should handle single word names", () => {
|
||||
const eventName = "Meeting with John";
|
||||
const result = updateHostInEventName(eventName, "John", "Jane");
|
||||
expect(result).toBe("Meeting with Jane");
|
||||
});
|
||||
|
||||
it("should handle names with multiple parts", () => {
|
||||
const eventName = "Meeting with John van der Berg";
|
||||
const result = updateHostInEventName(eventName, "John van der Berg", "Jane Smith");
|
||||
expect(result).toBe("Meeting with Jane Smith");
|
||||
});
|
||||
|
||||
it("should handle names with apostrophes", () => {
|
||||
const eventName = "Meeting with John O'Connor";
|
||||
const result = updateHostInEventName(eventName, "John O'Connor", "Jane Smith");
|
||||
expect(result).toBe("Meeting with Jane Smith");
|
||||
});
|
||||
|
||||
it("should handle names with special characters", () => {
|
||||
const eventName = "Meeting with Jean-Pierre Dupont";
|
||||
const result = updateHostInEventName(eventName, "Jean-Pierre Dupont", "Jane Smith");
|
||||
expect(result).toBe("Meeting with Jane Smith");
|
||||
});
|
||||
|
||||
it("should prioritize full name over first name", () => {
|
||||
const eventName = "John.Doe and John are coming";
|
||||
const result = updateHostInEventName(eventName, "John Doe", "Jane Smith");
|
||||
expect(result).toBe("Jane.Smith and John are coming");
|
||||
});
|
||||
|
||||
it("should only replace first match", () => {
|
||||
const eventName = "John.Doe and John Doe meeting";
|
||||
const result = updateHostInEventName(eventName, "John Doe", "Jane Smith");
|
||||
expect(result).toBe("Jane.Smith and John Doe meeting");
|
||||
});
|
||||
|
||||
it("should handle different new name structure", () => {
|
||||
const eventName = "Meeting with John.Doe";
|
||||
const result = updateHostInEventName(eventName, "John Doe", "Jane");
|
||||
expect(result).toBe("Meeting with Jane");
|
||||
});
|
||||
|
||||
it("should handle multiple occurrences with priority", () => {
|
||||
const eventName = "John and John.Doe and John_Doe";
|
||||
const result = updateHostInEventName(eventName, oldHost, newHost);
|
||||
expect(result).toBe("John and Jane.Smith and John_Doe");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+45
-7
@@ -111,18 +111,56 @@ export function getEventName(eventNameObj: EventNameObjectType, forAttendeeView
|
||||
}
|
||||
|
||||
export function updateHostInEventName(eventName: string, oldHost: string, newHost: string) {
|
||||
const oldHostFirstName = oldHost.split(" ")[0];
|
||||
const newHostFirstName = newHost.split(" ")[0];
|
||||
const oldParts = oldHost.trim().split(/\s+/);
|
||||
const newParts = newHost.trim().split(/\s+/);
|
||||
|
||||
if (!eventName.includes(oldHost) && !eventName.includes(oldHostFirstName)) {
|
||||
return eventName;
|
||||
}
|
||||
// Handle cases where names might have different number of parts
|
||||
const oldFirst = oldParts[0];
|
||||
const oldLast = oldParts.slice(1).join(" ");
|
||||
const newFirst = newParts[0];
|
||||
const newLast = newParts.slice(1).join(" ");
|
||||
|
||||
const formats = [
|
||||
// Full name patterns (prioritize these first)
|
||||
...(oldLast
|
||||
? [
|
||||
{
|
||||
pattern: `${oldFirst}.${oldLast}`,
|
||||
replacement: newLast ? `${newFirst}.${newLast}` : newFirst,
|
||||
},
|
||||
{
|
||||
pattern: `${oldFirst}-${oldLast}`,
|
||||
replacement: newLast ? `${newFirst}-${newLast}` : newFirst,
|
||||
},
|
||||
{
|
||||
pattern: `${oldFirst}_${oldLast}`,
|
||||
replacement: newLast ? `${newFirst}_${newLast}` : newFirst,
|
||||
},
|
||||
{
|
||||
pattern: `${oldFirst} ${oldLast}`,
|
||||
replacement: newLast ? `${newFirst} ${newLast}` : newFirst,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
// First name only (last to avoid partial matches)
|
||||
{
|
||||
pattern: oldFirst,
|
||||
replacement: newFirst,
|
||||
},
|
||||
];
|
||||
|
||||
let updatedEventName = eventName;
|
||||
|
||||
updatedEventName = updatedEventName.replace(oldHost, newHost);
|
||||
for (const { pattern, replacement } of formats) {
|
||||
// Escape special regex characters in the pattern
|
||||
const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
const regex = new RegExp(`\\b${escapedPattern}\\b`, "gi");
|
||||
|
||||
updatedEventName = updatedEventName.replace(oldHostFirstName, newHostFirstName);
|
||||
if (regex.test(updatedEventName)) {
|
||||
updatedEventName = updatedEventName.replace(regex, replacement);
|
||||
return updatedEventName;
|
||||
}
|
||||
}
|
||||
|
||||
return updatedEventName;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user