angular/packages/core/src/render3/node_assert.ts
2017-12-21 21:40:58 -08:00

36 lines
1.2 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {assertEqual, assertNotEqual} from './assert';
import {LNode, LNodeFlags} from './l_node';
export function assertNodeType(node: LNode, type: LNodeFlags) {
assertNotEqual(node, null, 'node');
assertEqual(node.flags & LNodeFlags.TYPE_MASK, type, 'Node.type', typeSerializer);
}
export function assertNodeOfPossibleTypes(node: LNode, ...types: LNodeFlags[]) {
assertNotEqual(node, null, 'node');
const nodeType = (node.flags & LNodeFlags.TYPE_MASK);
for (let i = 0; i < types.length; i++) {
if (nodeType === types[i]) {
return;
}
}
throw new Error(
`Expected node of possible types: ${types.map(typeSerializer).join(', ')} but got ${typeSerializer(nodeType)}`);
}
function typeSerializer(type: LNodeFlags): string {
if (type == LNodeFlags.Projection) return 'Projection';
if (type == LNodeFlags.Container) return 'Container';
if (type == LNodeFlags.View) return 'View';
if (type == LNodeFlags.Element) return 'Element';
return '??? ' + type + ' ???';
}