-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalue-object.ts
More file actions
36 lines (30 loc) · 843 Bytes
/
value-object.ts
File metadata and controls
36 lines (30 loc) · 843 Bytes
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
import { shallowEqual } from 'fast-equals';
export interface IValueObject<T = unknown> {
get value(): T;
equals(vo?: IValueObject): boolean;
}
/**
* A small immutable object whose equality is not based on identity but purely on its attributes.
*
* ## Rules
* - Value objects are immutable;
* - Value objects can reference other objects;
*
* ## Notes:
* - Value objects can/should hold validation for their data. When we have validation rules.
*/
export abstract class ValueObject<T> implements IValueObject<T> {
protected readonly _data: T;
constructor(props: T) {
this._data = Object.freeze(props);
}
public equals(vo?: ValueObject<T>): boolean {
if (vo?.value === undefined) {
return false;
}
return shallowEqual(this._data, vo.value);
}
public get value() {
return this._data;
}
}