"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;
|