dmn-eval-js-es5
Version:
Evaluation of DMN 1.1 decision tables, limited to S-FEEL (Simple Friendly Enough Expression Language), es5 browser compatible
140 lines (119 loc) • 4.81 kB
JavaScript
;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
/*
*
* ©2016-2017 EdgeVerve Systems Limited (a fully owned Infosys subsidiary),
* Bangalore, India. All Rights Reserved.
*
*/
/*
Decision Model and Notation, v1.1
Page : 131
*/
/*
Format : duration(from) // from - duration string
Description :convert "from" to a days and time or years and months duration
e.g. : date_and_time("2012-12-24T23:59:00") - date_and_time("2012-12-22T03:45:00") = duration("P2DT20H14M")
duration("P2Y2M") = duration("P26M")
*/
/*
Format : years_and_months_duration(from, to) // both are date_and_time
Description : return years and months duration between "from" and "to"
e.g. : years and months duration(date("2011-12-22"), date("2013-08-24")) = duration("P1Y8M")
*/
var moment = require('moment');
var addProperties = require('./add-properties');
var _require = require('../../helper/meta'),
ymd_ISO_8601 = _require.ymd_ISO_8601,
dtd_ISO_8601 = _require.dtd_ISO_8601,
types = _require.types,
properties = _require.properties;
var years = properties.years,
months = properties.months,
days = properties.days,
hours = properties.hours,
minutes = properties.minutes,
seconds = properties.seconds;
var dtdProps = Object.assign({}, { days: days, hours: hours, minutes: minutes, seconds: seconds, type: types.dtd, isDtd: true, isDuration: true });
var ymdProps = Object.assign({}, { years: years, months: months, type: types.ymd, isYmd: true, isDuration: true });
var isDateTime = function isDateTime(args) {
return args.reduce(function (recur, next) {
return recur && (next.isDateTime || next.isDate);
}, true);
};
var daysAndTimeDuration = function daysAndTimeDuration() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var dtd = void 0;
if (args.length === 1) {
dtd = moment.duration(args[0]);
dtd = dtd.isValid() ? dtd : new Error('Invalid Duration : "days_and_time_duration" in-built function');
} else if (args.length === 2 && isDateTime(args)) {
var start = args[0],
end = args[1];
dtd = moment.duration(Math.floor(end.diff(start)));
} else {
throw new Error('Invalid number of arguments specified with "days_and_time_duration" in-built function');
}
if (dtd instanceof Error) {
throw dtd;
} else {
return addProperties(dtd, dtdProps);
}
};
var yearsAndMonthsDuration = function yearsAndMonthsDuration() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
var ymd = void 0;
if (args.length === 1) {
ymd = moment.duration(args[0]);
ymd = ymd.isValid() ? ymd : new Error('Invalid Duration : "years_and_months_duration" in-built function');
} else if (args.length === 2 && isDateTime(args)) {
var start = args[0],
end = args[1];
var _months = Math.floor(moment.duration(end.diff(start)).asMonths());
ymd = moment.duration(_months, 'months');
} else {
throw new Error('Invalid number of arguments specified with "years_and_months_duration" in-built function');
}
if (ymd instanceof Error) {
throw ymd;
} else {
return addProperties(ymd, ymdProps);
}
};
// slice(1) is necessary as "P" will be available in both the patterns and we need to check if some optional part is not undefined to determine a type
var patternMatch = function patternMatch(arg, pattern) {
return arg.match(pattern).slice(1).reduce(function (recur, next) {
return recur || Boolean(next);
}, false);
};
var duration = function duration(arg) {
var result = void 0;
if (arg !== null && arg !== undefined) {
if (typeof arg === 'string') {
if (patternMatch(arg, ymd_ISO_8601)) {
try {
result = yearsAndMonthsDuration(arg);
} catch (err) {
throw err;
}
} else if (patternMatch(arg, dtd_ISO_8601)) {
try {
result = daysAndTimeDuration(arg);
} catch (err) {
throw err;
}
}
if (result === undefined) {
throw new Error('Invalid Format : "duration" built-in function. Please check the input format');
}
} else {
throw new Error('Type Error : "duration" built-in function expects a string but "' + (typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) + '" encountered');
}
}
return result;
};
module.exports = { duration: duration, 'years and months duration': yearsAndMonthsDuration, 'days and time duration': daysAndTimeDuration };