all files / models/filter-clause/ filter-clause.js

85.87% Statements 79/92
69.57% Branches 32/46
94.44% Functions 17/18
85.87% Lines 79/92
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138  82× 82×     115× 33×   33× 33× 33×     82×   115× 115× 115× 33×   82× 82×         115× 115×   90×   18× 15×   60× 60×       60× 60× 60× 60×               42×         42×             38×       38× 12× 38×     12×   81×         61× 61× 61×   61×                          
"use strict";
var immutable_class_1 = require('immutable-class');
var chronoshift_1 = require('chronoshift');
var plywood_1 = require('plywood');
function isLiteral(ex) {
    Eif (ex instanceof plywood_1.LiteralExpression)
        return plywood_1.TimeRange.isTimeRange(ex.value) || plywood_1.Set.isSet(ex.value);
    return false;
}
function isRelative(ex) {
    if (ex instanceof plywood_1.ChainExpression) {
        Iif (ex.type !== 'TIME_RANGE')
            return false;
        var expression = ex.expression;
        Eif (expression instanceof plywood_1.RefExpression) {
            return expression.name === FilterClause.NOW_REF_NAME || expression.name === FilterClause.MAX_TIME_REF_NAME;
        }
    }
    return false;
}
var check;
var FilterClause = (function () {
    function FilterClause(parameters) {
        this.expression = parameters.expression;
        var selection = parameters.selection;
        if (isRelative(selection)) {
            this.relative = true;
        }
        else Eif (isLiteral(selection)) {
            this.relative = false;
        }
        else {
            throw new Error("invalid expression " + selection.toString());
        }
        this.selection = selection;
        this.exclude = parameters.exclude || false;
    }
    FilterClause.isFilterClause = function (candidate) {
        return immutable_class_1.isInstanceOf(candidate, FilterClause);
    };
    FilterClause.evaluate = function (selection, now, maxTime, timezone) {
        if (!selection)
            return null;
        var maxTimeMinuteTop = chronoshift_1.minute.move(chronoshift_1.minute.floor(maxTime, timezone), timezone, 1);
        var datum = {};
        datum[FilterClause.NOW_REF_NAME] = now;
        datum[FilterClause.MAX_TIME_REF_NAME] = maxTimeMinuteTop;
        return selection.getFn()(datum, { timezone: timezone.toString() });
    };
    FilterClause.fromExpression = function (ex) {
        var exclude = false;
        Iif (ex.lastAction() instanceof plywood_1.NotAction) {
            ex = ex.popAction();
            exclude = true;
        }
        var lastAction = ex.lastAction();
        var dimExpression = ex.popAction();
        Eif (lastAction instanceof plywood_1.InAction || lastAction instanceof plywood_1.OverlapAction) {
            return new FilterClause({
                expression: dimExpression,
                selection: lastAction.expression,
                exclude: exclude
            });
        }
        throw new Error("invalid expression " + ex.toString());
    };
    FilterClause.fromJS = function (parameters) {
        var value = {
            expression: plywood_1.Expression.fromJS(parameters.expression),
            selection: plywood_1.Expression.fromJS(parameters.selection),
            exclude: Boolean(parameters.exclude)
        };
        return new FilterClause(value);
    };
    FilterClause.prototype.valueOf = function () {
        return {
            expression: this.expression,
            selection: this.selection,
            exclude: this.exclude
        };
    };
    FilterClause.prototype.toJS = function () {
        var js = {
            expression: this.expression.toJS(),
            selection: this.selection.toJS()
        };
        if (this.exclude)
            js.exclude = true;
        return js;
    };
    FilterClause.prototype.toJSON = function () {
        return this.toJS();
    };
    FilterClause.prototype.toString = function () {
        return "[FilterClause: " + this.expression.toString() + "]";
    };
    FilterClause.prototype.equals = function (other) {
        return FilterClause.isFilterClause(other) &&
            this.expression.equals(other.expression) &&
            this.selection.equals(other.selection) &&
            this.exclude === other.exclude;
    };
    FilterClause.prototype.toExpression = function () {
        var _a = this, expression = _a.expression, selection = _a.selection;
        var ex = (selection.type === 'TIME_RANGE') ? expression.in(selection) : expression.overlap(selection);
        Iif (this.exclude)
            ex = ex.not();
        return ex;
    };
    FilterClause.prototype.getTimeRange = function () {
        if (this.relative)
            return null;
        var v = this.selection.getLiteralValue();
        return plywood_1.TimeRange.isTimeRange(v) ? v : null;
    };
    FilterClause.prototype.getLiteralSet = function () {
        Iif (this.relative)
            return null;
        var v = this.selection.getLiteralValue();
        return plywood_1.TimeRange.isTimeRange(v) ? plywood_1.Set.fromJS([v]) : v;
    };
    FilterClause.prototype.changeSelection = function (selection) {
        var value = this.valueOf();
        value.selection = selection;
        return new FilterClause(value);
    };
    FilterClause.prototype.evaluate = function (now, maxTime, timezone) {
        Iif (!this.relative)
            return this;
        return this.changeSelection(plywood_1.r(FilterClause.evaluate(this.selection, now, maxTime, timezone)));
    };
    FilterClause.NOW_REF_NAME = 'n';
    FilterClause.MAX_TIME_REF_NAME = 'm';
    return FilterClause;
}());
exports.FilterClause = FilterClause;
check = FilterClause;