all files / lib/ Router.js

93.98% Statements 78/83
86% Branches 43/50
100% Functions 9/9
93.9% Lines 77/82
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239                                                            11×                                 13×     13×                 22×   22× 22×                       40× 40×   106× 106×         106×       106× 106× 66×       40× 40×   40× 18×     18×   16× 16×     16× 16× 16× 16× 15×                         18×     22×     40×   40×                             22× 22×   22× 22×   22×   22×   12×     12× 18× 18× 17×       12×         22×   20× 12×   12×     20×       20× 13×         22×     22×                                        
'use strict';
 
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.default = undefined;
 
var _Route = require('./RouterRoute/Route');
 
var _Route2 = _interopRequireDefault(_Route);
 
var _Segment = require('./RouterRoute/Segment');
 
var _Segment2 = _interopRequireDefault(_Segment);
 
var _Route3 = require('./Route');
 
var _Route4 = _interopRequireDefault(_Route3);
 
/**
 * @function
 * @param obj
*/
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
const regExpStartingSlash = /^\/+/;
const regExpEndingSlash = /\/+$/;
 
let Router = class Router {
    /**
     * @param {RoutesTranslations} routesTranslations
    */
    constructor(routesTranslations) {
        this._routesMap = new Map();
        this._routes = [];
        this._routesTranslations = routesTranslations;
    }
 
    /**
     * Get a Route by its key
     *
     * @param {string} key
     * @return {Route}
    */
    get(key) {
        return this._routesMap.get(key);
    }
 
    /**
     * @param {string} routeKey
     * @param {RouterRouteCommon} route
    */
    addRoute(routeKey, route) {
        if (route instanceof _Route2.default) {
            this._addInternalRoute(routeKey, route);
        }
 
        this._routes.push(route);
    }
 
    /**
     * @param {string} routeKey
     * @param {RouterRoute} route
    */
    _addInternalRoute(routeKey, route) {
        Iif (this._routesMap.has(routeKey)) {
            throw new Error(`duplicate routeKey: "${ routeKey }"`);
        }
        this._routesMap.set(routeKey, route);
    }
 
    /**
     * @param {string} path
     * @param {string=} lang
     * @return {Route}
    */
    find(path) {
        let lang = arguments.length <= 1 || arguments[1] === undefined ? 'en' : arguments[1];
 
        path = `/${ path.trim().replace(regExpStartingSlash, '').replace(regExpEndingSlash, '') }`;
        return this._findRoute(this._routes, path, path, lang);
    }
 
    /**
     * @param {Array} routes
     * @param {string} completePath
     * @param {string} path
     * @param {string} lang
     * @param {Map} namedParams
     * @return {RouterRoute} route the route or undefined if none found
    */
    _findRoute(routes, completePath, path, lang, namedParams) {
        let result;
        routes.some((route, index) => {
            /* RouterRouteLang */
            let routeLang = route.get(lang);
            Iif (!routeLang) {
                throw new Error(`Cannot find routeLang for lang ${ lang } and route ${ index }`);
            }
 
            // console.log(`[springbokjs-router] trying ${routeLang.regExp}`);
            Iif (this.logger) {
                this.logger.info(`[springbokjs-router] trying ${ routeLang.regExp }`);
            }
 
            const match = routeLang.match(path);
            if (!match) {
                return false;
            }
            // console.log(match);
 
            match.shift(); // remove m[0];
            let groupCount = match.length;
 
            if (route instanceof _Segment2.default) {
                const restOfThePath = match[--groupCount];
 
                // Copy/paste... argh I hate that !
                if (route.getNamedParamsCount() !== 0) {
                    // set params
                    Eif (!namedParams) {
                        namedParams = new Map();
                    }
 
                    let group = 0;
                    route.namedParams.forEach(paramName => {
                        const value = match[group++];
                        if (value) {
                            namedParams.set(paramName, value);
                        }
                    });
                }
 
                /* if (route.defaultRoute) {
                    if (restOfThePath.length !== 0) {
                        result = this._findRoute(route.subRoutes, completePath, restOfThePath, lang, namedParams);
                    }
                    if (!result) {
                        result = this._createRoute(completePath, lang, route.defaultRoute, undefined, 0, namedParams);
                    }
                } else {*/
                result = this._findRoute(route.subRoutes, completePath, restOfThePath, lang, namedParams);
                // }
            } else {
                    result = this._createRoute(completePath, lang, route, match, groupCount, namedParams);
                }
 
            return true;
        });
        return result;
    }
 
    /**
     * Creates a new Route result
     *
     * @param {string} completePath
     * @param {string} lang
     * @param {RouterRoute} route
     * @param {Array} match
     * @param {int} groupCount
     * @param {Map} namedParams
     * @return {Route} route
    */
    _createRoute(completePath, lang, route, match, groupCount, namedParams) {
        let group = 0;
        let extension = groupCount === 0 || !route.extension ? undefined : match[--groupCount];
 
        let controller = route.controller;
        let action = route.action;
 
        let otherParams;
 
        if (route.getNamedParamsCount() !== 0) {
            // set params
            if (!namedParams) {
                namedParams = new Map();
            }
 
            route.namedParams.forEach(paramName => {
                const value = match[group++];
                if (value) {
                    namedParams.set(paramName, value);
                }
            });
 
            Iif (!namedParams.size) {
                namedParams = undefined;
            }
        }
 
        if (namedParams) {
            // Replace controller and action if needed
            if (namedParams.has('controller')) {
                controller = this._routesTranslations.untranslate(namedParams.get('controller'), lang);
                // Should we remove it ?
                namedParams.delete('controller');
            }
 
            if (namedParams.has('action')) {
                action = this._routesTranslations.untranslate(namedParams.get('action'), lang);
                // Should we remove it ?
                namedParams.delete('action');
            }
 
            if (!namedParams.size) {
                namedParams = undefined;
            }
        }
 
        // The only not-named param can be /*
        if (group + 1 === groupCount && match[group]) {
            otherParams = match[group].split('/');
        }
 
        return new _Route4.default(completePath, controller, action, namedParams, otherParams, extension);
    }
 
    /**
     * Create a link
     *
     * @param {string} lang
     * @param {string} routeKey
     * @param {string} [params.extension]
     * @param {string} [params.queryString]
     * @param {string} [params.hash]
     * @return {string}
    * @param params
    */
    urlGenerator(lang, routeKey, params) {
        const route = this._routesMap.get(routeKey);
        try {
            return route.routes.get(lang).url(params);
        } catch (err) {
            throw new Error(err.message);
        }
    }
};
exports.default = Router;
//# sourceMappingURL=Router.js.map