Presupuestador_web/shared/lib/contexts/common/domain/entities/ValueObject.ts
2024-04-23 17:29:38 +02:00

40 lines
749 B
TypeScript

import { shallowEqual } from "shallow-equal-object";
type Primitive = string | boolean | number;
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>;
};
*/