@barchart/common-js
Version:
Library of common JavaScript utilities
219 lines (213 loc) • 6.05 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Specification_exports = {};
__export(Specification_exports, {
And: () => And,
Not: () => Not,
Or: () => Or,
default: () => Specification
});
module.exports = __toCommonJS(Specification_exports);
var assert = __toESM(require("./../lang/assert.js"));
class Specification {
constructor() {
}
/**
* Evaluates the specification, returning true or false.
*
* @public
* @param {*=} data
* @returns {boolean}
*/
evaluate(data) {
return this._evaluate(data);
}
/**
* @protected
* @param {*=} data
* @returns {boolean}
*/
_evaluate(data) {
return false;
}
/**
* Wraps the current instance and another {@link Specification} into a new
* specification which only evaluates to true when both wrapped specifications
* evaluate to true.
*
* @public
* @param {Specification} other
* @returns {And}
*/
and(other) {
assert.argumentIsRequired(other, "other", Specification, "Specification");
return new And(this, other);
}
/**
* Wraps the current instance and another {@link Specification} into a new
* specification which only evaluates to true when either of the wrapped
* specifications evaluate to true.
*
* @public
* @param {Specification} other
* @returns {Or}
*/
or(other) {
assert.argumentIsRequired(other, "other", Specification, "Specification");
return new Or(this, other);
}
/**
* Wraps the current instance in a new {@link Specification} which evaluates
* to the inverse result of the wrapped specification.
*
* @public
* @returns {Not}
*/
not() {
return new Not(this);
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return "[Specification]";
}
}
class And extends Specification {
#specificationOne;
#specificationTwo;
/**
* @param {Specification} specificationOne
* @param {Specification} specificationTwo
*/
constructor(specificationOne, specificationTwo) {
super();
assert.argumentIsRequired(specificationOne, "specificationOne", Specification, "Specification");
assert.argumentIsRequired(specificationTwo, "specificationTwo", Specification, "Specification");
this.#specificationOne = specificationOne;
this.#specificationTwo = specificationTwo;
}
/**
* @protected
* @override
* @param {*=} data
* @returns {boolean}
*/
_evaluate(data) {
return this.#specificationOne.evaluate(data) && this.#specificationTwo.evaluate(data);
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return "[And]";
}
}
class Or extends Specification {
#specificationOne;
#specificationTwo;
/**
* @param {Specification} specificationOne
* @param {Specification} specificationTwo
*/
constructor(specificationOne, specificationTwo) {
super();
assert.argumentIsRequired(specificationOne, "specificationOne", Specification, "Specification");
assert.argumentIsRequired(specificationTwo, "specificationTwo", Specification, "Specification");
this.#specificationOne = specificationOne;
this.#specificationTwo = specificationTwo;
}
/**
* @protected
* @override
* @param {*=} data
* @returns {boolean}
*/
_evaluate(data) {
return this.#specificationOne.evaluate(data) || this.#specificationTwo.evaluate(data);
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return "[Or]";
}
}
class Not extends Specification {
#otherSpecification;
/**
* @param {Specification} otherSpecification
*/
constructor(otherSpecification) {
super();
assert.argumentIsRequired(otherSpecification, "otherSpecification", Specification, "Specification");
this.#otherSpecification = otherSpecification;
}
/**
* @protected
* @override
* @param {*=} data
* @returns {boolean}
*/
_evaluate(data) {
return !this.#otherSpecification.evaluate(data);
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return "[Not]";
}
}
Specification.And = And;
Specification.Or = Or;
Specification.Not = Not;
{
const cjsExports = module.exports;
const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports;
if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) {
Object.keys(cjsExports).forEach((key) => {
if (key !== 'default' && key !== '__esModule') {
cjsDefaultExport[key] = cjsExports[key];
}
});
}
module.exports = cjsDefaultExport;
}