all files / umd/ path-resolver.js

100% Statements 28/28
100% Branches 23/23
100% Functions 4/4
100% Lines 28/28
1 statement, 4 branches Ignored     
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83                                                           133×     110×       23×   23×     23×   23×   23× 38×   38× 14×   24× 18× 17×                   23×   23×        
(function (global, factory) {
    'use strict';
 
    var built = factory(global);
 
    /* istanbul ignore else */
    Eif (typeof module === 'object' && module) {
        module.exports = built;
    }
 
    /* istanbul ignore next */
    Iif (typeof define === 'function' && define.amd) {
        define(factory);
    }
 
    global.PathResolver = built;
}(typeof global !== 'undefined' ? global : /* istanbul ignore next */ this, function (global) {
 
    'use strict';
 
/**
 * Creates an instance of PathResolver class.
 *
 * @constructor
 */
function PathResolver() {}
 
PathResolver.prototype = {
    constructor: PathResolver,
 
    /**
     * Resolves the path of module.
     *
     * @param {string} root Root path which will be used as reference to resolve the path of the dependency.
     * @param {string} dependency The dependency path, which have to be resolved.
     * @return {string} The resolved dependency path.
     */
    resolvePath: function(root, dependency) {
        if (dependency === 'exports' || dependency === 'module' ||
            !(dependency.indexOf('.') === 0 || dependency.indexOf('..') === 0)) {
 
            return dependency;
        }
 
        // Split module directories
        var moduleParts = root.split('/');
        // Remove module name
        moduleParts.splice(-1, 1);
 
        // Split dependency directories
        var dependencyParts = dependency.split('/');
        // Extract dependecy name
        var dependencyName = dependencyParts.splice(-1, 1);
 
        for (var i = 0; i < dependencyParts.length; i++) {
            var dependencyPart = dependencyParts[i];
 
            if (dependencyPart === '.') {
                continue;
 
            } else if (dependencyPart === '..') {
                if (moduleParts.length) {
                    moduleParts.splice(-1, 1);
                }
                else {
                    moduleParts = moduleParts.concat(dependencyParts.slice(i));
 
                    break;
                }
 
            } else {
                moduleParts.push(dependencyPart);
            }
        }
 
        moduleParts.push(dependencyName);
 
        return moduleParts.join('/');
    }
};
 
    return PathResolver;
}));