UNPKG

rich-domain

Version:

This package provide utils file and interfaces to assistant build a complex application with domain driving design

103 lines 4.11 kB
import { UID } from "../types"; /** * @description Represents a unique identifier for Entities or Aggregates, providing methods * to generate and manipulate ID values, including the ability to create new IDs, convert them * to a shorter format, clone them, and check for equality. */ export declare class ID<T = string> implements UID<T> { private _value; private _isNew; private _createdAt; private readonly MAX_SIZE; private constructor(); /** * @description Marks the current ID as new. * @private */ private setAsNew; /** * @description Convert the current ID value into a shortened, 16-byte version. * If the current ID value is shorter than 16 bytes, a UUID is prepended to ensure * sufficient length. * @returns An updated `ID` instance with a short, 16-byte value. */ toShort(): UID<string>; /** * @description Retrieves the current ID value. * @returns The ID value as a string. */ value(): string; /** * @description Indicates whether this `ID` instance was created as a new, * previously uninitialized ID (no value passed to `create()`), or if it was * initialized from a provided value. * @returns `true` if the ID is new; otherwise, `false`. * * @example * ```typescript * ID.create("some-value").isNew(); // returns false * ID.create().isNew(); // returns true, since no argument was provided * ``` */ isNew(): boolean; /** * @description Retrieves the creation date of this `ID` instance. * @returns The `Date` object representing when this ID was created or last modified. */ createdAt(): Date; /** * @description Checks if the current ID is short (16 bytes). * @returns `true` if the ID is 16 bytes long, `false` otherwise. */ isShort(): boolean; /** * @description Compares the current ID against another `UID` instance by value. * @param id Another `UID` to compare to. * @returns `true` if both IDs share the same value type and string value, otherwise `false`. */ equal(id: UID<any>): boolean; /** * @description Alias for `equal`. Compares the current ID against another `UID` instance by value. * @param id Another `UID` to compare to. * @returns `true` if both IDs share the same value, otherwise `false`. */ isEqual(id: UID<any>): boolean; /** * @description Performs a deep comparison by serializing both IDs and checking if * the JSON representation is identical. * @param id Another `UID` to compare to. * @returns `true` if both IDs are deeply equal; otherwise `false`. */ deepEqual(id: UID<any>): boolean; /** * @description Creates a new `UID` instance cloned from the current one, but marks * the new clone as a new ID. * @returns A cloned `UID` instance that is considered new. */ cloneAsNew(): UID<string>; /** * @description Creates a clone of the current `UID` instance with the same value * and properties, but without marking it as new. * @returns A cloned `UID` instance identical to the current one. */ clone(): UID<T>; /** * @description Creates a new short (16-byte) ID. If no `id` value is provided, * a new UUID is generated and then shortened. * @param id An optional string or number to use as the base value. * @returns A `UID` instance with a short, 16-byte value. */ static short(id?: string | number): UID<string>; /** * @description Creates a new `ID` instance. If no `id` value is provided, a new * UUID is generated. If an `id` is provided, that value is used directly. * @param id Optional value to initialize the ID with. If not provided, a new UUID is created. * @returns A `UID` instance. */ static create<T = string | number>(id?: T): UID<string>; } export default ID; export declare const id: typeof ID; export declare const Uid: typeof ID.create; export declare const Id: typeof ID.create; //# sourceMappingURL=id.d.ts.map