Uecko_ERP/apps/server/src/contexts/common/domain/value-object.ts
2025-01-30 11:45:31 +01:00

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) : "";
}
}