All files / web/bundles/pimui/js route-matcher.js

87.76% Statements 43/49
73.08% Branches 19/26
100% Functions 5/5
87.76% Lines 43/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100    1183x 1183x 3883x     3883x 176x     3883x       3883x       3883x       3883x   3883x 1453162x     3883x 809871x 809871x 809871x 809871x   809871x             809871x 1453162x   966950x 966950x 966950x   486212x 486212x 486212x   486212x 469659x   16553x   486212x 486212x           809871x 803742x     6129x   6129x 3883x 3883x   3883x 1837x 1837x 1832x   5x       3883x                     1183x        
'use strict';
 
define(['routing'], function (Routing) {
    var matchUrl = function (url) {
        var routes = Routing.getRoutes().c;
        var route;
 
        if (url.indexOf('?') !== -1) {
            url = url.substring(0, url.indexOf('?'));
        }
 
        Iif (url.indexOf('#') === 0) {
            url = url.substring(1);
        }
 
        Iif (url.indexOf('#') !== -1) {
            url = url.substring(0, url.indexOf('#'));
        }
 
        Iif (url.indexOf('/') !== 0) {
            url = '/' + url;
        }
 
        url = url.replace(Routing.getBaseUrl(), '');
 
        var escape = function (value) {
            return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
        };
 
        for (var name in routes) {
            route = routes[name];
            var pattern = '';
            var variables = [];
            var matching = true;
 
            Iif (route.requirements._method &&
                -1 === route.requirements._method.indexOf('GET') &&
                -1 === route.requirements._method.indexOf('POST')
            ) {
                continue;
            }
 
            route.tokens.forEach(function (token) {
                switch (token[0]) {
                    case 'text':
                        pattern = escape(token[1]) + pattern;
                        matching = matching && url.indexOf(token[1]) !== -1;
                        break;
                    case 'variable':
                        var separator = escape(token[1]);
                        var varPattern = token[2].replace('++', '+');
                        var varName = token[3];
 
                        if (undefined === route.defaults[token[3]]) {
                            pattern = separator + '(' + varPattern + ')' + pattern;
                        } else {
                            pattern = '(?:' + separator + '(' + varPattern + '))?' + pattern;
                        }
                        variables.push(varName);
                        break;
                    default:
                        break;
                }
            });
 
            if (!matching) {
                continue;
            }
 
            var matches = url.match(new RegExp('^' + pattern + '$'));
 
            if (matches) {
                var params = {};
                variables.reverse();
 
                variables.forEach(function (variable, index) {
                    var matchedValue = matches[index + 1];
                    if (undefined !== matchedValue) {
                        params[variable] = matchedValue;
                    } else {
                        params[variable] = route.defaults[variable];
                    }
                });
 
                return {
                    name: name,
                    route: route,
                    params: params
                };
            }
        }
 
        return false;
    };
 
    return {
        match: matchUrl
    };
});