src/moostaka.js
/**
     * handle navigation events routing to a "route" if one is defined which
     * matches the specified pathname
     *  
     * @param {string} pathname the uri to change to
     */
handle navigation events routing to a "route" if one is defined which
matches the specified pathname

@param {string} pathname the uri to change to
handle navigation events routing to a " route " if one is defined which matches the specified pathname @param { string } pathname the uri to change to
navigate(pathname) {
        if(this.onnavigate) {
            this.onnavigate(pathname);
        }
        if(!pathname || pathname === '/') {
            pathname = this.defaultRoute;
        }
        let hashParts = pathname.split('/');
        let params = {};
        for(let i = 0, len = this.routes.length; i < len; i++) {
            if(typeof this.routes[i].pattern === 'string') {
                let routeParts = this.routes[i].pattern.split('/');
                let thisRouteMatch = true;
                if(routeParts.length !== hashParts.length) {
                    thisRouteMatch = false;
                } else {
                    for(let x = 0; x < routeParts.length; x++) {
                        if(routeParts[x] === '*') {
                            break;
                        }
                        if(routeParts[x].substring(0, 1) !== ':') {
                            if(lowerCase(routeParts[x]) !== lowerCase(hashParts[x])) {
                                thisRouteMatch = false;
                            }
                        } else {
                            if(thisRouteMatch) {
                                let partName = routeParts[x].substring(1);
                                params[partName] = hashParts[x];
                            }
                        }
                    }
                }
                if(thisRouteMatch === true) {
                    return this.routes[i].handler(params);
                }
            } else {
                if(pathname.substring(1).match(this.routes[i].pattern)) {
                    return this.routes[i].handler({'hash': pathname.substring(1).split('/')});
                }
            }
        }
        if(this.defaultRoute != pathname) {
            history.pushState('data', 'home', this.defaultRoute);
            this.navigate(this.defaultRoute);
        } 
    }
navigate ( pathname ) { if ( this . onnavigate ) { this . onnavigate ( pathname ) ; } if ( ! pathname || pathname === '/' ) { pathname = this . defaultRoute ; } let hashParts = pathname . split ( '/' ) ; let params = { } ; for ( let i = 0 , len = this . routes . length ; i < len ; i ++ ) { if ( typeof this . routes [ i ] . pattern === 'string' ) { let routeParts = this . routes [ i ] . pattern . split ( '/' ) ; let thisRouteMatch = true ; if ( routeParts . length !== hashParts . length ) { thisRouteMatch = false ; } else { for ( let x = 0 ; x < routeParts . length ; x ++ ) { if ( routeParts [ x ] === '*' ) { break ; } if ( routeParts [ x ] . substring ( 0 , 1 ) !== ':' ) { if ( lowerCase ( routeParts [ x ] ) !== lowerCase ( hashParts [ x ] ) ) { thisRouteMatch = false ; } } else { if ( thisRouteMatch ) { let partName = routeParts [ x ] . substring ( 1 ) ; params [ partName ] = hashParts [ x ] ; } } } } if ( thisRouteMatch === true ) { return this . routes [ i ] . handler ( params ) ; } } else { if ( pathname . substring ( 1 ) . match ( this . routes [ i ] . pattern ) ) { return this . routes [ i ] . handler ( { 'hash' : pathname . substring ( 1 ) . split ( '/' ) } ) ; } } } if ( this . defaultRoute != pathname ) { history . pushState ( 'data' , 'home' , this . defaultRoute ) ; this . navigate ( this . defaultRoute ) ; } }
handle navigation events routing to a "route" if one is defined which
 matches the specified pathname
handle navigation events routing to a " route " if one is defined which matches the specified pathname
// if no path, go to default // if segment count is not equal the pattern will not match // A wildcard is found, lets break and return what we have already // if not optional params we check it // this is an optional param that the user will want but only // if route still matches... // if route is matched // no routes were matched. try defaultRoute if that is not the route being attempted // else we should perhaps implement an error route?