2025-01-29 19:02:59 +00:00
|
|
|
import { shallowEqual } from "shallow-equal-object";
|
|
|
|
|
|
|
|
|
|
export abstract class ValueObject<T> {
|
2025-02-20 18:55:24 +00:00
|
|
|
protected readonly props: T;
|
2025-01-29 19:02:59 +00:00
|
|
|
|
2025-02-20 18:55:24 +00:00
|
|
|
protected constructor(props: T) {
|
|
|
|
|
this.props = Object.freeze(props); // 🔒 Garantiza inmutabilidad
|
2025-01-29 19:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-20 18:55:24 +00:00
|
|
|
abstract getValue(): any;
|
|
|
|
|
|
2025-01-29 19:02:59 +00:00
|
|
|
equals(other: ValueObject<T>): boolean {
|
|
|
|
|
if (!(other instanceof ValueObject)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-20 18:55:24 +00:00
|
|
|
if (other.props === undefined || other.props === null) {
|
2025-01-29 19:02:59 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-20 18:55:24 +00:00
|
|
|
return shallowEqual(this.props, other.props);
|
2025-01-29 19:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-20 18:55:24 +00:00
|
|
|
/*isEmpty(): boolean {
|
|
|
|
|
return this.props === null || this.props === undefined;
|
|
|
|
|
}*/
|
2025-01-29 19:02:59 +00:00
|
|
|
|
2025-02-20 18:55:24 +00:00
|
|
|
/*toString(): string {
|
|
|
|
|
return this.props !== null && this.props !== undefined ? String(this.props) : "";
|
|
|
|
|
}*/
|
2025-01-29 19:02:59 +00:00
|
|
|
}
|