@synet/patterns
Version:
Robust, battle-tested collection of stable patterns used in Synet packages
50 lines (49 loc) • 1.38 kB
TypeScript
import { Result } from "./result";
import { ValueObject } from "./value-object";
/**
* Represents a unique identifier.
*
* UniqueId encapsulates the concept of identity and provides type safety for IDs.
* By default, it uses UUID v4, but can accept custom ID values when provided.
*
* @example
* // Generate a new ID
* const id = new UniqueId();
*
* // Use an existing ID
* const existingId = UniqueId.create("550e8400-e29b-41d4-a716-446655440000");
* if (existingId.isSuccess) {
* const id = existingId.value;
* console.log(id.toString()); // "550e8400-e29b-41d4-a716-446655440000"
* }
*/
export declare class UniqueId extends ValueObject<{
value: string;
}> {
/**
* Creates a new UniqueId.
* If no id is provided, generates a new UUID v4.
*
* @param id Optional existing ID value
*/
constructor(id?: string);
/**
* Creates a UniqueId with validation.
*
* @param id The ID string to validate
* @returns A Result containing either the UniqueId or an error
*/
static create(id: string): Result<UniqueId>;
/**
* Returns the string representation of this ID.
*/
toString(): string;
/**
* Returns the string representation when converted to JSON.
*/
toJSON(): string;
/**
* Creates a deep copy of this UniqueId.
*/
clone(): UniqueId;
}