Presupuestador_web/shared/lib/contexts/common/domain/entities/ValueObject.ts

40 lines
775 B
TypeScript
Raw Normal View History

2024-04-23 15:29:38 +00:00
import { shallowEqual } from "shallow-equal-object";
2024-07-15 13:24:22 +00:00
export type Primitive = string | boolean | number | null | undefined;
2024-04-23 15:29:38 +00:00
export interface IValueObjectOptions {
label?: string;
path?: string;
}
export abstract class ValueObject<T> {
readonly props: T;
constructor(value: T) {
this.props = typeof value === "object" ? Object.freeze(value) : value;
}
get value(): T {
return this.props;
}
public abstract toPrimitive(): Primitive;
public equals(vo: ValueObject<T>): boolean {
if (vo === null || vo === undefined) {
return false;
}
if (vo.props === undefined) {
return false;
}
return shallowEqual(this.props, vo.props);
}
}
/*export type TComposedValueObject = {
[key: string]: ValueObject<any>;
};
*/