UNPKG

@overture-stack/sqon-builder

Version:
95 lines 4.41 kB
"use strict"; 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)); }; Object.defineProperty(exports, "__esModule", { value: true }); var sqon_1 = require("../types/sqon"); var asArray_1 = require("./asArray"); var createFilter_1 = require("./createFilter"); var filterDuplicates_1 = require("./filterDuplicates"); var deduplicateValues = function (filter) { if ((0, sqon_1.isArrayFilter)(filter)) { var value = (0, asArray_1.default)(filter.content.value).filter(filterDuplicates_1.default); return (0, createFilter_1.createFilter)(filter.content.fieldName, filter.op, value); } return filter; }; var reduceSQON = function (sqon) { if ((0, sqon_1.isFilter)(sqon)) { return deduplicateValues(sqon); } else { var output = { op: sqon.op, content: [], }; if (sqon.pivot !== undefined) { output.pivot = sqon.pivot; } var _loop_1 = function (innerSqon) { var _b, _c; if ((0, sqon_1.isFilter)(innerSqon)) { var match = output.content.find(function (content) { return content.op === innerSqon.op && content.content.fieldName === innerSqon.content.fieldName; }); if (match !== undefined) { if (match.op === sqon_1.FilterKeys.GreaterThan && innerSqon.op === sqon_1.FilterKeys.GreaterThan) { if (output.op === sqon_1.CombinationKeys.And || output.op === sqon_1.CombinationKeys.Not) { match.content.value = Math.max(match.content.value, innerSqon.content.value); } else { match.content.value = Math.min(match.content.value, innerSqon.content.value); } } if (match.op === sqon_1.FilterKeys.LesserThan && innerSqon.op === sqon_1.FilterKeys.LesserThan) { if (output.op === sqon_1.CombinationKeys.And || output.op === sqon_1.CombinationKeys.Not) { match.content.value = Math.min(match.content.value, innerSqon.content.value); } else { match.content.value = Math.max(match.content.value, innerSqon.content.value); } } if (match.op === sqon_1.FilterKeys.In && innerSqon.op === sqon_1.FilterKeys.In) { match.content.value = __spreadArray(__spreadArray([], (0, asArray_1.default)(match.content.value), true), (0, asArray_1.default)(innerSqon.content.value), true); } } else { output.content.push(innerSqon); } } else { if (innerSqon.content.length === 0) { return "continue"; } if (innerSqon.content.length === 1 && innerSqon.op !== sqon_1.CombinationKeys.Not) { (_b = output.content).push.apply(_b, innerSqon.content); return "continue"; } if (innerSqon.op === sqon_1.CombinationKeys.Not) { output.content.push(innerSqon); return "continue"; } if (innerSqon.op === output.op && innerSqon.pivot === output.pivot) { (_c = output.content).push.apply(_c, innerSqon.content); return "continue"; } output.content.push(innerSqon); } }; for (var _i = 0, _a = sqon.content; _i < _a.length; _i++) { var innerSqon = _a[_i]; _loop_1(innerSqon); } if (output.content.length === 1 && sqon.op !== sqon_1.CombinationKeys.Not) { return reduceSQON(output.content[0]); } output.content = output.content.map(reduceSQON); return output; } }; exports.default = reduceSQON; //# sourceMappingURL=reduceSQON.js.map