spec-pattern-ts
Version:
Specification pattern for TypeScript
57 lines • 2.15 kB
TypeScript
/**
* spec-pattern-ts v3.0.0
* 통합된 스펙 인터페이스로 완벽한 조합 가능
*/
/**
* 통합 스펙 인터페이스
* 모든 스펙은 컨텍스트 객체를 다룹니다
* @template TContext - 스펙이 검증하는 컨텍스트 타입
*/
export interface ISpecification<TContext extends Record<string, any>> {
isSatisfiedBy(candidate: TContext): boolean;
/**
* isSatisfiedBy의 간편한 alias
* @alias isSatisfiedBy
* @param candidate - 검증할 컨텍스트 객체
* @returns 스펙 만족 여부
* @example
* ```typescript
* const isValid = spec.is(data); // spec.isSatisfiedBy(data)와 동일
* ```
*/
is(candidate: TContext): boolean;
and<TOther extends Record<string, any>>(other: ISpecification<TOther>): ISpecification<TContext & TOther>;
or<TOther extends Record<string, any>>(other: ISpecification<TOther>): ISpecification<TContext | TOther>;
not(): ISpecification<TContext>;
}
/**
* 스펙 생성 함수
*/
export declare function Spec<T, K extends string>(key: K, predicate: (value: T) => boolean): ISpecification<{
[P in K]: T;
}>;
/**
* 여러 조건을 한번에 체크하는 복합 스펙
*/
export declare function CompositeSpec<TContext extends Record<string, any>>(predicate: (context: TContext) => boolean): ISpecification<TContext>;
/**
* 스펙 빌더
*/
export declare class SpecBuilder<T> {
private predicate;
constructor(predicate: (value: T) => boolean);
as<K extends string>(key: K): ISpecification<{
[P in K]: T;
}>;
}
/**
* 스펙 정의 헬퍼
*/
export declare function define<T>(predicate: (value: T) => boolean): SpecBuilder<T>;
/**
* 편의 함수들
*/
export declare function allOf<TContext extends Record<string, any>>(...specs: ISpecification<any>[]): ISpecification<TContext>;
export declare function anyOf<TContext extends Record<string, any>>(...specs: ISpecification<any>[]): ISpecification<TContext>;
export declare function not<TContext extends Record<string, any>>(spec: ISpecification<TContext>): ISpecification<TContext>;
//# sourceMappingURL=specification-v3.d.ts.map