Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

feat(profile): add resume copy title helpers

Khuram Niaz67108febc9aa
diff --git a/__tests__/resumeCopyTitle.spec.ts b/__tests__/resumeCopyTitle.spec.ts
new file mode 100644
index 0000000..7bae4f7
--- /dev/null
+++ b/__tests__/resumeCopyTitle.spec.ts
@@ -0,0 +1,60 @@
+import { buildCopyTitle, ensureUniqueTitle } from "@/lib/resumeCopyTitle";
+
+describe("buildCopyTitle", () => {
+ it("appends (Copy) when nothing is taken", () => {
+ expect(buildCopyTitle("Backend Resume", [])).toBe("Backend Resume (Copy)");
+ });
+
+ it("numbers subsequent copies", () => {
+ expect(buildCopyTitle("Backend Resume", ["Backend Resume (Copy)"])).toBe(
+ "Backend Resume (Copy 2)",
+ );
+ expect(
+ buildCopyTitle("Backend Resume", [
+ "Backend Resume (Copy)",
+ "Backend Resume (Copy 2)",
+ ]),
+ ).toBe("Backend Resume (Copy 3)");
+ });
+
+ it("compares case-insensitively and ignores surrounding whitespace", () => {
+ expect(buildCopyTitle(" Backend Resume ", ["backend resume (COPY)"])).toBe(
+ "Backend Resume (Copy 2)",
+ );
+ });
+
+ it("strips an existing copy suffix so titles do not nest", () => {
+ expect(buildCopyTitle("Backend Resume (Copy 2)", [])).toBe(
+ "Backend Resume (Copy)",
+ );
+ expect(
+ buildCopyTitle("Backend Resume (Copy)", ["Backend Resume (Copy)"]),
+ ).toBe("Backend Resume (Copy 2)");
+ });
+
+ it("truncates the base so the result fits in 100 characters", () => {
+ const long = "A".repeat(120);
+ const result = buildCopyTitle(long, []);
+ expect(result.length).toBeLessThanOrEqual(100);
+ expect(result.endsWith(" (Copy)")).toBe(true);
+ });
+});
+
+describe("ensureUniqueTitle", () => {
+ it("returns the title unchanged when free", () => {
+ expect(ensureUniqueTitle("My Resume", ["Other"])).toBe("My Resume");
+ });
+
+ it("appends an incrementing counter when taken", () => {
+ expect(ensureUniqueTitle("My Resume", ["my resume"])).toBe("My Resume (2)");
+ expect(ensureUniqueTitle("My Resume", ["My Resume", "My Resume (2)"])).toBe(
+ "My Resume (3)",
+ );
+ });
+
+ it("truncates so the result fits in 100 characters", () => {
+ const long = "B".repeat(120);
+ const result = ensureUniqueTitle(long, [long.slice(0, 100)]);
+ expect(result.length).toBeLessThanOrEqual(100);
+ });
+});
diff --git a/src/lib/resumeCopyTitle.ts b/src/lib/resumeCopyTitle.ts
new file mode 100644
index 0000000..65b1245
--- /dev/null
+++ b/src/lib/resumeCopyTitle.ts
@@ -0,0 +1,42 @@
+const MAX_TITLE_LENGTH = 100;
+
+// Matches a trailing " (Copy)" / " (Copy 3)" so copying a copy does not nest
+const COPY_SUFFIX = /\s*\(copy(?:\s+\d+)?\)$/i;
+
+const normalize = (value: string): string => value.trim().toLowerCase();
+
+// Trim the base so `${base}${suffix}` still fits the title column limit
+const fit = (base: string, suffix: string): string => {
+ const room = MAX_TITLE_LENGTH - suffix.length;
+ return `${base.slice(0, Math.max(room, 0)).trimEnd()}${suffix}`;
+};
+
+export const buildCopyTitle = (
+ sourceTitle: string,
+ takenTitles: string[],
+): string => {
+ const base = sourceTitle.trim().replace(COPY_SUFFIX, "").trim();
+ const taken = new Set(takenTitles.map(normalize));
+
+ let candidate = fit(base, " (Copy)");
+ let counter = 2;
+ while (taken.has(normalize(candidate))) {
+ candidate = fit(base, ` (Copy ${counter++})`);
+ }
+ return candidate;
+};
+
+export const ensureUniqueTitle = (
+ desiredTitle: string,
+ takenTitles: string[],
+): string => {
+ const base = desiredTitle.trim();
+ const taken = new Set(takenTitles.map(normalize));
+
+ let candidate = fit(base, "");
+ let counter = 2;
+ while (taken.has(normalize(candidate))) {
+ candidate = fit(base, ` (${counter++})`);
+ }
+ return candidate;
+};