DocxGenerator.js

const TITLE_FONT = 24;
const SUB_TITLE_FONT = 14;
const TITLE_H1_FONT = 20;
const TITLE_H2_FONT = 12;
const PARA_TEXT_FONT = 12;
const COLOR_BLACK_LIGHTER = 555555;
const COLOR_GREY_LIGHT = 'CCCCCC';
const SEPERATOR_PIPE = ' | ';
const SEPERATOR_TO = ' to ';
const BULLET_POINT = '    ' + String.fromCharCode( 8226 ) + '  ';

const TITLE = { color: COLOR_BLACK_LIGHTER, font_face: 'Arial', font_size: TITLE_FONT };
const SUB_TITLE = { color: COLOR_GREY_LIGHT, font_face: 'Arial', font_size: SUB_TITLE_FONT };
const TITLE_H1 = { color: COLOR_BLACK_LIGHTER, font_face: 'Arial', font_size: TITLE_H1_FONT };
const TITLE_H2 = { bold: true, color: COLOR_BLACK_LIGHTER, font_face: 'Arial', font_size: TITLE_H2_FONT };
const TITLE_PARA = { align: 'center', backline: 'E0E0E0', font_face: 'Arial' };
const PARA_TEXT = { color: COLOR_BLACK_LIGHTER, font_face: 'Arial', font_size: PARA_TEXT_FONT };
const HORIZONTAL_LINE = { color: COLOR_GREY_LIGHT, font_face: 'Arial', font_size: 8 };

function generate(docx, inputs) {
    addHeading(docx, inputs.profile);
    addObjective(docx, inputs.objective);
    addSkills(docx, inputs.skills);
    addWorkExperience(docx, inputs.companies);
    addEducation(docx, inputs.educations);
    addExperience(docx, inputs.experiences);
    addActivities(docx, inputs.activities);
    addInterests(docx, inputs.interest);
}

function addHeading(docx, profile) {
    var para = docx.createP(TITLE_PARA);
    var line = profile.fullname.toUpperCase();
    para.addText(line , TITLE);
    para.addLineBreak();

    line = '';
    if(profile.designation) {
        line += profile.designation;
    }
    if(profile.gender) {
        line += SEPERATOR_PIPE + profile.gender;
    }
    if(profile.experienceTotal) {
        line += SEPERATOR_PIPE + profile.experienceTotal;
    }
    if(line) {
        para.addText(line, SUB_TITLE);
    }

    createHorizontalLine(para);
    para.addLineBreak();
}

function addObjective(docx, objective) {
    var title = docx.createP();
    title.addText(objective.title, TITLE_H1);
    var para = docx.createP();
    para.addText(objective.description, PARA_TEXT);
    para.addLineBreak();
}

function addSkills(docx, skills) {
    var title = docx.createP();
    title.addText(skills.title, TITLE_H1);
    var para = docx.createP();
    var line = '';

    skills.list.forEach(function (skill, index) {
        line = '';
        if(skill.skillName) {
            line += skill.skillName + SEPERATOR_PIPE;
        }
        if(skill.versionNo) {
            line += skill.versionNo + SEPERATOR_PIPE;
        }
        if(skill.totalExp) {
            line += skill.totalExp;
        }
        if(line) {
            para.addText(BULLET_POINT + line, PARA_TEXT);
            para.addLineBreak();
        }
    });
}

function addWorkExperience(docx, companies) {
    var title = docx.createP();
    title.addText(companies.title, TITLE_H1);
    var para = docx.createP();
    var line = '';

    companies.list.forEach(function (company, index) {
        // COMPANY TITLE
        line = '';
        if(company.companyName) {
            line += company.companyName;
        }
        if(company.startDate && company.endDate) {
            line += SEPERATOR_PIPE + company.startDate + SEPERATOR_TO + company.endDate;
        }
        para.addText(line, TITLE_H2);
        para.addLineBreak();

        // PROJECT TITLE
        company.projects.forEach(function (project, index) {
            line = '';
            if(project.projectName) {
                line += project.projectName;
            }
            if(project.startDate && project.endDate) {
                line += SEPERATOR_PIPE + project.startDate + SEPERATOR_TO + project.endDate;
            }
            para.addText(line, PARA_TEXT);
            para.addLineBreak();

            // Responsibilities
            project.responsibilities.forEach(function (responsibility, index) {
                if(responsibility.value) {
                    para.addText(BULLET_POINT + responsibility.value, PARA_TEXT);
                    para.addLineBreak();
                }
            });
            if(project.responsibilities.length > 0) {
                para.addLineBreak();
            }
        });

        if((index + 1) < companies.list.length) {
            para.addLineBreak();
        }
    });
}

function addEducation(docx, educations) {
    var title = docx.createP();
    title.addText(educations.title, TITLE_H1);
    var para = docx.createP();
    var line = '';

    educations.list.forEach(function (education, index) {
        line = '';
        if(education.educationTitle) {
            line += education.educationTitle;
        }
        if(education.studiedFrom) {
            line += SEPERATOR_PIPE + education.studiedFrom;
        }
        if(education.startDate && education.endDate) {
            line += SEPERATOR_PIPE + education.startDate + SEPERATOR_TO + education.endDate;
        }
        if(education.specialization) {
            line += SEPERATOR_PIPE + education.specialization;
        }
        if(education.score) {
            line += SEPERATOR_PIPE + education.score;
        }
        para.addText(BULLET_POINT + line, PARA_TEXT);
        para.addLineBreak();
    });
}

function addExperience(docx, experiences) {
    var title = docx.createP();
    title.addText(experiences.title, TITLE_H1);
    var para = docx.createP();
    var line = '';

    experiences.list.forEach(function (experience, index) {
        if(experience.description) {
            para.addText(BULLET_POINT + experience.description, PARA_TEXT);
            para.addLineBreak();
        }
    });
}

function addActivities(docx, activities) {
    var title = docx.createListOfDots();
    title.addText(activities.title, TITLE_H1);
    var para = docx.createP();
    var line = '';

    activities.list.forEach(function (activity, index) {
        if(activity.description) {
            para.addText(BULLET_POINT + activity.description, PARA_TEXT);
            para.addLineBreak();
        }
    });
}

function addInterests(docx, interest) {
    var title = docx.createP();
    title.addText(interest.title, TITLE_H1);
    var para = docx.createP();
    var line = '';

    para.addText(interest.hobbies.title, TITLE_H2);
    para.addLineBreak();
    interest.hobbies.list.forEach(function (hobby, index) {
        if(hobby.value) {
            para.addText(BULLET_POINT + hobby.value, PARA_TEXT);
            para.addLineBreak();
        }
    });

    para.addLineBreak();
    para.addText(interest.blogs.title, TITLE_H2);
    para.addLineBreak();
    interest.blogs.list.forEach(function (blog, index) {
        if(blog.value) {
            para.addText(BULLET_POINT + blog.value, PARA_TEXT);
            para.addLineBreak();
        }
    });

    para.addLineBreak();
    para.addText(interest.projects.title, TITLE_H2);
    para.addLineBreak();
    interest.projects.list.forEach(function (project, index) {
        if(project.value) {
            para.addText(BULLET_POINT + project.value, PARA_TEXT);
            para.addLineBreak();
        }
    });
}

function createHorizontalLine(para) {
    var line = '________________________________________________________________________________________';
    para.addLineBreak();
    para.addText(line, HORIZONTAL_LINE);
}

const DocxGenerator = {
    generate
};

module.exports = DocxGenerator;