36 lines
802 B
TypeScript
36 lines
802 B
TypeScript
import { shallowEqual } from "shallow-equal-object";
|
|
|
|
export abstract class ValueObject<T> {
|
|
protected readonly _value: T;
|
|
|
|
protected constructor(value: T) {
|
|
this._value = typeof value === "object" && value !== null ? Object.freeze(value) : value;
|
|
|
|
Object.freeze(this);
|
|
}
|
|
|
|
equals(other: ValueObject<T>): boolean {
|
|
if (!(other instanceof ValueObject)) {
|
|
return false;
|
|
}
|
|
|
|
if (other._value === undefined || other._value === null) {
|
|
return false;
|
|
}
|
|
|
|
return shallowEqual(this._value, other._value);
|
|
}
|
|
|
|
getValue(): T {
|
|
return this._value;
|
|
}
|
|
|
|
isEmpty(): boolean {
|
|
return this._value === null || this._value === undefined;
|
|
}
|
|
|
|
toString(): string {
|
|
return this._value !== null && this._value !== undefined ? String(this._value) : "";
|
|
}
|
|
}
|