convert metricFunctions to TS

This commit is contained in:
Alexander Zobnin
2020-05-07 11:49:51 +03:00
parent 44660476ca
commit e7cd31c75c
2 changed files with 28 additions and 20 deletions

View File

@@ -1,8 +1,8 @@
import _ from 'lodash'; import _ from 'lodash';
import $ from 'jquery'; import { isNumeric } from './utils';
var index = []; const index = [];
var categories = { const categories = {
Transform: [], Transform: [],
Aggregate: [], Aggregate: [],
Filter: [], Filter: [],
@@ -298,11 +298,15 @@ addFuncDef({
defaultParams: ['avg'], defaultParams: ['avg'],
}); });
_.each(categories, function(funcList, catName) { _.each(categories, (funcList, catName) => {
categories[catName] = _.sortBy(funcList, 'name'); categories[catName] = _.sortBy(funcList, 'name');
}); });
class FuncInstance { class FuncInstance {
def: any;
params: any;
text: string;
constructor(funcDef, params) { constructor(funcDef, params) {
this.def = funcDef; this.def = funcDef;
@@ -318,13 +322,13 @@ class FuncInstance {
} }
bindFunction(metricFunctions) { bindFunction(metricFunctions) {
var func = metricFunctions[this.def.name]; const func = metricFunctions[this.def.name];
if (func) { if (func) {
// Bind function arguments // Bind function arguments
var bindedFunc = func; let bindedFunc = func;
var param; let param;
for (var i = 0; i < this.params.length; i++) { for (let i = 0; i < this.params.length; i++) {
param = this.params[i]; param = this.params[i];
// Convert numeric params // Convert numeric params
@@ -341,23 +345,21 @@ class FuncInstance {
} }
render(metricExp) { render(metricExp) {
var str = this.def.name + '('; const str = this.def.name + '(';
var parameters = _.map(this.params, function(value, index) { const parameters = _.map(this.params, (value, index) => {
const paramType = this.def.params[index].type;
var paramType = this.def.params[index].type;
if (paramType === 'int' || if (paramType === 'int' ||
paramType === 'float' || paramType === 'float' ||
paramType === 'value_or_series' || paramType === 'value_or_series' ||
paramType === 'boolean') { paramType === 'boolean') {
return value; return value;
} } else if (paramType === 'int_or_interval' && isNumeric(value)) {
else if (paramType === 'int_or_interval' && $.isNumeric(value)) {
return value; return value;
} }
return "'" + value + "'"; return "'" + value + "'";
}, this); });
if (metricExp) { if (metricExp) {
parameters.unshift(metricExp); parameters.unshift(metricExp);
@@ -378,16 +380,15 @@ class FuncInstance {
// handle optional parameters // handle optional parameters
// if string contains ',' and next param is optional, split and update both // if string contains ',' and next param is optional, split and update both
if (this._hasMultipleParamsInString(strValue, index)) { if (this._hasMultipleParamsInString(strValue, index)) {
_.each(strValue.split(','), function(partVal, idx) { _.each(strValue.split(','), (partVal, idx) => {
this.updateParam(partVal.trim(), idx); this.updateParam(partVal.trim(), idx);
}, this); });
return; return;
} }
if (strValue === '' && this.def.params[index].optional) { if (strValue === '' && this.def.params[index].optional) {
this.params.splice(index, 1); this.params.splice(index, 1);
} }else {
else {
this.params[index] = strValue; this.params[index] = strValue;
} }
@@ -400,7 +401,7 @@ class FuncInstance {
return; return;
} }
var text = this.def.name + '('; let text = this.def.name + '(';
text += this.params.join(', '); text += this.params.join(', ');
text += ')'; text += ')';
this.text = text; this.text = text;

View File

@@ -345,6 +345,13 @@ export function getArrayDepth(a, level = 0) {
return level + 1; return level + 1;
} }
/**
* Checks whether its argument represents a numeric value.
*/
export function isNumeric(n: any): boolean {
return !isNaN(parseFloat(n)) && isFinite(n);
}
// Fix for backward compatibility with lodash 2.4 // Fix for backward compatibility with lodash 2.4
if (!_.includes) { if (!_.includes) {
_.includes = (_ as any).contains; _.includes = (_ as any).contains;