chore(facade): add enum index lookup support

This commit is contained in:
Matias Niemelä 2016-03-08 16:38:50 -08:00 committed by Alex Eagle
parent 8e3e45097a
commit b691da26af
3 changed files with 27 additions and 2 deletions

View File

@ -54,6 +54,11 @@ dynamic deserializeEnum(num val, Map<num, dynamic> values) {
return values[val];
}
String resolveEnumToken(enumValue, val) {
// turn Enum.Token -> Token
return val.toString().replaceFirst(new RegExp('^.+\\.'),'');
}
class StringWrapper {
static String fromCharCode(int code) {
return new String.fromCharCode(code);

View File

@ -195,6 +195,10 @@ export function deserializeEnum(val, values: Map<number, any>): any {
return val;
}
export function resolveEnumToken(enumValue, val): string {
return enumValue[val];
}
export class StringWrapper {
static fromCharCode(code: number): string { return String.fromCharCode(code); }
@ -468,4 +472,4 @@ export function isPrimitive(obj: any): boolean {
export function hasConstructor(value: Object, type: Type): boolean {
return value.constructor === type;
}
}

View File

@ -5,9 +5,15 @@ import {
RegExpMatcherWrapper,
StringWrapper,
CONST_EXPR,
hasConstructor
hasConstructor,
resolveEnumToken
} from 'angular2/src/facade/lang';
enum UsefulEnum {
MyToken,
MyOtherToken
}
class MySuperclass {}
class MySubclass extends MySuperclass {}
@ -124,6 +130,16 @@ export function main() {
});
});
describe('resolveEnumToken', () => {
it("should resolve a token given an enum and index values", () => {
var token = UsefulEnum.MyToken;
expect(resolveEnumToken(UsefulEnum, token)).toEqual('MyToken');
token = UsefulEnum.MyOtherToken;
expect(resolveEnumToken(UsefulEnum, token)).toEqual('MyOtherToken');
});
});
describe('hasConstructor', () => {
it("should be true when the type matches",
() => { expect(hasConstructor(new MySuperclass(), MySuperclass)).toEqual(true); });