all files / packages/ngdoc-ext/services/ getTypeLink.js

19.23% Statements 5/26
0% Branches 0/10
33.33% Functions 1/3
19.23% Lines 5/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                                                                          
var _ = require('lodash');
var catharsis = require('catharsis');
 
/**
 * @dgService getTypeLink
 * @description Get link to type
 */
module.exports = function getTypeLink(aliasMap, getLinkInfo, log) {
 
    function TypeLink (typeStr) {
        var typeName = typeStr;
        try {
            log.debug('[getTypeLink] building type link for %s', typeStr);
            var type = catharsis.parse(typeName, {jsdoc: true});
            if (type.type === catharsis.Types.TypeApplication) {
                typeName = catharsis.stringify(type.applications[0], {restringify: true});
                type = _.clone(type);
                type.applications = type.applications.map(function (type) {
                    if (type.type !== catharsis.Types.FunctionType && type.type !== catharsis.Types.UndefinedLiteral) {
                        type.name = getLinkInfo(type.name).title;
                    }
                    return type;
                });
            } else {
                if (type.type !== catharsis.Types.FunctionType && type.type !== catharsis.Types.UndefinedLiteral) {
                    typeName = type.name;
                    type = _.clone(type);
                    type.name = getLinkInfo(type.name).title;
                }
            }
            typeStr = catharsis.stringify(type, {restringify: true});
        } catch (e) {
            log.error('[getTypeLink] Parse of "%s" failed with reason: %s', typeStr, e.message);
            typeName = typeStr;
        }
        var res = _.template('{@link ${link} ${title}}')({link: typeName, title: typeStr});
        log.silly('[getTypeLink] built link %s', res);
        return res;
    };
 
    return TypeLink;
};