UNPKG

@synet/patterns

Version:

Robust, battle-tested collection of stable patterns used in Synet packages

59 lines (58 loc) 1.85 kB
/** * Base class for Value Objects - immutable objects defined by their property values. * * Value Objects encapsulate domain concepts that have no distinct identity. * Two Value Objects are equal when all their properties are equal. * * @see /docs/value-object.md for detailed documentation and examples * * @template T - The shape of properties that define this Value Object * @example * class Email extends ValueObject<{ address: string }> { * private constructor(props: { address: string }) { * super(props); * } * * public static create(address: string): Result<Email> { * if (!address.includes('@')) { * return Result.fail('Email must contain @ symbol'); * } * return Result.success(new Email({ address })); * } * * get value(): string { * return this.props.address; * } * } */ export declare abstract class ValueObject<T extends object> { /** * The properties that define this Value Object. * These are protected to prevent direct modification from outside. */ protected readonly props: Readonly<T>; /** * Creates a new Value Object with the given properties. * The properties are frozen to ensure immutability. */ constructor(props: T); /** * Checks if this Value Object is equal to another Value Object. * Two Value Objects are considered equal if all their properties are equal. */ equals(vo?: ValueObject<T> | null): boolean; /** * Deep comparison of properties. * This handles nested objects and arrays properly. */ private isEqual; /** * Returns a string representation of this Value Object. */ toString(): string; /** * Returns a shallow copy of the props. * This can be used when you need to access the raw data. */ protected toObject(): T; }