1 | import _ from 'lodash';
|
2 | import Time from './time';
|
3 |
|
4 | export default function createContext(configuration, ...args) {
|
5 | if (_.isUndefined(configuration) || _.isUndefined(configuration.context)) {
|
6 | throw new Error('Unable to create context, the given configuration is not valid');
|
7 | }
|
8 |
|
9 | const inputContext = _.omit(configuration.context, configuration.output);
|
10 |
|
11 | return _.reduce(args, (context, arg) => {
|
12 | if (arg instanceof Time) {
|
13 | const { day_of_month, day_of_week, month_of_year, time_of_day, timezone } = arg;
|
14 |
|
15 | return _.mapValues(inputContext, (v, k) => {
|
16 | if (v.type === 'day_of_week' && (_.isUndefined(v.is_generated) || v.is_generated)) {
|
17 | return day_of_week;
|
18 | }
|
19 | else if (v.type === 'time_of_day' && (_.isUndefined(v.is_generated) || v.is_generated)) {
|
20 | return time_of_day;
|
21 | }
|
22 | else if (v.type === 'day_of_month' && (_.isUndefined(v.is_generated) || v.is_generated)) {
|
23 | return day_of_month;
|
24 | }
|
25 | else if (v.type === 'month_of_year' && (_.isUndefined(v.is_generated) || v.is_generated)) {
|
26 | return month_of_year;
|
27 | }
|
28 | else if (v.type === 'timezone') {
|
29 | return timezone;
|
30 | }
|
31 | else {
|
32 | return context[k];
|
33 | }
|
34 | });
|
35 | }
|
36 | else {
|
37 | return _.mapValues(inputContext, (v, k) => {
|
38 | return _.isUndefined(arg[k]) ? context[k] : arg[k];
|
39 | });
|
40 | }
|
41 | }, _.mapValues(inputContext, () => {
|
42 | return undefined;
|
43 | }));
|
44 | }
|