Uecko_ERP/apps/server/src/common/domain/value-objects/value-object.ts
2025-02-04 19:25:10 +01:00

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