mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-07-13 06:24:54 +10:00
refactor(import): add v4section/v4url aliases, inline clamp wrappers, replace classes with fns
Finding 5: Introduce V4Url + V4Section<T> type aliases in reactive-resume-v4-json.tsx; collapse
310-line V4ResumeData type (13x 5-field section header, 11x {label,href}) into typed aliases.
Inferred shape is structurally identical.
Finding 6: Remove 9 single-caller clamp/pxToPt wrappers; replace compile-time constants
(rotation=0, sidebarWidth=35, shadowWidth=0, gapX=4, gapY=6) with literals; inline dynamic
calls (clamp(x, 32, 512) etc.) at the two body/heading typography call sites.
Finding 7: Convert three stateless single-method importer classes to plain functions.
Extract shared rethrowAsImportError() to error.ts, replacing triplicated ZodError catch blocks.
Update web import dialog + all in-package test files to use function API.
Claude-Session: https://claude.ai/code/session_012Bnvt1MghwHj4qQRxuQUGa
This commit is contained in:
@@ -9,9 +9,9 @@ import { Link, useNavigate } from "@tanstack/react-router";
|
||||
import { useRef, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import z from "zod";
|
||||
import { JSONResumeImporter } from "@reactive-resume/import/json-resume";
|
||||
import { ReactiveResumeJSONImporter } from "@reactive-resume/import/reactive-resume-json";
|
||||
import { ReactiveResumeV4JSONImporter } from "@reactive-resume/import/reactive-resume-v4-json";
|
||||
import { parseJSONResume } from "@reactive-resume/import/json-resume";
|
||||
import { parseReactiveResumeJSON } from "@reactive-resume/import/reactive-resume-json";
|
||||
import { parseReactiveResumeV4JSON } from "@reactive-resume/import/reactive-resume-v4-json";
|
||||
import { Badge } from "@reactive-resume/ui/components/badge";
|
||||
import { Button } from "@reactive-resume/ui/components/button";
|
||||
import {
|
||||
@@ -167,21 +167,15 @@ export function ImportResumeDialog(_: DialogProps<"resume.import">) {
|
||||
let data: ResumeData | undefined;
|
||||
|
||||
if (value.type === "json-resume-json") {
|
||||
const json = await value.file.text();
|
||||
const importer = new JSONResumeImporter();
|
||||
data = importer.parse(json);
|
||||
data = parseJSONResume(await value.file.text());
|
||||
}
|
||||
|
||||
if (value.type === "reactive-resume-json") {
|
||||
const json = await value.file.text();
|
||||
const importer = new ReactiveResumeJSONImporter();
|
||||
data = importer.parse(json);
|
||||
data = parseReactiveResumeJSON(await value.file.text());
|
||||
}
|
||||
|
||||
if (value.type === "reactive-resume-v4-json") {
|
||||
const json = await value.file.text();
|
||||
const importer = new ReactiveResumeV4JSONImporter();
|
||||
data = importer.parse(json);
|
||||
data = parseReactiveResumeV4JSON(await value.file.text());
|
||||
}
|
||||
|
||||
if (value.type === "pdf") {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { flattenError, ZodError } from "zod";
|
||||
|
||||
/** Rethrows a ZodError as a serialized string error; re-throws other errors as-is. */
|
||||
export function rethrowAsImportError(error: unknown): never {
|
||||
if (error instanceof ZodError) {
|
||||
throw new Error(JSON.stringify(flattenError(error)));
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
@@ -1,22 +1,20 @@
|
||||
// biome-ignore-all lint/style/noNonNullAssertion: These tests assert imported section lengths before inspecting the first item.
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { JSONResumeImporter } from "./json-resume";
|
||||
import { parseJSONResume } from "./json-resume";
|
||||
|
||||
const importer = new JSONResumeImporter();
|
||||
|
||||
describe("JSONResumeImporter.parse", () => {
|
||||
describe("parseJSONResume", () => {
|
||||
it("throws when input is not valid JSON", () => {
|
||||
expect(() => importer.parse("not json")).toThrow();
|
||||
expect(() => parseJSONResume("not json")).toThrow();
|
||||
});
|
||||
|
||||
it("throws a serialized validation error for an obviously invalid shape", () => {
|
||||
// Email field is validated; a non-email string should fail the loose schema.
|
||||
const invalid = JSON.stringify({ basics: { email: "not-an-email" } });
|
||||
expect(() => importer.parse(invalid)).toThrow();
|
||||
expect(() => parseJSONResume(invalid)).toThrow();
|
||||
});
|
||||
|
||||
it("imports an empty JSON Resume into a baseline ResumeData", () => {
|
||||
const result = importer.parse("{}");
|
||||
const result = parseJSONResume("{}");
|
||||
// Defaults preserve a name field even when unset by input.
|
||||
expect(typeof result.basics.name).toBe("string");
|
||||
});
|
||||
@@ -34,7 +32,7 @@ describe("JSONResumeImporter.parse", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
const result = parseJSONResume(json);
|
||||
expect(result.basics.name).toBe("Jane Doe");
|
||||
expect(result.basics.headline).toBe("Engineer");
|
||||
expect(result.basics.email).toBe("jane@example.com");
|
||||
@@ -62,7 +60,7 @@ describe("JSONResumeImporter.parse", () => {
|
||||
],
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
const result = parseJSONResume(json);
|
||||
expect(result.sections.experience.items).toHaveLength(1);
|
||||
|
||||
const item = result.sections.experience.items[0]!;
|
||||
@@ -89,7 +87,7 @@ describe("JSONResumeImporter.parse", () => {
|
||||
],
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
const result = parseJSONResume(json);
|
||||
expect(result.sections.education.items).toHaveLength(1);
|
||||
|
||||
const edu = result.sections.education.items[0]!;
|
||||
@@ -103,7 +101,7 @@ describe("JSONResumeImporter.parse", () => {
|
||||
skills: [{ name: "TypeScript", level: "Master", keywords: ["node", "react"] }],
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
const result = parseJSONResume(json);
|
||||
const skill = result.sections.skills.items[0]!;
|
||||
|
||||
expect(skill.name).toBe("TypeScript");
|
||||
@@ -121,7 +119,7 @@ describe("JSONResumeImporter.parse", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
const result = parseJSONResume(json);
|
||||
expect(result.sections.profiles.items).toHaveLength(1);
|
||||
|
||||
const profile = result.sections.profiles.items[0]!;
|
||||
@@ -134,13 +132,13 @@ describe("JSONResumeImporter.parse", () => {
|
||||
basics: { image: "https://example.com/pic.jpg" },
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
const result = parseJSONResume(json);
|
||||
expect(result.picture.url).toBe("https://example.com/pic.jpg");
|
||||
expect(result.picture.hidden).toBe(false);
|
||||
});
|
||||
|
||||
it("leaves the summary content empty when basics.summary is absent", () => {
|
||||
const result = importer.parse(JSON.stringify({ basics: { name: "Jane" } }));
|
||||
const result = parseJSONResume(JSON.stringify({ basics: { name: "Jane" } }));
|
||||
expect(result.summary.content).toBe("");
|
||||
});
|
||||
|
||||
@@ -159,7 +157,7 @@ describe("JSONResumeImporter.parse", () => {
|
||||
],
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
const result = parseJSONResume(json);
|
||||
expect(result.sections.projects.items).toHaveLength(1);
|
||||
|
||||
const project = result.sections.projects.items[0]!;
|
||||
|
||||
+260
-264
@@ -1,11 +1,12 @@
|
||||
import type { ResumeData } from "@reactive-resume/schema/resume/data";
|
||||
import { flattenError, ZodError, z } from "zod";
|
||||
import { ZodError, z } from "zod";
|
||||
import { getNetworkIcon } from "@reactive-resume/resume/icons";
|
||||
import { resumeDataSchema } from "@reactive-resume/schema/resume/data";
|
||||
import { defaultResumeData } from "@reactive-resume/schema/resume/default";
|
||||
import { generateId } from "@reactive-resume/utils/string";
|
||||
import { createUrl } from "@reactive-resume/utils/url";
|
||||
import { formatPeriod, formatSingleDate } from "./date";
|
||||
import { rethrowAsImportError } from "./error";
|
||||
import { arrayToHtmlList, toHtmlDescription } from "./html";
|
||||
import { parseLevel } from "./level";
|
||||
|
||||
@@ -175,276 +176,271 @@ function formatLocation(location?: JSONResumeLocation): string {
|
||||
return parts.join(", ");
|
||||
}
|
||||
|
||||
export class JSONResumeImporter {
|
||||
convert(jsonResume: JSONResume): ResumeData {
|
||||
const result: ResumeData = {
|
||||
...defaultResumeData,
|
||||
// ponytail: stateless two-method class → two plain functions
|
||||
function convertJSONResume(jsonResume: JSONResume): ResumeData {
|
||||
const result: ResumeData = {
|
||||
...defaultResumeData,
|
||||
};
|
||||
|
||||
// Map basics
|
||||
if (jsonResume.basics) {
|
||||
const basics = jsonResume.basics;
|
||||
result.basics = {
|
||||
name: basics.name || "",
|
||||
headline: basics.label || "",
|
||||
email: basics.email || "",
|
||||
phone: basics.phone || "",
|
||||
location: basics.location ? formatLocation(basics.location) : "",
|
||||
website: createUrl(basics.url),
|
||||
customFields: [],
|
||||
};
|
||||
|
||||
// Map basics
|
||||
if (jsonResume.basics) {
|
||||
const basics = jsonResume.basics;
|
||||
result.basics = {
|
||||
name: basics.name || "",
|
||||
headline: basics.label || "",
|
||||
email: basics.email || "",
|
||||
phone: basics.phone || "",
|
||||
location: basics.location ? formatLocation(basics.location) : "",
|
||||
website: createUrl(basics.url),
|
||||
customFields: [],
|
||||
};
|
||||
|
||||
// Map image to picture
|
||||
if (basics.image) {
|
||||
result.picture = {
|
||||
...defaultResumeData.picture,
|
||||
url: basics.image,
|
||||
hidden: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Map summary
|
||||
if (jsonResume.basics?.summary) {
|
||||
result.summary = {
|
||||
...defaultResumeData.summary,
|
||||
content: `<p>${jsonResume.basics.summary}</p>`,
|
||||
// Map image to picture
|
||||
if (basics.image) {
|
||||
result.picture = {
|
||||
...defaultResumeData.picture,
|
||||
url: basics.image,
|
||||
hidden: false,
|
||||
};
|
||||
}
|
||||
|
||||
// Map work to experience
|
||||
if (jsonResume.work && jsonResume.work.length > 0) {
|
||||
result.sections.experience = {
|
||||
...defaultResumeData.sections.experience,
|
||||
items: jsonResume.work
|
||||
.filter((work) => work.name || work.position)
|
||||
.map((work) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
company: work.name || "",
|
||||
position: work.position || "",
|
||||
location: work.location || "",
|
||||
period: formatPeriod(work.startDate, work.endDate),
|
||||
website: createItemWebsite(work.url),
|
||||
roles: [],
|
||||
description: toHtmlDescription(work.summary, work.highlights),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map education
|
||||
if (jsonResume.education && jsonResume.education.length > 0) {
|
||||
result.sections.education = {
|
||||
...defaultResumeData.sections.education,
|
||||
items: jsonResume.education
|
||||
.filter((edu) => edu.institution)
|
||||
.map((edu) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
school: edu.institution || "",
|
||||
degree: [edu.studyType, edu.area].filter(Boolean).join(" in ") || "",
|
||||
area: edu.area || "",
|
||||
grade: edu.score || "",
|
||||
location: "",
|
||||
period: formatPeriod(edu.startDate, edu.endDate),
|
||||
website: createItemWebsite(edu.url),
|
||||
description: edu.courses && edu.courses.length > 0 ? arrayToHtmlList(edu.courses) : "",
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map projects
|
||||
if (jsonResume.projects && jsonResume.projects.length > 0) {
|
||||
result.sections.projects = {
|
||||
...defaultResumeData.sections.projects,
|
||||
items: jsonResume.projects
|
||||
.filter((project) => project.name)
|
||||
.map((project) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
name: project.name || "",
|
||||
period: formatPeriod(project.startDate, project.endDate),
|
||||
website: createItemWebsite(project.url),
|
||||
description: toHtmlDescription(project.description, project.highlights),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map skills
|
||||
if (jsonResume.skills && jsonResume.skills.length > 0) {
|
||||
result.sections.skills = {
|
||||
...defaultResumeData.sections.skills,
|
||||
items: jsonResume.skills
|
||||
.filter((skill) => skill.name)
|
||||
.map((skill) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
icon: "star",
|
||||
iconColor: "",
|
||||
name: skill.name || "",
|
||||
proficiency: skill.level || "",
|
||||
level: parseLevel(skill.level),
|
||||
keywords: skill.keywords || [],
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map languages
|
||||
if (jsonResume.languages && jsonResume.languages.length > 0) {
|
||||
result.sections.languages = {
|
||||
...defaultResumeData.sections.languages,
|
||||
items: jsonResume.languages
|
||||
.filter((lang) => lang.language)
|
||||
.map((lang) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
language: lang.language || "",
|
||||
fluency: lang.fluency || "",
|
||||
level: parseLevel(lang.fluency),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map interests
|
||||
if (jsonResume.interests && jsonResume.interests.length > 0) {
|
||||
result.sections.interests = {
|
||||
...defaultResumeData.sections.interests,
|
||||
items: jsonResume.interests
|
||||
.filter((interest) => interest.name)
|
||||
.map((interest) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
icon: "star",
|
||||
iconColor: "",
|
||||
name: interest.name || "",
|
||||
keywords: interest.keywords || [],
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map awards
|
||||
if (jsonResume.awards && jsonResume.awards.length > 0) {
|
||||
result.sections.awards = {
|
||||
...defaultResumeData.sections.awards,
|
||||
items: jsonResume.awards
|
||||
.filter((award) => award.title)
|
||||
.map((award) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
title: award.title || "",
|
||||
awarder: award.awarder || "",
|
||||
date: formatSingleDate(award.date),
|
||||
website: createItemWebsite(),
|
||||
description: award.summary ? `<p>${award.summary}</p>` : "",
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map certificates
|
||||
if (jsonResume.certificates && jsonResume.certificates.length > 0) {
|
||||
result.sections.certifications = {
|
||||
...defaultResumeData.sections.certifications,
|
||||
items: jsonResume.certificates
|
||||
.filter((cert) => cert.name)
|
||||
.map((cert) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
title: cert.name || "",
|
||||
issuer: cert.issuer || "",
|
||||
date: formatSingleDate(cert.date),
|
||||
website: createItemWebsite(cert.url),
|
||||
description: "",
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map publications
|
||||
if (jsonResume.publications && jsonResume.publications.length > 0) {
|
||||
result.sections.publications = {
|
||||
...defaultResumeData.sections.publications,
|
||||
items: jsonResume.publications
|
||||
.filter((pub) => pub.name)
|
||||
.map((pub) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
title: pub.name || "",
|
||||
publisher: pub.publisher || "",
|
||||
date: formatSingleDate(pub.releaseDate),
|
||||
website: createItemWebsite(pub.url),
|
||||
description: pub.summary ? `<p>${pub.summary}</p>` : "",
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map volunteer
|
||||
if (jsonResume.volunteer && jsonResume.volunteer.length > 0) {
|
||||
result.sections.volunteer = {
|
||||
...defaultResumeData.sections.volunteer,
|
||||
items: jsonResume.volunteer
|
||||
.filter((vol) => vol.organization)
|
||||
.map((vol) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
organization: vol.organization || "",
|
||||
location: "",
|
||||
period: formatPeriod(vol.startDate, vol.endDate),
|
||||
website: createItemWebsite(vol.url),
|
||||
description: toHtmlDescription(vol.summary, vol.highlights),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map references
|
||||
if (jsonResume.references && jsonResume.references.length > 0) {
|
||||
result.sections.references = {
|
||||
...defaultResumeData.sections.references,
|
||||
items: jsonResume.references
|
||||
.filter((ref) => ref.name || ref.reference)
|
||||
.map((ref) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
name: ref.name || "",
|
||||
position: "",
|
||||
website: createItemWebsite(),
|
||||
phone: "",
|
||||
description: ref.reference ? `<p>${ref.reference}</p>` : "",
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map profiles (from basics.profiles) to profiles section
|
||||
if (jsonResume.basics?.profiles && jsonResume.basics.profiles.length > 0) {
|
||||
result.sections.profiles = {
|
||||
...defaultResumeData.sections.profiles,
|
||||
items: jsonResume.basics.profiles
|
||||
.filter((profile) => profile.network)
|
||||
.map((profile) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
icon: getNetworkIcon(profile.network),
|
||||
iconColor: "",
|
||||
network: profile.network || "",
|
||||
username: profile.username || "",
|
||||
website: createItemWebsite(profile.url, profile.username || profile.network),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
return resumeDataSchema.parse(result);
|
||||
}
|
||||
|
||||
parse(json: string): ResumeData {
|
||||
try {
|
||||
const jsonResume = jsonResumeSchema.parse(JSON.parse(json));
|
||||
return this.convert(jsonResume);
|
||||
} catch (error) {
|
||||
if (error instanceof ZodError) {
|
||||
const errors = flattenError(error);
|
||||
throw new Error(JSON.stringify(errors));
|
||||
}
|
||||
// Map summary
|
||||
if (jsonResume.basics?.summary) {
|
||||
result.summary = {
|
||||
...defaultResumeData.summary,
|
||||
content: `<p>${jsonResume.basics.summary}</p>`,
|
||||
hidden: false,
|
||||
};
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
// Map work to experience
|
||||
if (jsonResume.work && jsonResume.work.length > 0) {
|
||||
result.sections.experience = {
|
||||
...defaultResumeData.sections.experience,
|
||||
items: jsonResume.work
|
||||
.filter((work) => work.name || work.position)
|
||||
.map((work) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
company: work.name || "",
|
||||
position: work.position || "",
|
||||
location: work.location || "",
|
||||
period: formatPeriod(work.startDate, work.endDate),
|
||||
website: createItemWebsite(work.url),
|
||||
roles: [],
|
||||
description: toHtmlDescription(work.summary, work.highlights),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map education
|
||||
if (jsonResume.education && jsonResume.education.length > 0) {
|
||||
result.sections.education = {
|
||||
...defaultResumeData.sections.education,
|
||||
items: jsonResume.education
|
||||
.filter((edu) => edu.institution)
|
||||
.map((edu) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
school: edu.institution || "",
|
||||
degree: [edu.studyType, edu.area].filter(Boolean).join(" in ") || "",
|
||||
area: edu.area || "",
|
||||
grade: edu.score || "",
|
||||
location: "",
|
||||
period: formatPeriod(edu.startDate, edu.endDate),
|
||||
website: createItemWebsite(edu.url),
|
||||
description: edu.courses && edu.courses.length > 0 ? arrayToHtmlList(edu.courses) : "",
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map projects
|
||||
if (jsonResume.projects && jsonResume.projects.length > 0) {
|
||||
result.sections.projects = {
|
||||
...defaultResumeData.sections.projects,
|
||||
items: jsonResume.projects
|
||||
.filter((project) => project.name)
|
||||
.map((project) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
name: project.name || "",
|
||||
period: formatPeriod(project.startDate, project.endDate),
|
||||
website: createItemWebsite(project.url),
|
||||
description: toHtmlDescription(project.description, project.highlights),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map skills
|
||||
if (jsonResume.skills && jsonResume.skills.length > 0) {
|
||||
result.sections.skills = {
|
||||
...defaultResumeData.sections.skills,
|
||||
items: jsonResume.skills
|
||||
.filter((skill) => skill.name)
|
||||
.map((skill) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
icon: "star",
|
||||
iconColor: "",
|
||||
name: skill.name || "",
|
||||
proficiency: skill.level || "",
|
||||
level: parseLevel(skill.level),
|
||||
keywords: skill.keywords || [],
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map languages
|
||||
if (jsonResume.languages && jsonResume.languages.length > 0) {
|
||||
result.sections.languages = {
|
||||
...defaultResumeData.sections.languages,
|
||||
items: jsonResume.languages
|
||||
.filter((lang) => lang.language)
|
||||
.map((lang) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
language: lang.language || "",
|
||||
fluency: lang.fluency || "",
|
||||
level: parseLevel(lang.fluency),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map interests
|
||||
if (jsonResume.interests && jsonResume.interests.length > 0) {
|
||||
result.sections.interests = {
|
||||
...defaultResumeData.sections.interests,
|
||||
items: jsonResume.interests
|
||||
.filter((interest) => interest.name)
|
||||
.map((interest) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
icon: "star",
|
||||
iconColor: "",
|
||||
name: interest.name || "",
|
||||
keywords: interest.keywords || [],
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map awards
|
||||
if (jsonResume.awards && jsonResume.awards.length > 0) {
|
||||
result.sections.awards = {
|
||||
...defaultResumeData.sections.awards,
|
||||
items: jsonResume.awards
|
||||
.filter((award) => award.title)
|
||||
.map((award) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
title: award.title || "",
|
||||
awarder: award.awarder || "",
|
||||
date: formatSingleDate(award.date),
|
||||
website: createItemWebsite(),
|
||||
description: award.summary ? `<p>${award.summary}</p>` : "",
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map certificates
|
||||
if (jsonResume.certificates && jsonResume.certificates.length > 0) {
|
||||
result.sections.certifications = {
|
||||
...defaultResumeData.sections.certifications,
|
||||
items: jsonResume.certificates
|
||||
.filter((cert) => cert.name)
|
||||
.map((cert) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
title: cert.name || "",
|
||||
issuer: cert.issuer || "",
|
||||
date: formatSingleDate(cert.date),
|
||||
website: createItemWebsite(cert.url),
|
||||
description: "",
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map publications
|
||||
if (jsonResume.publications && jsonResume.publications.length > 0) {
|
||||
result.sections.publications = {
|
||||
...defaultResumeData.sections.publications,
|
||||
items: jsonResume.publications
|
||||
.filter((pub) => pub.name)
|
||||
.map((pub) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
title: pub.name || "",
|
||||
publisher: pub.publisher || "",
|
||||
date: formatSingleDate(pub.releaseDate),
|
||||
website: createItemWebsite(pub.url),
|
||||
description: pub.summary ? `<p>${pub.summary}</p>` : "",
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map volunteer
|
||||
if (jsonResume.volunteer && jsonResume.volunteer.length > 0) {
|
||||
result.sections.volunteer = {
|
||||
...defaultResumeData.sections.volunteer,
|
||||
items: jsonResume.volunteer
|
||||
.filter((vol) => vol.organization)
|
||||
.map((vol) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
organization: vol.organization || "",
|
||||
location: "",
|
||||
period: formatPeriod(vol.startDate, vol.endDate),
|
||||
website: createItemWebsite(vol.url),
|
||||
description: toHtmlDescription(vol.summary, vol.highlights),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map references
|
||||
if (jsonResume.references && jsonResume.references.length > 0) {
|
||||
result.sections.references = {
|
||||
...defaultResumeData.sections.references,
|
||||
items: jsonResume.references
|
||||
.filter((ref) => ref.name || ref.reference)
|
||||
.map((ref) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
name: ref.name || "",
|
||||
position: "",
|
||||
website: createItemWebsite(),
|
||||
phone: "",
|
||||
description: ref.reference ? `<p>${ref.reference}</p>` : "",
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// Map profiles (from basics.profiles) to profiles section
|
||||
if (jsonResume.basics?.profiles && jsonResume.basics.profiles.length > 0) {
|
||||
result.sections.profiles = {
|
||||
...defaultResumeData.sections.profiles,
|
||||
items: jsonResume.basics.profiles
|
||||
.filter((profile) => profile.network)
|
||||
.map((profile) => ({
|
||||
id: generateId(),
|
||||
hidden: false,
|
||||
icon: getNetworkIcon(profile.network),
|
||||
iconColor: "",
|
||||
network: profile.network || "",
|
||||
username: profile.username || "",
|
||||
website: createItemWebsite(profile.url, profile.username || profile.network),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
return resumeDataSchema.parse(result);
|
||||
}
|
||||
|
||||
export function parseJSONResume(json: string): ResumeData {
|
||||
try {
|
||||
const jsonResume = jsonResumeSchema.parse(JSON.parse(json));
|
||||
return convertJSONResume(jsonResume);
|
||||
} catch (error) {
|
||||
if (error instanceof ZodError) rethrowAsImportError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,27 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { defaultResumeData } from "@reactive-resume/schema/resume/default";
|
||||
import { ReactiveResumeJSONImporter } from "./reactive-resume-json";
|
||||
import { parseReactiveResumeJSON } from "./reactive-resume-json";
|
||||
|
||||
const importer = new ReactiveResumeJSONImporter();
|
||||
|
||||
describe("ReactiveResumeJSONImporter", () => {
|
||||
describe("parseReactiveResumeJSON", () => {
|
||||
it("round-trips the default resume data", () => {
|
||||
const result = importer.parse(JSON.stringify(defaultResumeData));
|
||||
const result = parseReactiveResumeJSON(JSON.stringify(defaultResumeData));
|
||||
expect(result.basics.name).toBe(defaultResumeData.basics.name);
|
||||
});
|
||||
|
||||
it("throws a JSON-serialised validation error for an invalid object", () => {
|
||||
// Missing required top-level fields.
|
||||
expect(() => importer.parse(JSON.stringify({ foo: "bar" }))).toThrow();
|
||||
expect(() => parseReactiveResumeJSON(JSON.stringify({ foo: "bar" }))).toThrow();
|
||||
});
|
||||
|
||||
it("throws when the input is not valid JSON", () => {
|
||||
expect(() => importer.parse("not-json")).toThrow();
|
||||
expect(() => parseReactiveResumeJSON("not-json")).toThrow();
|
||||
});
|
||||
|
||||
it("creates a default layout page when the imported data has no pages", () => {
|
||||
const data = structuredClone(defaultResumeData);
|
||||
data.metadata.layout.pages = [];
|
||||
|
||||
const result = importer.parse(JSON.stringify(data));
|
||||
const result = parseReactiveResumeJSON(JSON.stringify(data));
|
||||
expect(result.metadata.layout.pages.length).toBe(1);
|
||||
expect(result.metadata.layout.pages[0]?.main.length).toBeGreaterThan(0);
|
||||
});
|
||||
@@ -38,7 +36,7 @@ describe("ReactiveResumeJSONImporter", () => {
|
||||
},
|
||||
];
|
||||
|
||||
const result = importer.parse(JSON.stringify(data));
|
||||
const result = parseReactiveResumeJSON(JSON.stringify(data));
|
||||
const firstPage = result.metadata.layout.pages[0];
|
||||
expect(firstPage).toBeDefined();
|
||||
const allIds = new Set([...(firstPage?.main ?? []), ...(firstPage?.sidebar ?? [])]);
|
||||
@@ -53,7 +51,7 @@ describe("ReactiveResumeJSONImporter", () => {
|
||||
});
|
||||
|
||||
it("does not modify the layout when every built-in section is already placed", () => {
|
||||
const result = importer.parse(JSON.stringify(defaultResumeData));
|
||||
const result = parseReactiveResumeJSON(JSON.stringify(defaultResumeData));
|
||||
expect(result.metadata.layout.pages.length).toBe(defaultResumeData.metadata.layout.pages.length);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { ResumeData } from "@reactive-resume/schema/resume/data";
|
||||
import { flattenError, ZodError } from "zod";
|
||||
import { ZodError } from "zod";
|
||||
import { resumeDataSchema, sectionTypeSchema } from "@reactive-resume/schema/resume/data";
|
||||
import { rethrowAsImportError } from "./error";
|
||||
|
||||
const BUILT_IN_LAYOUT_SECTION_IDS = sectionTypeSchema.options.filter((section) => section !== "cover-letter");
|
||||
|
||||
@@ -62,18 +63,13 @@ function normalizeBuiltInSectionsInLayout(data: ResumeData): ResumeData {
|
||||
};
|
||||
}
|
||||
|
||||
export class ReactiveResumeJSONImporter {
|
||||
parse(json: string): ResumeData {
|
||||
try {
|
||||
const parsed = resumeDataSchema.parse(JSON.parse(json));
|
||||
return resumeDataSchema.parse(normalizeBuiltInSectionsInLayout(parsed));
|
||||
} catch (error) {
|
||||
if (error instanceof ZodError) {
|
||||
const errors = flattenError(error);
|
||||
throw new Error(JSON.stringify(errors));
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
// ponytail: stateless single-method class → plain function
|
||||
export function parseReactiveResumeJSON(json: string): ResumeData {
|
||||
try {
|
||||
const parsed = resumeDataSchema.parse(JSON.parse(json));
|
||||
return resumeDataSchema.parse(normalizeBuiltInSectionsInLayout(parsed));
|
||||
} catch (error) {
|
||||
if (error instanceof ZodError) rethrowAsImportError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { ReactiveResumeV4JSONImporter } from "./reactive-resume-v4-json";
|
||||
import { parseReactiveResumeV4JSON } from "./reactive-resume-v4-json";
|
||||
|
||||
const baseV4 = () => ({
|
||||
basics: {
|
||||
@@ -205,11 +205,9 @@ const baseV4 = () => ({
|
||||
},
|
||||
});
|
||||
|
||||
const importer = new ReactiveResumeV4JSONImporter();
|
||||
|
||||
describe("ReactiveResumeV4JSONImporter — broad section mapping", () => {
|
||||
describe("parseReactiveResumeV4JSON — broad section mapping", () => {
|
||||
const v4 = baseV4();
|
||||
const result = importer.parse(JSON.stringify(v4));
|
||||
const result = parseReactiveResumeV4JSON(JSON.stringify(v4));
|
||||
|
||||
it("maps basics fields (name, headline, contact, website, customFields)", () => {
|
||||
expect(result.basics.name).toBe("Jane Doe");
|
||||
@@ -303,6 +301,6 @@ describe("ReactiveResumeV4JSONImporter — broad section mapping", () => {
|
||||
});
|
||||
|
||||
it("invalid JSON throws", () => {
|
||||
expect(() => importer.parse("not json")).toThrow();
|
||||
expect(() => parseReactiveResumeV4JSON("not json")).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { ReactiveResumeV4JSONImporter } from "./reactive-resume-v4-json";
|
||||
import { parseReactiveResumeV4JSON } from "./reactive-resume-v4-json";
|
||||
|
||||
// Minimal v4 resume JSON to use as base in all tests.
|
||||
// V4 layout is Array<Array<string[]>>: [ [mainColumn, sidebarColumn] ]
|
||||
@@ -124,11 +124,9 @@ function makeV4Base(overrides: Record<string, unknown> = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
const importer = new ReactiveResumeV4JSONImporter();
|
||||
|
||||
// ─── Bug #2726-A: Custom section items without a name ("description-only") ───
|
||||
|
||||
describe("ReactiveResumeV4JSONImporter – custom section description-only items", () => {
|
||||
describe("parseReactiveResumeV4JSON – custom section description-only items", () => {
|
||||
it("puts description in the body (not the title) when item has no name", () => {
|
||||
const v4 = makeV4Base({
|
||||
custom: {
|
||||
@@ -150,7 +148,7 @@ describe("ReactiveResumeV4JSONImporter – custom section description-only items
|
||||
},
|
||||
});
|
||||
|
||||
const result = importer.parse(JSON.stringify(v4));
|
||||
const result = parseReactiveResumeV4JSON(JSON.stringify(v4));
|
||||
const section = result.customSections[0];
|
||||
expect(section).toBeDefined();
|
||||
|
||||
@@ -184,7 +182,7 @@ describe("ReactiveResumeV4JSONImporter – custom section description-only items
|
||||
},
|
||||
});
|
||||
|
||||
const result = importer.parse(JSON.stringify(v4));
|
||||
const result = parseReactiveResumeV4JSON(JSON.stringify(v4));
|
||||
const section = result.customSections[0];
|
||||
const item = section?.items[0] as { company: string; position: string; description: string };
|
||||
|
||||
@@ -214,7 +212,7 @@ describe("ReactiveResumeV4JSONImporter – custom section description-only items
|
||||
},
|
||||
});
|
||||
|
||||
const result = importer.parse(JSON.stringify(v4));
|
||||
const result = parseReactiveResumeV4JSON(JSON.stringify(v4));
|
||||
const item = result.customSections[0]?.items[0] as { description: string };
|
||||
// summary takes priority over description
|
||||
expect(item.description).toBe("<p>Rich HTML content</p>");
|
||||
@@ -241,7 +239,7 @@ describe("ReactiveResumeV4JSONImporter – custom section description-only items
|
||||
},
|
||||
});
|
||||
|
||||
const result = importer.parse(JSON.stringify(v4));
|
||||
const result = parseReactiveResumeV4JSON(JSON.stringify(v4));
|
||||
const item = result.customSections[0]?.items[0] as { company: string; position: string; description: string };
|
||||
expect(item.company).toBe("#1");
|
||||
expect(item.position).toBe("");
|
||||
@@ -268,7 +266,7 @@ describe("ReactiveResumeV4JSONImporter – custom section description-only items
|
||||
},
|
||||
});
|
||||
|
||||
const result = importer.parse(JSON.stringify(v4));
|
||||
const result = parseReactiveResumeV4JSON(JSON.stringify(v4));
|
||||
const item = result.customSections[0]?.items[0] as { hidden: boolean };
|
||||
// Should default to visible (hidden=false), consistent with all other sections
|
||||
expect(item.hidden).toBe(false);
|
||||
@@ -291,7 +289,7 @@ describe("ReactiveResumeV4JSONImporter – custom section description-only items
|
||||
},
|
||||
});
|
||||
|
||||
const result = importer.parse(JSON.stringify(v4));
|
||||
const result = parseReactiveResumeV4JSON(JSON.stringify(v4));
|
||||
const section = result.customSections[0];
|
||||
// Both items must migrate — hidden items must NOT be dropped
|
||||
expect(section?.items).toHaveLength(2);
|
||||
@@ -304,7 +302,7 @@ describe("ReactiveResumeV4JSONImporter – custom section description-only items
|
||||
|
||||
// ─── Bug #2726-B: Skill / Language levels incorrectly clamped to max ──────────
|
||||
|
||||
describe("ReactiveResumeV4JSONImporter – skill level scaling (v4: 0-10 → v5: 0-5)", () => {
|
||||
describe("parseReactiveResumeV4JSON – skill level scaling (v4: 0-10 → v5: 0-5)", () => {
|
||||
function makeWithSkills(items: Array<{ id: string; visible: boolean; name: string; level: number }>) {
|
||||
return makeV4Base({
|
||||
skills: {
|
||||
@@ -319,54 +317,56 @@ describe("ReactiveResumeV4JSONImporter – skill level scaling (v4: 0-10 → v5:
|
||||
}
|
||||
|
||||
it("scales level 10 → 5", () => {
|
||||
const result = importer.parse(
|
||||
const result = parseReactiveResumeV4JSON(
|
||||
JSON.stringify(makeWithSkills([{ id: "s1", visible: true, name: "TypeScript", level: 10 }])),
|
||||
);
|
||||
expect(result.sections.skills.items[0]?.level).toBe(5);
|
||||
});
|
||||
|
||||
it("scales level 8 → 4 (not 5)", () => {
|
||||
const result = importer.parse(
|
||||
const result = parseReactiveResumeV4JSON(
|
||||
JSON.stringify(makeWithSkills([{ id: "s2", visible: true, name: "React", level: 8 }])),
|
||||
);
|
||||
expect(result.sections.skills.items[0]?.level).toBe(4);
|
||||
});
|
||||
|
||||
it("scales level 6 → 3 (not 5)", () => {
|
||||
const result = importer.parse(
|
||||
const result = parseReactiveResumeV4JSON(
|
||||
JSON.stringify(makeWithSkills([{ id: "s3", visible: true, name: "GraphQL", level: 6 }])),
|
||||
);
|
||||
expect(result.sections.skills.items[0]?.level).toBe(3);
|
||||
});
|
||||
|
||||
it("scales level 5 → 3 (rounds 2.5 up)", () => {
|
||||
const result = importer.parse(
|
||||
const result = parseReactiveResumeV4JSON(
|
||||
JSON.stringify(makeWithSkills([{ id: "s4", visible: true, name: "Node", level: 5 }])),
|
||||
);
|
||||
expect(result.sections.skills.items[0]?.level).toBe(3);
|
||||
});
|
||||
|
||||
it("keeps level 0 → 0 (hides the visual indicator)", () => {
|
||||
const result = importer.parse(
|
||||
const result = parseReactiveResumeV4JSON(
|
||||
JSON.stringify(makeWithSkills([{ id: "s5", visible: true, name: "Rust", level: 0 }])),
|
||||
);
|
||||
expect(result.sections.skills.items[0]?.level).toBe(0);
|
||||
});
|
||||
|
||||
it("clamps negative level to 0", () => {
|
||||
const result = importer.parse(JSON.stringify(makeWithSkills([{ id: "s6", visible: true, name: "Go", level: -3 }])));
|
||||
const result = parseReactiveResumeV4JSON(
|
||||
JSON.stringify(makeWithSkills([{ id: "s6", visible: true, name: "Go", level: -3 }])),
|
||||
);
|
||||
expect(result.sections.skills.items[0]?.level).toBe(0);
|
||||
});
|
||||
|
||||
it("clamps level above 10 to 5", () => {
|
||||
const result = importer.parse(
|
||||
const result = parseReactiveResumeV4JSON(
|
||||
JSON.stringify(makeWithSkills([{ id: "s7", visible: true, name: "C++", level: 15 }])),
|
||||
);
|
||||
expect(result.sections.skills.items[0]?.level).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ReactiveResumeV4JSONImporter – language level scaling (v4: 0-10 → v5: 0-5)", () => {
|
||||
describe("parseReactiveResumeV4JSON – language level scaling (v4: 0-10 → v5: 0-5)", () => {
|
||||
function makeWithLanguages(items: Array<{ id: string; visible: boolean; name: string; level: number }>) {
|
||||
return makeV4Base({
|
||||
languages: {
|
||||
@@ -381,14 +381,14 @@ describe("ReactiveResumeV4JSONImporter – language level scaling (v4: 0-10 →
|
||||
}
|
||||
|
||||
it("scales level 10 → 5", () => {
|
||||
const result = importer.parse(
|
||||
const result = parseReactiveResumeV4JSON(
|
||||
JSON.stringify(makeWithLanguages([{ id: "l1", visible: true, name: "Spanish", level: 10 }])),
|
||||
);
|
||||
expect(result.sections.languages.items[0]?.level).toBe(5);
|
||||
});
|
||||
|
||||
it("scales level 6 → 3 (not 5)", () => {
|
||||
const result = importer.parse(
|
||||
const result = parseReactiveResumeV4JSON(
|
||||
JSON.stringify(makeWithLanguages([{ id: "l2", visible: true, name: "French", level: 6 }])),
|
||||
);
|
||||
expect(result.sections.languages.items[0]?.level).toBe(3);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user