-
Notifications
You must be signed in to change notification settings - Fork 2
/
RecordUtil.ts
40 lines (37 loc) · 1.36 KB
/
RecordUtil.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
import { ArrayUtil } from '../util';
import { DataAnnotation } from './DataAnnotation';
import { NullRecord } from './NullRecord';
import { Property } from './Property';
import {PropertyDef} from "./PropertyDef";
import { Record } from './Record';
import { RecordBuffer } from './RecordBuffer';
import { RecordImpl } from './RecordImpl';
import { TypeNames } from './types';
/**
* Utility for working with Records
*/
export class RecordUtil {
public static newRecord(id: string, properties: Property[], annotations: DataAnnotation[] = []): Record {
return new RecordImpl(id, ArrayUtil.copy(properties), ArrayUtil.copy(annotations), TypeNames.RecordTypeName);
}
public static isRecord(o: any): boolean {
return o instanceof RecordImpl || o instanceof RecordBuffer || o instanceof NullRecord;
}
public static unionRight(l1: Array<Property>, l2: Array<Property>): Array<Property> {
const result: Array<Property> = ArrayUtil.copy(l1);
l2.forEach((p2: Property) => {
if (
!l1.some((p1: Property, i) => {
if (p1.name === p2.name) {
result[i] = p2;
return true;
}
return false;
})
) {
result.push(p2);
}
});
return result;
}
}