{"size": 531, "ext": "js", "max_stars_repo_path": "forms-flow-forms/src/actions/fields/datetime.js", "max_stars_repo_name": "yichun-aot/forms-flow-ai", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "forms-flow-forms/src/actions/fields/datetime.js", "max_issues_repo_name": "yichun-aot/forms-flow-ai", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "forms-flow-forms/src/actions/fields/datetime.js", "max_forks_repo_name": "yichun-aot/forms-flow-ai", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.2352941176, "max_line_length": 94, "alphanum_fraction": 0.5894538606}
'use strict';
const _ = require('lodash');

module.exports = formio => async (component, data, handler, action, {validation}) => {
  // Only perform before validation has occurred.
  if (validation && ['put', 'post', 'patch'].includes(action)) {
    let value = _.get(data, component.key);
    if (value) {
      value = _.isArray(value) ? value.map(val => val ? new Date(val) : '') : new Date(value);
      _.set(data, component.key, value);
    }
    else if (value === '') {
      _.set(data, component.key, null);
    }
  }
};
