diff --git a/modules/angular2/src/facade/lang.dart b/modules/angular2/src/facade/lang.dart index 6c8e63291f..b7ed18ddfc 100644 --- a/modules/angular2/src/facade/lang.dart +++ b/modules/angular2/src/facade/lang.dart @@ -54,6 +54,11 @@ dynamic deserializeEnum(num val, Map 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); diff --git a/modules/angular2/src/facade/lang.ts b/modules/angular2/src/facade/lang.ts index eee138d4fc..8cc1c835ac 100644 --- a/modules/angular2/src/facade/lang.ts +++ b/modules/angular2/src/facade/lang.ts @@ -195,6 +195,10 @@ export function deserializeEnum(val, values: Map): 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; -} \ No newline at end of file +} diff --git a/modules/angular2/test/facade/lang_spec.ts b/modules/angular2/test/facade/lang_spec.ts index 352b1a75f7..67c010487a 100644 --- a/modules/angular2/test/facade/lang_spec.ts +++ b/modules/angular2/test/facade/lang_spec.ts @@ -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); });