spec-pattern-ts
Version:
Specification pattern for TypeScript
133 lines (131 loc) • 3.21 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/specification-v3.ts
var BaseSpecification = class {
/**
* isSatisfiedBy의 간편한 alias
* @alias isSatisfiedBy
*/
is(candidate) {
return this.isSatisfiedBy(candidate);
}
and(other) {
return new AndSpecification(this, other);
}
or(other) {
return new OrSpecification(this, other);
}
not() {
return new NotSpecification(this);
}
};
__name(BaseSpecification, "BaseSpecification");
var SingleKeySpecification = class extends BaseSpecification {
constructor(key, predicate) {
super();
this.key = key;
this.predicate = predicate;
}
isSatisfiedBy(candidate) {
return this.key in candidate && this.predicate(candidate[this.key]);
}
};
__name(SingleKeySpecification, "SingleKeySpecification");
var AndSpecification = class extends BaseSpecification {
constructor(left, right) {
super();
this.left = left;
this.right = right;
}
isSatisfiedBy(candidate) {
const leftResult = this.left.isSatisfiedBy(candidate);
const rightResult = this.right.isSatisfiedBy(candidate);
return leftResult && rightResult;
}
};
__name(AndSpecification, "AndSpecification");
var OrSpecification = class extends BaseSpecification {
constructor(left, right) {
super();
this.left = left;
this.right = right;
}
isSatisfiedBy(candidate) {
try {
if (this.left.isSatisfiedBy(candidate))
return true;
} catch {
}
try {
if (this.right.isSatisfiedBy(candidate))
return true;
} catch {
}
return false;
}
};
__name(OrSpecification, "OrSpecification");
var NotSpecification = class extends BaseSpecification {
constructor(spec) {
super();
this.spec = spec;
}
isSatisfiedBy(candidate) {
return !this.spec.isSatisfiedBy(candidate);
}
};
__name(NotSpecification, "NotSpecification");
function Spec(key, predicate) {
return new SingleKeySpecification(key, predicate);
}
__name(Spec, "Spec");
function CompositeSpec(predicate) {
return new PredicateSpecification(predicate);
}
__name(CompositeSpec, "CompositeSpec");
var PredicateSpecification = class extends BaseSpecification {
constructor(predicate) {
super();
this.predicate = predicate;
}
isSatisfiedBy(candidate) {
return this.predicate(candidate);
}
};
__name(PredicateSpecification, "PredicateSpecification");
var SpecBuilder = class {
constructor(predicate) {
this.predicate = predicate;
}
as(key) {
return Spec(key, this.predicate);
}
};
__name(SpecBuilder, "SpecBuilder");
function define(predicate) {
return new SpecBuilder(predicate);
}
__name(define, "define");
function allOf(...specs) {
return specs.reduce((acc, spec) => acc.and(spec));
}
__name(allOf, "allOf");
function anyOf(...specs) {
return specs.reduce((acc, spec) => acc.or(spec));
}
__name(anyOf, "anyOf");
function not(spec) {
return spec.not();
}
__name(not, "not");
export {
CompositeSpec,
Spec,
SpecBuilder,
allOf,
anyOf,
define,
not
};
/*! For license information please see index.mjs.LEGAL.txt */
//# sourceMappingURL=index.mjs.map