FactuGES_ERP/packages/rdx-ddd/src/value-objects/value-object.ts

26 lines
555 B
TypeScript
Raw Normal View History

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-09-04 10:02:24 +00:00
abstract getProps(): any;
2025-02-20 18:55:24 +00:00
2025-04-01 14:26:15 +00:00
abstract toPrimitive(): 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
}
}