cleanup: fix type errors when compiling with strictNullChecks enabled

This commit is contained in:
vsavkin 2016-05-24 13:22:49 -07:00
parent 1f98519380
commit 1a4e911b8b
2 changed files with 13 additions and 12 deletions

View File

@ -110,7 +110,8 @@ function matchIndex(config: Route[], url: TreeNode<UrlSegment>): MatchResult | n
for (let r of config) { for (let r of config) {
if (r.index) { if (r.index) {
const outlet = r.outlet ? r.outlet : PRIMARY_OUTLET; const outlet = r.outlet ? r.outlet : PRIMARY_OUTLET;
return new MatchResult(r.component, r.children, [], {}, [url], [], outlet); const children = r.children ? r.children : [];
return new MatchResult(r.component, children, [], {}, [url], [], outlet);
} }
} }
return null; return null;
@ -122,7 +123,7 @@ function matchWithParts(route: Route, url: TreeNode<UrlSegment>): MatchResult |
const path = route.path.startsWith("/") ? route.path.substring(1) : route.path; const path = route.path.startsWith("/") ? route.path.substring(1) : route.path;
if (path === "**") { if (path === "**") {
const consumedUrl = []; const consumedUrl = [];
let u = url; let u:TreeNode<UrlSegment>|null = url;
while (u) { while (u) {
consumedUrl.push(u.value); consumedUrl.push(u.value);
u = first(u.children); u = first(u.children);

View File

@ -12,53 +12,53 @@ export class Tree<T> {
} }
children(t: T): T[] { children(t: T): T[] {
const n = _findNode(t, this._root); const n = findNode(t, this._root);
return n ? n.children.map(t => t.value) : []; return n ? n.children.map(t => t.value) : [];
} }
firstChild(t: T): T | null { firstChild(t: T): T | null {
const n = _findNode(t, this._root); const n = findNode(t, this._root);
return n && n.children.length > 0 ? n.children[0].value : null; return n && n.children.length > 0 ? n.children[0].value : null;
} }
pathFromRoot(t: T): T[] { return _findPath(t, this._root, []).map(s => s.value); } pathFromRoot(t: T): T[] { return findPath(t, this._root, []).map(s => s.value); }
contains(tree: Tree<T>): boolean { return _contains(this._root, tree._root); } contains(tree: Tree<T>): boolean { return contains(this._root, tree._root); }
} }
export function rootNode<T>(tree: Tree<T>): TreeNode<T> { export function rootNode<T>(tree: Tree<T>): TreeNode<T> {
return tree._root; return tree._root;
} }
function _findNode<T>(expected: T, c: TreeNode<T>): TreeNode<T> | null { function findNode<T>(expected: T, c: TreeNode<T>): TreeNode<T> | null {
if (expected === c.value) return c; if (expected === c.value) return c;
for (let cc of c.children) { for (let cc of c.children) {
const r = _findNode(expected, cc); const r = findNode(expected, cc);
if (r) return r; if (r) return r;
} }
return null; return null;
} }
function _findPath<T>(expected: T, c: TreeNode<T>, collected: TreeNode<T>[]): TreeNode<T>[] { function findPath<T>(expected: T, c: TreeNode<T>, collected: TreeNode<T>[]): TreeNode<T>[] {
collected.push(c); collected.push(c);
if (expected === c.value) return collected; if (expected === c.value) return collected;
for (let cc of c.children) { for (let cc of c.children) {
const cloned = collected.slice(0); const cloned = collected.slice(0);
const r = _findPath(expected, cc, cloned); const r = findPath(expected, cc, cloned);
if (r) return r; if (r) return r;
} }
return []; return [];
} }
function _contains<T>(tree: TreeNode<T>, subtree: TreeNode<T>): boolean { function contains<T>(tree: TreeNode<T>, subtree: TreeNode<T>): boolean {
if (tree.value !== subtree.value) return false; if (tree.value !== subtree.value) return false;
for (let subtreeNode of subtree.children) { for (let subtreeNode of subtree.children) {
const s = tree.children.filter(child => child.value === subtreeNode.value); const s = tree.children.filter(child => child.value === subtreeNode.value);
if (s.length === 0) return false; if (s.length === 0) return false;
if (!_contains(s[0], subtreeNode)) return false; if (!contains(s[0], subtreeNode)) return false;
} }
return true; return true;