<commit_before>import { computed } from '@ember/object';
import moment from 'moment';
import { values } from 'lodash';
import { isValidUrl } from 'open-event-frontend/utils/validators';
import { FORM_DATE_FORMAT, FORM_TIME_FORMAT } from 'open-event-frontend/utils/dictionary/date-time';

/**
 * Get/set a splitted URL from/to a string URL field
 *
 * @param property The string URL field
 * @returns {*}
 */
export const computedSegmentedLink = function(property) {
  return computed(property, {
    get() {
      const splitted = this.get(property) ? this.get(property).split('://') : [];
      if (!splitted || splitted.length === 0 || (splitted.length === 1 && splitted[0].includes('http'))) {
        return {
          protocol : 'https',
          address  : ''
        };
      }
      return {
        protocol : splitted[0],
        address  : splitted[1]
      };
    },
    set(key, value) {
      const finalLink = values(value).join('://');
      if (finalLink && isValidUrl(finalLink.trim())) {
        this.set(property, finalLink.trim());
      } else {
        this.set(property, null);
      }
      return value;
    }
  });
};

/**
 * Get set splitted date time from/to a Full datetime object
 * @param property The full date time object
 * @param segmentFormat The part of the date to be returned. (time/date or a custom format)
 * @returns {*}
 */
export const computedDateTimeSplit = function(property, segmentFormat) {
  return computed(property, {
    get() {
      return moment(this.get(property)).format(getFormat(segmentFormat));
    },
    set(key, value) {
      const newDate = moment(value, getFormat(segmentFormat));
      let oldDate = moment(this.get(property));
      if (segmentFormat === 'time') {
        oldDate.hour(newDate.hour());
        oldDate.minute(newDate.minute());
      } else if (segmentFormat === 'date') {
        oldDate.date(newDate.date());
        oldDate.month(newDate.month());
        oldDate.year(newDate.year());
      } else {
        oldDate = newDate;
      }
      this.set(property, oldDate);
      return value;
    }
  });
};

function getFormat(segmentFormat) {
  return segmentFormat === 'time' ? FORM_TIME_FORMAT : (segmentFormat === 'date' ? FORM_DATE_FORMAT : segmentFormat);
}
<commit_msg>Update end date of event automatically when start date is selected. (#1696)<commit_after>import { computed } from '@ember/object';
import moment from 'moment';
import { values } from 'lodash';
import { isValidUrl } from 'open-event-frontend/utils/validators';
import { FORM_DATE_FORMAT, FORM_TIME_FORMAT } from 'open-event-frontend/utils/dictionary/date-time';

/**
 * Get/set a splitted URL from/to a string URL field
 *
 * @param property The string URL field
 * @returns {*}
 */
export const computedSegmentedLink = function(property) {
  return computed(property, {
    get() {
      const splitted = this.get(property) ? this.get(property).split('://') : [];
      if (!splitted || splitted.length === 0 || (splitted.length === 1 && splitted[0].includes('http'))) {
        return {
          protocol : 'https',
          address  : ''
        };
      }
      return {
        protocol : splitted[0],
        address  : splitted[1]
      };
    },
    set(key, value) {
      const finalLink = values(value).join('://');
      if (finalLink && isValidUrl(finalLink.trim())) {
        this.set(property, finalLink.trim());
      } else {
        this.set(property, null);
      }
      return value;
    }
  });
};

/**
 * Get set splitted date time from/to a Full datetime object
 * @param property The full date time object
 * @param segmentFormat The part of the date to be returned. (time/date or a custom format)
 * @returns {*}
 */
export const computedDateTimeSplit = function(property, segmentFormat) {
  return computed(property, {
    get() {
      if (property === 'endsAt' && segmentFormat === 'date') {
        return moment(this.get(property)).add(1, 'days').format(getFormat(segmentFormat));
      }
      return moment(this.get(property)).format(getFormat(segmentFormat));
    },
    set(key, value) {
      const newDate = moment(value, getFormat(segmentFormat));
      let oldDate = moment(this.get(property));
      if (segmentFormat === 'time') {
        oldDate.hour(newDate.hour());
        oldDate.minute(newDate.minute());
      } else if (segmentFormat === 'date') {
        oldDate.date(newDate.date());
        oldDate.month(newDate.month());
        oldDate.year(newDate.year());
      } else {
        oldDate = newDate;
      }
      this.set(property, oldDate);
      this.set('endsAt', oldDate);
      return value;
    }
  });
};

function getFormat(segmentFormat) {
  return segmentFormat === 'time' ? FORM_TIME_FORMAT : (segmentFormat === 'date' ? FORM_DATE_FORMAT : segmentFormat);
}

{'fraction_non_alphanumeric': 0.1493845190903401, 'fraction_numerical': 0.0031295639474233257, 'mean_word_length': 2.9295081967213115, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7293596', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1411, 'n_words': 341}