UNPKG

@synet/patterns

Version:

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

69 lines (68 loc) 1.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Specification = void 0; /** * Base specification pattern implementation. * * Specifications encapsulate query logic, making it reusable and composable. * They represent a predicate that determines if an object satisfies some criteria. */ class Specification { /** * Combines this specification with another using AND operator */ and(other) { return new AndSpecification(this, other); } /** * Combines this specification with another using OR operator */ or(other) { return new OrSpecification(this, other); } /** * Negates this specification */ not() { return new NotSpecification(this); } } exports.Specification = Specification; /** * Specification that combines two specifications with logical AND */ class AndSpecification extends Specification { constructor(left, right) { super(); this.left = left; this.right = right; } isSatisfiedBy(candidate) { return (this.left.isSatisfiedBy(candidate) && this.right.isSatisfiedBy(candidate)); } } /** * Specification that combines two specifications with logical OR */ class OrSpecification extends Specification { constructor(left, right) { super(); this.left = left; this.right = right; } isSatisfiedBy(candidate) { return (this.left.isSatisfiedBy(candidate) || this.right.isSatisfiedBy(candidate)); } } /** * Specification that negates another specification */ class NotSpecification extends Specification { constructor(specification) { super(); this.specification = specification; } isSatisfiedBy(candidate) { return !this.specification.isSatisfiedBy(candidate); } }