1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _PROPERTY_FORMATTER, _OPERATORS$IN, _FORMATTER_FROM_DECIS;
|
8 |
|
9 | exports.formatProperty = formatProperty;
|
10 | exports.formatDecisionRules = formatDecisionRules;
|
11 |
|
12 | var _lodash = require('lodash');
|
13 |
|
14 | var _lodash2 = _interopRequireDefault(_lodash);
|
15 |
|
16 | var _time2 = require('./time');
|
17 |
|
18 | var _time3 = _interopRequireDefault(_time2);
|
19 |
|
20 | var _constants = require('./constants');
|
21 |
|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23 |
|
24 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
25 |
|
26 | var DAYS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
27 |
|
28 | var MONTH = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
29 |
|
30 | var PROPERTY_FORMATTER = (_PROPERTY_FORMATTER = {}, _defineProperty(_PROPERTY_FORMATTER, _constants.TYPE_ANY, function (value) {
|
31 | return value;
|
32 | }), _defineProperty(_PROPERTY_FORMATTER, _constants.TYPES.continuous, function (number) {
|
33 | return '' + Math.round(number * 100) / 100;
|
34 | }), _defineProperty(_PROPERTY_FORMATTER, _constants.TYPES.time_of_day, function (time) {
|
35 | var _time = time instanceof _time3.default ? time.time_of_day : time;
|
36 | var hours = Math.floor(_time);
|
37 | var hoursStr = _lodash2.default.padStart(hours, 2, '0');
|
38 | var decMinutes = Math.round((_time - hours) * 60 * 100) / 100;
|
39 | var minutes = Math.floor(decMinutes);
|
40 | var minutesStr = _lodash2.default.padStart(minutes, 2, '0');
|
41 | var seconds = Math.round((decMinutes - minutes) * 60);
|
42 | var secondsStr = _lodash2.default.padStart(seconds, 2, '0');
|
43 |
|
44 | if (seconds > 0) {
|
45 | return hoursStr + ':' + minutesStr + ':' + secondsStr;
|
46 | } else {
|
47 | return hoursStr + ':' + minutesStr;
|
48 | }
|
49 | }), _defineProperty(_PROPERTY_FORMATTER, _constants.TYPES.day_of_week, function (day) {
|
50 | var _day = day instanceof _time3.default ? day.day_of_week : day;
|
51 | return DAYS[_day];
|
52 | }), _defineProperty(_PROPERTY_FORMATTER, _constants.TYPES.day_of_month, function (day) {
|
53 | var _day = day instanceof _time3.default ? day.day_of_month : day;
|
54 | return _lodash2.default.padStart(_day, 2, '0');
|
55 | }), _defineProperty(_PROPERTY_FORMATTER, _constants.TYPES.month_of_year, function (month) {
|
56 | var _month = month instanceof _time3.default ? month.month_of_year : month;
|
57 | return MONTH[_month - 1];
|
58 | }), _PROPERTY_FORMATTER);
|
59 |
|
60 | function formatProperty(type) {
|
61 | var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
|
62 |
|
63 | var formatter = PROPERTY_FORMATTER[type] || PROPERTY_FORMATTER[_constants.TYPE_ANY];
|
64 | var extendedFormatter = function extendedFormatter(value) {
|
65 |
|
66 | if (_lodash2.default.isNull(value)) {
|
67 | return 'null';
|
68 | }
|
69 |
|
70 | else if (_lodash2.default.isPlainObject(value) && _lodash2.default.isEmpty(value)) {
|
71 | return 'N/A';
|
72 | }
|
73 | return formatter(value);
|
74 | };
|
75 | if (!_lodash2.default.isUndefined(value)) {
|
76 | return extendedFormatter(value);
|
77 | }
|
78 | return extendedFormatter;
|
79 | }
|
80 |
|
81 | var FORMATTER_FROM_DECISION_RULE = (_FORMATTER_FROM_DECIS = {}, _defineProperty(_FORMATTER_FROM_DECIS, _constants.OPERATORS.IS, _defineProperty({}, _constants.TYPE_ANY, function (_ref) {
|
82 | var property = _ref.property,
|
83 | operand = _ref.operand,
|
84 | operandFormatter = _ref.operandFormatter;
|
85 |
|
86 | if (property) {
|
87 | return '\'' + property + '\' is ' + operandFormatter(operand);
|
88 | }
|
89 | return 'is ' + operandFormatter(operand);
|
90 | })), _defineProperty(_FORMATTER_FROM_DECIS, _constants.OPERATORS.IN, (_OPERATORS$IN = {}, _defineProperty(_OPERATORS$IN, _constants.TYPE_ANY, function (_ref2) {
|
91 | var property = _ref2.property,
|
92 | operand = _ref2.operand,
|
93 | operandFormatter = _ref2.operandFormatter;
|
94 |
|
95 | if (property) {
|
96 | return '\'' + property + '\' in [' + operandFormatter(operand[0]) + ', ' + operandFormatter(operand[1]) + '[';
|
97 | }
|
98 | return '[' + operandFormatter(operand[0]) + ', ' + operandFormatter(operand[1]) + '[';
|
99 | }), _defineProperty(_OPERATORS$IN, _constants.TYPES.day_of_week, function (_ref3) {
|
100 | var property = _ref3.property,
|
101 | operand = _ref3.operand,
|
102 | operandFormatter = _ref3.operandFormatter;
|
103 |
|
104 | var day_from = Math.floor(operand[0]);
|
105 | var day_to = Math.floor(operand[1]);
|
106 |
|
107 | if (day_to - day_from == 1 || day_from == 6 && day_to == 0) {
|
108 | if (property) {
|
109 | return '\'' + property + '\' is ' + operandFormatter(day_from);
|
110 | }
|
111 | return operandFormatter(day_from);
|
112 | } else {
|
113 | if (property) {
|
114 | return '\'' + property + '\' from ' + operandFormatter(day_from) + ' to ' + operandFormatter((7 + day_to - 1) % 7);
|
115 | }
|
116 | return operandFormatter(day_from) + ' to ' + operandFormatter((7 + day_to - 1) % 7);
|
117 | }
|
118 | }), _defineProperty(_OPERATORS$IN, _constants.TYPES.month_of_year, function (_ref4) {
|
119 | var property = _ref4.property,
|
120 | operand = _ref4.operand,
|
121 | operandFormatter = _ref4.operandFormatter;
|
122 |
|
123 | var month_from = Math.floor(operand[0]);
|
124 | var month_to = Math.floor(operand[1]);
|
125 | if (month_to - month_from == 1 || month_from == 12 && month_to == 1) {
|
126 |
|
127 | if (property) {
|
128 | return '\'' + property + '\' is ' + operandFormatter(month_from);
|
129 | }
|
130 | return operandFormatter(month_from);
|
131 | } else if (month_to == 1) {
|
132 |
|
133 | if (property) {
|
134 | return '\'' + property + '\' from ' + operandFormatter(month_from) + ' to ' + operandFormatter(12);
|
135 | }
|
136 | return operandFormatter(month_from) + ' to ' + operandFormatter(12);
|
137 | } else {
|
138 | if (property) {
|
139 | return '\'' + property + '\' from ' + operandFormatter(month_from) + ' to ' + operandFormatter(month_to - 1);
|
140 | }
|
141 | return operandFormatter(month_from) + ' to ' + operandFormatter(month_to - 1);
|
142 | }
|
143 | }), _OPERATORS$IN)), _defineProperty(_FORMATTER_FROM_DECIS, _constants.OPERATORS.GTE, _defineProperty({}, _constants.TYPE_ANY, function (_ref5) {
|
144 | var property = _ref5.property,
|
145 | operand = _ref5.operand,
|
146 | operandFormatter = _ref5.operandFormatter;
|
147 |
|
148 | if (property) {
|
149 | return '\'' + property + '\' >= ' + operandFormatter(operand);
|
150 | }
|
151 | return '>= ' + operandFormatter(operand);
|
152 | })), _defineProperty(_FORMATTER_FROM_DECIS, _constants.OPERATORS.LT, _defineProperty({}, _constants.TYPE_ANY, function (_ref6) {
|
153 | var property = _ref6.property,
|
154 | operand = _ref6.operand,
|
155 | operandFormatter = _ref6.operandFormatter;
|
156 |
|
157 | if (property) {
|
158 | return '\'' + property + '\' < ' + operandFormatter(operand);
|
159 | }
|
160 | return '< ' + operandFormatter(operand);
|
161 | })), _FORMATTER_FROM_DECIS);
|
162 |
|
163 | function formatDecisionRules(decisionRules) {
|
164 | return decisionRules.map(function (_ref7) {
|
165 | var property = _ref7.property,
|
166 | type = _ref7.type,
|
167 | operand = _ref7.operand,
|
168 | operator = _ref7.operator;
|
169 |
|
170 | var operatorFormatters = FORMATTER_FROM_DECISION_RULE[operator];
|
171 | if (!operatorFormatters) {
|
172 | throw new Error('Unable to format the given decision rule: unknown operator \'' + operator + '\'.');
|
173 | }
|
174 | var formatter = operatorFormatters[type] || operatorFormatters[_constants.TYPE_ANY];
|
175 | var operandFormatter = formatProperty(type || _constants.TYPE_ANY);
|
176 | return formatter({ property: property, type: type, operator: operator, operandFormatter: operandFormatter, operand: operand });
|
177 | }).join(' and ');
|
178 | } |
\ | No newline at end of file |