all files / models/measure/ measure.js

88.24% Statements 60/68
72.41% Branches 21/29
84.62% Functions 11/13
88.24% Lines 60/68
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  50×           50× 50× 50× 50× 50× 50×   50× 50× 50×   50× 50×   33×                               42× 42×                         42×         42×   42×       28×                          
"use strict";
var immutable_class_1 = require('immutable-class');
var numeral = require('numeral');
var plywood_1 = require('plywood');
var general_1 = require('../../utils/general/general');
function formatFnFactory(format) {
    return function (n) {
        if (isNaN(n) || !isFinite(n))
            return '-';
        return numeral(n).format(format);
    };
}
var check;
var Measure = (function () {
    function Measure(parameters) {
        var name = parameters.name;
        general_1.verifyUrlSafeName(name);
        this.name = name;
        this.title = parameters.title || general_1.makeTitle(name);
        var expression = parameters.expression;
        Iif (!expression)
            throw new Error('measure must have expression');
        this.expression = expression;
        var format = parameters.format || Measure.DEFAULT_FORMAT;
        Iif (format[0] === '(')
            throw new Error('can not have format that uses ( )');
        this.format = format;
        this.formatFn = formatFnFactory(format);
    }
    Measure.isMeasure = function (candidate) {
        return immutable_class_1.isInstanceOf(candidate, Measure);
    };
    Measure.measuresFromAttributeInfo = function (attribute) {
        var name = attribute.name;
        var $main = plywood_1.$('main');
        var ref = plywood_1.$(name);
        if (attribute.special) {
            if (attribute.special === 'unique') {
                return [
                    new Measure({
                        name: name,
                        expression: $main.countDistinct(ref)
                    })
                ];
            }
            else {
                return [];
            }
        }
        var expression = $main.sum(ref);
        var makerAction = attribute.makerAction;
        if (makerAction) {
            switch (makerAction.action) {
                case 'min':
                    expression = $main.min(ref);
                    break;
                case 'max':
                    expression = $main.max(ref);
                    break;
            }
        }
        return [new Measure({ name: name, expression: expression })];
    };
    Measure.fromJS = function (parameters) {
        var name = parameters.name;
        return new Measure({
            name: name,
            title: parameters.title,
            expression: parameters.expression ? plywood_1.Expression.fromJSLoose(parameters.expression) : plywood_1.$('main').sum(plywood_1.$(name)),
            format: parameters.format
        });
    };
    Measure.prototype.valueOf = function () {
        return {
            name: this.name,
            title: this.title,
            expression: this.expression,
            format: this.format
        };
    };
    Measure.prototype.toJS = function () {
        var js = {
            name: this.name,
            title: this.title,
            expression: this.expression.toJS()
        };
        Iif (this.format !== Measure.DEFAULT_FORMAT)
            js.format = this.format;
        return js;
    };
    Measure.prototype.toJSON = function () {
        return this.toJS();
    };
    Measure.prototype.toString = function () {
        return "[Measure: " + this.name + "]";
    };
    Measure.prototype.equals = function (other) {
        return Measure.isMeasure(other) &&
            this.name === other.name &&
            this.title === other.title &&
            this.expression.equals(other.expression) &&
            this.format === other.format;
    };
    Measure.prototype.toApplyAction = function () {
        var _a = this, name = _a.name, expression = _a.expression;
        return new plywood_1.ApplyAction({
            name: name,
            expression: expression
        });
    };
    Measure.DEFAULT_FORMAT = '0,0.0 a';
    Measure.INTEGER_FORMAT = '0,0 a';
    return Measure;
}());
exports.Measure = Measure;
check = Measure;