{"size": 1789, "ext": "js", "max_stars_repo_path": "src/modules/lib.js", "max_stars_repo_name": "sylvain-artois/Migrate-Blog-From-Tumblr-To-KeystoneJS", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/modules/lib.js", "max_issues_repo_name": "sylvain-artois/Migrate-Blog-From-Tumblr-To-KeystoneJS", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/modules/lib.js", "max_forks_repo_name": "sylvain-artois/Migrate-Blog-From-Tumblr-To-KeystoneJS", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.9275362319, "max_line_length": 80, "alphanum_fraction": 0.6338736724}
const _         = require('lodash');
const entities  = require("entities");

module.exports = {
    decode: decodeEntities,
    oIdToCategory: oIdToCategory,
    keystoneToWp: keystoneToWp,
    getStatus: getStatus,
    getTitle: getTitle,
    getContent: getContent,
    getFormat: getFormat
};

function decodeEntities(str) {
    return entities.decodeHTML(str);
}

function oIdToCategory(keystoneCategories, category){
    return _.find(
        keystoneCategories,
        (keystoneCategory) => { return keystoneCategory.$oid === category.$oid }
    );
}

function keystoneToWp(keystoneCategory, wpCategories) {
    return _.find(
        wpCategories,
        (wpCategory) => { return wpCategory.slug === keystoneCategory.key }
    );
}

function getFormat(keystonePostType) {
    if (keystonePostType === 'photo') {
        return 'image';
    } else if (keystonePostType === 'text') {
        return 'standard';
    } else if (keystonePostType === 'quote') {
        return 'quote';
    } else if (keystonePostType === 'gallery') {
        return 'gallery';
    } else if (keystonePostType === 'medium') {
        //video or audio ?
    }
}

function getStatus(keystonePostState) {
    if (keystonePostState === 'published') {
        return 'publish';
    } else if (keystonePostState === 'draft') {
        return 'draft';
    }

    //error ? Will fail when calling API.create
    return "";
}

function getTitle(post) {
    if (post.type === 'quote') return post.writer;
    if (post.type === 'gallery') return post.captionText;
    return post.title || "";
}

function getContent(post) {
    if (post.type === 'quote') return decodeEntities(post.quoteText);
    if (post.type === 'text') return decodeEntities(post.contentText);
    return decodeEntities(post.title || "");
}

