-
Notifications
You must be signed in to change notification settings - Fork 2
/
Entity.ts
54 lines (44 loc) · 1.04 KB
/
Entity.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
54
/**
* Entity.ts
*
* @copyright Vitalii Savchuk <esvit666@gmail.com>
* @package einvoicing
* @licence MIT https://opensource.org/licenses/MIT
*/
import { EntityId } from './EntityId';
const isEntity = <T, E, M extends EntityId<E>>(v: Entity<T, E, M>): v is Entity<T, E, M> => {
return v instanceof Entity
}
export abstract class Entity<T, E, H extends EntityId<E>> {
protected readonly _id: H;
protected props: T;
protected constructor(props: T, id?: H|E) {
if (id) {
this._id = <H>(typeof id === 'object' ? id : new EntityId<E>(id));
} else {
this._id = <H>(new EntityId());
}
this.props = props
}
public equals(object?: Entity<T, E, H>): boolean {
if (object === null || object === undefined) {
return false
}
if (this === object) {
return true
}
if (!isEntity(object)) {
return false
}
return this._id.equals(object._id);
}
get id(): H {
return this._id
}
toPrimitive(): T {
return this.props
}
toJSON(): T {
return this.toPrimitive()
}
}