mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-08-24 15:22:20 +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:
+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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user