1 | // class returns unique instance id's for columns.
|
2 | // eg, the following calls (in this order) will result in:
|
3 | //
|
4 | // getInstanceIdForKey('country') => 0
|
5 | // getInstanceIdForKey('country') => 1
|
6 | // getInstanceIdForKey('country') => 2
|
7 | // getInstanceIdForKey('country') => 3
|
8 | // getInstanceIdForKey('age') => 0
|
9 | // getInstanceIdForKey('age') => 1
|
10 | // getInstanceIdForKey('country') => 4
|
11 | export class GroupInstanceIdCreator {
|
12 |
|
13 | // this map contains keys to numbers, so we remember what the last call was
|
14 | private existingIds: any = {};
|
15 |
|
16 | public getInstanceIdForKey(key: string): number {
|
17 | let lastResult = this.existingIds[key];
|
18 | let result: number;
|
19 | if (typeof lastResult !== 'number') {
|
20 | // first time this key
|
21 | result = 0;
|
22 | } else {
|
23 | result = lastResult + 1;
|
24 | }
|
25 |
|
26 | this.existingIds[key] = result;
|
27 |
|
28 | return result;
|
29 | }
|
30 |
|
31 | } |
\ | No newline at end of file |