all files / packages/links-ext/services/links/ angular-link.js

52.38% Statements 11/21
0% Branches 0/8
25% Functions 1/4
52.38% Lines 11/21
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                                                                                            
var _ = require('lodash');
 
/**
 * @dgService angular-link
 * @description checks link for applicability and produces link to angularjs doc
 */
module.exports = function angularLink (log) {
    /**
     * ng.service.$q
     * ng.service.$q@1.2.29
     * ngRoute.provider.$routeProvider
     */
    var MATCH_REGEX = /^(ng[^\.]*)\.(\w+)\.(\$*[\w.\[\]]+)(?:@((?:\d+\.){2}\d+))?$/;
                      // ngModule    type   name               version
 
    var BASE_URL = 'https://docs.angularjs.org/api/${module}/${group}/${component}';
    var VERSIONED_URL = 'https://code.angularjs.org/${version}/docs/api/${module}/${group}/${component}';
 
    var BASE_COMPONENT_URL = 'https://docs.angularjs.org/partials/api/${module}/${group}/${component}.html';
    var VERSIONED_COMPONENT_URL = 'https://code.angularjs.org/1.2.29/docs/partials/api/${module}/${group}/${component}.html';
 
    function checkApplicability (component) {
        return MATCH_REGEX.test(component);
    }
 
    function makeUrl (component) {
        var matches = component.match(MATCH_REGEX);
        var version =  matches[4] || this.version;
        return version ? _.template(this.versionedUrl)({ version: version, module: matches[1], group: matches[2], component: matches[3] })
                       : _.template(this.baseUrl)({ module: matches[1], group: matches[2], component: matches[3] })
    }
 
    /**
     * @name makeTitle
     * @param {String} component component to build title for
     * @param {String} [title] optional title, is not used
     * @description creates title from component
     */
    function makeTitle (component, title) {
        var matches = component.match(MATCH_REGEX);
        var res = matches[3];
        var version = matches[4] || this.version;
        if (version) {
            res += ' (v' + version + ')';
        }
        return res;
    }
 
    return {
        version: void(null),
        baseUrl: BASE_URL,
        versionedUrl: VERSIONED_URL,
        test: checkApplicability,
        url: makeUrl,
        title: makeTitle
    }
};