spec-pattern-ts
Version:
Specification pattern for TypeScript
139 lines (136 loc) • 3.8 kB
JavaScript
;
var SpecPattern = (() => {
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
CompositeSpec: () => CompositeSpec,
Spec: () => Spec,
SpecBuilder: () => SpecBuilder,
allOf: () => allOf,
anyOf: () => anyOf,
define: () => define,
not: () => not
});
// 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);
}
};
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]);
}
};
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;
}
};
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;
}
};
var NotSpecification = class extends BaseSpecification {
constructor(spec) {
super();
this.spec = spec;
}
isSatisfiedBy(candidate) {
return !this.spec.isSatisfiedBy(candidate);
}
};
function Spec(key, predicate) {
return new SingleKeySpecification(key, predicate);
}
function CompositeSpec(predicate) {
return new PredicateSpecification(predicate);
}
var PredicateSpecification = class extends BaseSpecification {
constructor(predicate) {
super();
this.predicate = predicate;
}
isSatisfiedBy(candidate) {
return this.predicate(candidate);
}
};
var SpecBuilder = class {
constructor(predicate) {
this.predicate = predicate;
}
as(key) {
return Spec(key, this.predicate);
}
};
function define(predicate) {
return new SpecBuilder(predicate);
}
function allOf(...specs) {
return specs.reduce((acc, spec) => acc.and(spec));
}
function anyOf(...specs) {
return specs.reduce((acc, spec) => acc.or(spec));
}
function not(spec) {
return spec.not();
}
return __toCommonJS(index_exports);
})();
//# sourceMappingURL=index.js.map