1 | // class returns a unique id to use for the column. it checks the existing columns, and if the requested
|
2 | // id is already taken, it will start appending numbers until it gets a unique id.
|
3 | // eg, if the col field is 'name', it will try ids: {name, name_1, name_2...}
|
4 | // if no field or id provided in the col, it will try the ids of natural numbers
|
5 | import {Utils as _} from "../utils";
|
6 |
|
7 | export class ColumnKeyCreator {
|
8 |
|
9 | private existingKeys: string[] = [];
|
10 |
|
11 | public getUniqueKey(colId: string, colField: string): string {
|
12 |
|
13 | // in case user passed in number for colId, convert to string
|
14 | colId = _.toStringOrNull(colId);
|
15 |
|
16 | let count = 0;
|
17 | while (true) {
|
18 |
|
19 | let idToTry: string;
|
20 | if (colId) {
|
21 | idToTry = colId;
|
22 | if (count!==0) {
|
23 | idToTry += '_' + count;
|
24 | }
|
25 | } else if (colField) {
|
26 | idToTry = colField;
|
27 | if (count!==0) {
|
28 | idToTry += '_' + count;
|
29 | }
|
30 | } else {
|
31 | idToTry = '' + count;
|
32 | }
|
33 |
|
34 | if (this.existingKeys.indexOf(idToTry) < 0) {
|
35 | this.existingKeys.push(idToTry);
|
36 | return idToTry;
|
37 | }
|
38 |
|
39 | count++;
|
40 | }
|
41 | }
|
42 |
|
43 | } |
\ | No newline at end of file |