feat(router): implement RouterUrlParser

This commit is contained in:
vsavkin
2016-04-22 12:04:40 -07:00
committed by Victor Savkin
parent 90a1f7d5c4
commit f6985671dd
2 changed files with 72 additions and 0 deletions
@@ -0,0 +1,27 @@
import {UrlSegment, Tree} from './segments';
import {BaseException} from 'angular2/src/facade/exceptions';
export abstract class RouterUrlParser { abstract parse(url: string): Tree<UrlSegment>; }
export class DefaultRouterUrlParser extends RouterUrlParser {
parse(url: string): Tree<UrlSegment> {
if (url.length === 0) {
throw new BaseException(`Invalid url '${url}'`);
}
return new Tree<UrlSegment>(this._parseNodes(url));
}
private _parseNodes(url: string): UrlSegment[] {
let index = url.indexOf("/", 1);
let children: UrlSegment[];
let currentUrl;
if (index > -1) {
children = this._parseNodes(url.substring(index + 1));
currentUrl = url.substring(0, index);
} else {
children = [];
currentUrl = url;
}
return [new UrlSegment(currentUrl, {}, "")].concat(children);
}
}