UNPKG

dmn-eval-js-es5

Version:

Evaluation of DMN 1.1 decision tables, limited to S-FEEL (Simple Friendly Enough Expression Language), es5 browser compatible

123 lines (106 loc) 3.99 kB
'use strict'; 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 : date(from) // from - date string Description : convert "from" to a date e.g. : date("2012-12-25") – date("2012-12-24") = duration("P1D") */ /* Format : date and time(from) // from - date_and_time Description : convert "from" to a date (set time components to null) e.g. : date(date and time("2012-12-25T11:00:00Z")) = date("2012-12-25") */ /* Format : date(year, month, day) // year, month, day are numbers Description : creates a date from year, month, day component values e.g. : date(2012, 12, 25) = date("2012-12-25") */ var moment = require('moment-timezone'); var addProperties = require('./add-properties'); var _require = require('../../helper/meta'), types = _require.types, properties = _require.properties, UTC = _require.UTC, UTCTimePart = _require.UTCTimePart, time_ISO_8601 = _require.time_ISO_8601, date_ISO_8601 = _require.date_ISO_8601; var year = properties.year, month = properties.month, day = properties.day; var props = Object.assign({}, { year: year, month: month, day: day, type: types.date, isDate: true }); var isNumber = function isNumber(args) { return args.reduce(function (prev, next) { return prev && typeof next === 'number'; }, true); }; var parseDate = function parseDate(str) { try { var d = moment.parseZone('' + str + UTCTimePart); if (d.isValid()) { return d; } throw new Error('Invalid date. This is usually caused by an inappropriate format. Please check the input format.'); } catch (err) { return err; } }; var date = function date() { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var d = void 0; var result = void 0; if (args.length === 1) { var arg = args[0]; if (arg !== null && arg !== undefined) { if (typeof arg === 'string') { try { d = arg === '' ? moment.parseZone(UTCTimePart, time_ISO_8601) : parseDate(arg); } catch (err) { throw err; } } else if ((typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) === 'object') { if (arg instanceof Date) { var ISO = arg.toISOString(); var dateTime = moment.parseZone(ISO); var datePart = dateTime.format(date_ISO_8601); d = moment.parseZone('' + datePart + UTCTimePart); } else if (arg.isDateTime || moment.isMoment(arg)) { var _dateTime = moment.tz(arg.format(), UTC); var _datePart = _dateTime.format(date_ISO_8601); d = moment.parseZone('' + _datePart + UTCTimePart); } if (!d || !d.isValid()) { throw new Error('Invalid date. Parsing error while attempting to create date from ' + arg); } } else { throw new Error('Invalid format encountered. Please specify date in one of these formats :\n- "date("2012-12-25")"\n- date_and_time object'); } } } else if (args.length === 3 && isNumber(args)) { var _year = args[0], _month = args[1], _day = args[2]; d = moment.tz({ year: _year, month: _month, day: _day, hour: 0, minute: 0, second: 0 }, UTC); if (!d.isValid()) { throw new Error('Invalid date. Parsing error while attempting to create date from parts'); } } else { throw new Error('Invalid number of arguments specified with "date" in-built function'); } if (d !== undefined) { result = addProperties(d, props); } return result; }; module.exports = { date: date };