/
lookup.ts
53 lines (48 loc) · 1.43 KB
/
lookup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
export interface LookupOptions<TItem> {
idField?: string;
parentIdField?: string;
textField?: string;
}
export interface Lookup<TItem> {
items: TItem[];
itemById: { [key: string]: TItem };
idField: string;
parentIdField: string;
textField: string;
}
export class Lookup<TItem> {
public items: TItem[] = [];
public itemById: { [key: string]: TItem } = {};
public idField: string;
public parentIdField: string;
public textField: string;
constructor(options: LookupOptions<TItem>, items?: TItem[]) {
options = options || {};
this.idField = options.idField;
this.parentIdField = options.parentIdField;
this.textField = options.textField;
if (items != null)
this.update(items);
}
update?(value: TItem[]) {
this.items = [];
this.itemById = {};
if (value) {
for (var k of value) {
if (k == null || typeof k !== "object") // special case for distinct lookup
this.items.push({ [this.idField]: k, [this.textField]: k } as any);
else
this.items.push(k);
}
}
var idField = this.idField;
if (idField) {
for (var r of this.items) {
var v = (r as any)[idField];
if (v != null) {
this.itemById[v] = r;
}
}
}
}
}