X Tutup
Skip to content

Commit b691da2

Browse files
matskoalexeagle
authored andcommitted
chore(facade): add enum index lookup support
1 parent 8e3e450 commit b691da2

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

modules/angular2/src/facade/lang.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ dynamic deserializeEnum(num val, Map<num, dynamic> values) {
5454
return values[val];
5555
}
5656

57+
String resolveEnumToken(enumValue, val) {
58+
// turn Enum.Token -> Token
59+
return val.toString().replaceFirst(new RegExp('^.+\\.'),'');
60+
}
61+
5762
class StringWrapper {
5863
static String fromCharCode(int code) {
5964
return new String.fromCharCode(code);

modules/angular2/src/facade/lang.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ export function deserializeEnum(val, values: Map<number, any>): any {
195195
return val;
196196
}
197197

198+
export function resolveEnumToken(enumValue, val): string {
199+
return enumValue[val];
200+
}
201+
198202
export class StringWrapper {
199203
static fromCharCode(code: number): string { return String.fromCharCode(code); }
200204

@@ -468,4 +472,4 @@ export function isPrimitive(obj: any): boolean {
468472

469473
export function hasConstructor(value: Object, type: Type): boolean {
470474
return value.constructor === type;
471-
}
475+
}

modules/angular2/test/facade/lang_spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ import {
55
RegExpMatcherWrapper,
66
StringWrapper,
77
CONST_EXPR,
8-
hasConstructor
8+
hasConstructor,
9+
resolveEnumToken
910
} from 'angular2/src/facade/lang';
1011

12+
enum UsefulEnum {
13+
MyToken,
14+
MyOtherToken
15+
}
16+
1117
class MySuperclass {}
1218
class MySubclass extends MySuperclass {}
1319

@@ -124,6 +130,16 @@ export function main() {
124130
});
125131
});
126132

133+
describe('resolveEnumToken', () => {
134+
it("should resolve a token given an enum and index values", () => {
135+
var token = UsefulEnum.MyToken;
136+
expect(resolveEnumToken(UsefulEnum, token)).toEqual('MyToken');
137+
138+
token = UsefulEnum.MyOtherToken;
139+
expect(resolveEnumToken(UsefulEnum, token)).toEqual('MyOtherToken');
140+
});
141+
});
142+
127143
describe('hasConstructor', () => {
128144
it("should be true when the type matches",
129145
() => { expect(hasConstructor(new MySuperclass(), MySuperclass)).toEqual(true); });

0 commit comments

Comments
 (0)
X Tutup