dmn-eval-js-es5
Version:
Evaluation of DMN 1.1 decision tables, limited to S-FEEL (Simple Friendly Enough Expression Language), es5 browser compatible
179 lines (156 loc) • 4.22 kB
JavaScript
;
/*
*
* ©2016-2017 EdgeVerve Systems Limited (a fully owned Infosys subsidiary),
* Bangalore, India. All Rights Reserved.
*
*/
var _ = require('lodash');
var listContains = function listContains(list, element) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else {
if (list.indexOf(element) > -1) {
return true;
}
return false;
}
};
var count = function count(list) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else {
return list.length;
}
};
var min = function min(list) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else {
return _.min(list);
}
};
var max = function max(list) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else {
return _.max(list);
}
};
var sum = function sum(list) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else {
return _.sum(list);
}
};
var mean = function mean(list) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else {
return _.sum(list) / list.length;
}
};
var and = function and(list) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else {
return list.reduce(function (recur, next) {
return recur && next;
}, true);
}
};
var or = function or(list) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else {
return list.reduce(function (recur, next) {
return recur || next;
}, false);
}
};
var append = function append(element, list) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else {
return list.push(element);
}
};
var concatenate = function concatenate() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return args.reduce(function (result, next) {
return Array.prototype.concat(result, next);
}, []);
};
var insertBefore = function insertBefore(list, position, newItem) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else if (position > list.length || position < 0) {
throw new Error('invalid position');
} else {
return list.splice(position - 1, 0, newItem);
}
};
var remove = function remove(list, position) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else if (position > list.length - 1) {
throw new Error('invalid position');
} else {
return list.splice(position, 1);
}
};
var reverse = function reverse(list) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else {
return _.reverse(list);
}
};
var indexOf = function indexOf(list, match) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else {
return _.indexOf(list, match);
}
};
var union = function union() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return _.union(args);
};
var distinctValues = function distinctValues(list) {
if (!Array.isArray(list)) {
throw new Error('operation unsupported on element of this type');
} else {
return _.uniq(list);
}
};
var flatten = function flatten() {
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
return _.flatten(args);
};
module.exports = {
listContains: listContains,
count: count,
min: min,
max: max,
sum: sum,
mean: mean,
and: and,
or: or,
append: append,
concatenate: concatenate,
insertBefore: insertBefore,
remove: remove,
reverse: reverse,
indexOf: indexOf,
union: union,
distinctValues: distinctValues,
flatten: flatten
};