All files / src/router router.ts

100% Statements 14/14
100% Branches 3/3
100% Functions 12/12
100% Lines 14/14

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 101 102 103 104 105 106 107 108 109 110 111 112 11324x             24x               218x                   383x                   23x                   68x             2x               2x               89x               15x               2x               2x               178x                 2x      
import { Logger } from '../logger';
import { IDictionary } from '../utils';
import { IRoute } from './interfaces';
 
/**
 * Class that encapsulate a router defined by user
 */
export class Router {
	private static instance: any;
 
	/**
	 * Set the Router instance
	 * @param instance
	 */
	public static setInstance(instance: any) {
		Router.instance = instance;
	}
 
	/**
	 * Call an Router method
	 * @param method
	 * @param args
	 * @returns
	 */
	private static callInstanceMethod(method: string, args: any[] = []) {
		return Router.instance ? Router.instance[method](...args) : Logger.warn('Router instance not defined');
	}
 
	/**
	 * Call Router push method
	 * @param path
	 * @param onComplete
	 * @param onAbort
	 */
	public static push(path: any, onComplete?: (...args: any) => any, onAbort?: any) {
		Router.callInstanceMethod('push', [path, onComplete, onAbort]);
	}
 
	/**
	 * Call Router replace method
	 * @param path
	 * @param onComplete
	 * @param onAbort
	 */
	public static replace(path: any, onComplete?: (...args: any) => any, onAbort?: any): void {
		Router.callInstanceMethod('replace', [path, onComplete, onAbort]);
	}
 
	/**
	 * Call Router back method
	 */
	public static back(): void {
		Router.callInstanceMethod('back');
	}
 
	/**
	 * Call Router getCurrentRoute method returning the current route
	 * @returns
	 */
	public static getCurrentRoute(): IRoute {
		return Router.callInstanceMethod('getCurrentRoute');
	}
 
	/**
	 * Call Router getPath method returning the path
	 * @returns
	 */
	public static getPath(): string {
		return Router.callInstanceMethod('getPath');
	}
 
	/**
	 * Call Router getPath method returning the hash
	 * @returns
	 */
	public static getHash(): string {
		return Router.callInstanceMethod('getHash');
	}
 
	/**
	 * Call Router getPath method returning the query
	 * @returns
	 */
	public static getQuery(): IDictionary<string | (string | null)[]> {
		return Router.callInstanceMethod('getQuery');
	}
 
	/**
	 * Call Router getPath method returning the params
	 * @returns
	 */
	public static getParams(): IDictionary<string> {
		return Router.callInstanceMethod('getParams');
	}
 
	/**
	 * Call Router getFullPath method returning the full path
	 * @returns
	 */
	public static getFullPath(): string {
		return Router.callInstanceMethod('getFullPath');
	}
 
	/**
	 * Call Router afterEach method
	 * @param hook
	 * @returns
	 */
	public static afterEach(hook: (to: any, from: any) => any) {
		return Router.callInstanceMethod('afterEach', [hook]);
	}
}