75 lines
1.9 KiB
JavaScript
75 lines
1.9 KiB
JavaScript
import defaultData from './cv.json';
|
|
|
|
export const DEFAULT_DATA = defaultData;
|
|
|
|
export function validateCV(data) {
|
|
const errors = [];
|
|
|
|
if (!data.personal) {
|
|
errors.push('personal is required');
|
|
} else {
|
|
if (!data.personal.name?.trim()) errors.push('personal.name is required');
|
|
if (!data.personal.title?.trim()) errors.push('personal.title is required');
|
|
if (!data.personal.email?.trim()) errors.push('personal.email is required');
|
|
}
|
|
|
|
if (!Array.isArray(data.experience)) {
|
|
errors.push('experience must be an array');
|
|
}
|
|
|
|
if (!data.skills || typeof data.skills !== 'object') {
|
|
errors.push('skills must be an object');
|
|
}
|
|
|
|
if (!Array.isArray(data.education)) {
|
|
errors.push('education must be an array');
|
|
}
|
|
|
|
if (!Array.isArray(data.projects)) {
|
|
errors.push('projects must be an array');
|
|
}
|
|
|
|
return {
|
|
valid: errors.length === 0,
|
|
errors
|
|
};
|
|
}
|
|
|
|
export function mergeCVData(base, updates) {
|
|
return {
|
|
...base,
|
|
...updates,
|
|
personal: { ...base.personal, ...updates.personal }
|
|
};
|
|
}
|
|
|
|
export function exportToJSON(data) {
|
|
return JSON.stringify(data, null, 2);
|
|
}
|
|
|
|
export function importFromJSON(jsonString) {
|
|
try {
|
|
const parsed = JSON.parse(jsonString);
|
|
const validation = validateCV(parsed);
|
|
if (!validation.valid) {
|
|
return { success: false, errors: validation.errors };
|
|
}
|
|
return { success: true, data: parsed };
|
|
} catch (e) {
|
|
return { success: false, errors: [`JSON parse error: ${e.message}`] };
|
|
}
|
|
}
|
|
|
|
export function downloadJSON(data, filename = 'cv.json') {
|
|
const json = exportToJSON(data);
|
|
const blob = new Blob([json], { type: 'application/json' });
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = filename;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
URL.revokeObjectURL(url);
|
|
}
|