All files RussianRouter.js

100% Statements 33/33
100% Branches 17/17
100% Functions 10/10
100% Lines 33/33
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                    161x 1x     160x 159x               292x 292x 2x       290x 290x       499x 499x 499x 496x 593x 593x 593x 3x   590x 588x 378x 378x 1x       494x       18x       5257x 5257x       1x       899x       160x       159x 159x 205x   146x      
import {
    getDefaultPart
} from './utils.js';
import RouterOptions from './RouterOptions.js';
import Route from './Route.js';
import UserUri from './UserUri.js';
import RouterError from './RouterError.js';
 
export default class RussianRouter {
    constructor (rawRoutes={}, rawOptions={}) {
        if (typeof this.getDefaultPart !== 'function') {
            throw new RouterError(RouterError.INVALID_DEFAULT_GETTER);
        }
 
        this._parsedOptions = this._parseOptions(rawOptions);
        this._parsedRoutes = this._parseRoutes(rawRoutes);
    }
 
    destructor () {
 
    }
 
    generateUri (routeName, userParams={}, parsedRoutes=this._parsedRoutes) {
        const parsedRoute = parsedRoutes[routeName];
        if (!parsedRoute || !parsedRoute.canBeGenerated()) {
            throw new RouterError(RouterError.INVALID_ROUTE_NAME, {
                desiredRouteName: routeName
            });
        }
        const contextOptions = {router: this};
        return parsedRoute.generateUri(userParams, contextOptions);
    }
 
    matchUri (rawUri, parsedRoutes=this._parsedRoutes) {
        const parsedOptions = this._parsedOptions;
        const matchObjects = [];
        const userUri = new UserUri(rawUri, {router: this});
        for (let p in parsedRoutes) {
            const contextOptions = {router: this};
            const parsedRoute = parsedRoutes[p];
            if (!parsedRoute.canBeMatched()) {
                continue;
            }
            const matchObject = parsedRoute.matchUri(userUri, contextOptions);
            if (matchObject) {
                matchObjects.push(matchObject);
                if (parsedOptions.onlyRoute) {
                    break;
                }
            }
        }
        return parsedOptions.processMatchObjects(matchObjects);
    }
 
    resolveUri (rawUri) {
        return rawUri;
    }
 
    getDefaultPart () {
        const workAroundSolutionForBabelBug = getDefaultPart;
        return workAroundSolutionForBabelBug.call(this, ...arguments);
    }
 
    getParsedRoutes () {
        return this._parsedRoutes;
    }
 
    getParsedOptions () {
        return this._parsedOptions;
    }
 
    _parseOptions (rawOptions) {
        return new RouterOptions(rawOptions);
    }
 
    _parseRoutes (rawRoutes) {
        const parsedRoutes = {};
        for (let routeName in rawRoutes) {
            parsedRoutes[routeName] = new Route(routeName, rawRoutes[routeName]);
        }
        return parsedRoutes;
    }
}