sqon-builder
Version:
SQON creation and manipulation library.
177 lines • 5.78 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
exports.__esModule = true;
exports.filter = exports.combine = void 0;
var ArrayFilterKey;
(function (ArrayFilterKey) {
ArrayFilterKey["In"] = "in";
})(ArrayFilterKey || (ArrayFilterKey = {}));
var ScalarFilterKey;
(function (ScalarFilterKey) {
ScalarFilterKey["GreaterThan"] = "gt";
ScalarFilterKey["LesserThan"] = "lt";
})(ScalarFilterKey || (ScalarFilterKey = {}));
var CombinationKey;
(function (CombinationKey) {
CombinationKey["And"] = "and";
CombinationKey["Or"] = "or";
CombinationKey["Not"] = "not";
})(CombinationKey || (CombinationKey = {}));
function combine(op, content) {
return {
op: op,
content: content
};
}
exports.combine = combine;
function filter(op, field, value) {
return {
op: op,
content: {
field: field,
value: value
}
};
}
exports.filter = filter;
var combinationKeyValues = Object.values(CombinationKey);
function isCombination(operator) {
return combinationKeyValues.includes(operator.op);
}
function parseSQON(sqon) {
var parsed = JSON.parse(sqon);
if (isCombination(parsed)) {
return parsed;
}
else {
return { op: CombinationKey.And, content: parsed };
}
}
var SQON = (function () {
function SQON(sqon) {
if (typeof sqon === 'string') {
var parsed = parseSQON(sqon);
this.op = parsed.op;
this.content = parsed.content;
}
else if (sqon instanceof SQON) {
this.op = sqon.op;
this.content = __spreadArray([], sqon.content, true);
}
else {
if (isCombination(sqon)) {
this.op = sqon.op;
this.content = sqon.content;
}
else {
this.op = CombinationKey.And;
this.content = [sqon];
}
}
}
SQON.prototype.and = function (content) {
var output = new SQON(this);
if (output.op !== CombinationKey.And) {
output.content = [output.toOperator()];
output.op = CombinationKey.And;
}
var contentAsArray = [].concat(content);
contentAsArray.forEach(function (sqonToAdd) {
if (sqonToAdd.op === CombinationKey.And) {
output.content = output.content.concat(sqonToAdd.content);
}
else {
output.content.push(sqonToAdd.toOperator());
}
});
return output;
};
SQON.prototype.or = function (content) {
var output = new SQON(this);
if (output.op !== CombinationKey.Or) {
output.content = [output.toOperator()];
output.op = CombinationKey.Or;
}
var contentAsArray = [].concat(content);
contentAsArray.forEach(function (sqonToAdd) {
if (sqonToAdd.op === CombinationKey.Or) {
output.content = output.content.concat(sqonToAdd.content);
}
else {
output.content.push(sqonToAdd.toOperator());
}
});
return output;
};
SQON.prototype.not = function (content) {
var output = new SQON(this);
var operation = combine(CombinationKey.Not, [].concat(content));
return output.and(new SQON(operation));
};
SQON.prototype["in"] = function (field, value) {
var output = new SQON(this);
output.addFilter(filter(ArrayFilterKey.In, field, value));
return output;
};
SQON.prototype.gt = function (field, value) {
var output = new SQON(this);
output.addFilter(filter(ScalarFilterKey.GreaterThan, field, value));
return output;
};
SQON.prototype.lt = function (field, value) {
var output = new SQON(this);
output.addFilter(filter(ScalarFilterKey.LesserThan, field, value));
return output;
};
SQON.from = function (sqon) {
var content = typeof sqon === 'string' ? JSON.parse(sqon) : sqon;
return new SQON(content);
};
SQON.and = function (content) {
return new SQON(combine(CombinationKey.And, [].concat(content)));
};
SQON.or = function (content) {
return new SQON(combine(CombinationKey.Or, [].concat(content)));
};
SQON.not = function (content) {
return new SQON(combine(CombinationKey.Not, [].concat(content)));
};
SQON["in"] = function (field, value) {
return new SQON(filter(ArrayFilterKey.In, field, value));
};
SQON.gt = function (field, value) {
return new SQON(filter(ScalarFilterKey.GreaterThan, field, value));
};
SQON.lt = function (field, value) {
return new SQON(filter(ScalarFilterKey.LesserThan, field, value));
};
SQON.prototype.toString = function () {
return JSON.stringify(this);
};
SQON.prototype.toOperator = function () {
return {
op: this.op,
content: __spreadArray([], this.content, true)
};
};
SQON.prototype.addFilter = function (filter) {
if (this.op === CombinationKey.And) {
this.content = this.content.concat(filter);
}
else {
this.content = [this.toOperator(), filter];
this.op = CombinationKey.And;
}
};
return SQON;
}());
exports["default"] = SQON;
//# sourceMappingURL=SQON.js.map