<commit_before>import $ from 'jquery';
import _ from 'underscore';
import timeago from 'timeago.js';
import dateFormat from 'dateformat';
import { pluralize } from './text_utility';
import { languageCode, s__ } from '../../locale';

window.timeago = timeago;

/**
 * This method allows you to create new Date instance from existing
 * date instance without keeping the reference.
 *
 * @param {Date} date
 */
export const newDate = date => (date instanceof Date ? new Date(date.getTime()) : new Date());

/**
 * Returns i18n month names array.
 * If `abbreviated` is provided, returns abbreviated
 * name.
 *
 * @param {Boolean} abbreviated
 */
export const getMonthNames = abbreviated => {
  if (abbreviated) {
    return [
      s__('Jan'),
      s__('Feb'),
      s__('Mar'),
      s__('Apr'),
      s__('May'),
      s__('Jun'),
      s__('Jul'),
      s__('Aug'),
      s__('Sep'),
      s__('Oct'),
      s__('Nov'),
      s__('Dec'),
    ];
  }
  return [
    s__('January'),
    s__('February'),
    s__('March'),
    s__('April'),
    s__('May'),
    s__('June'),
    s__('July'),
    s__('August'),
    s__('September'),
    s__('October'),
    s__('November'),
    s__('December'),
  ];
};

export const pad = (val, len = 2) => `0${val}`.slice(-len);

/**
 * Given a date object returns the day of the week in English
 * @param {date} date
 * @returns {String}
 */
export const getDayName = date =>
  ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];

/**
 * @example
 * dateFormat('2017-12-05','mmm d, yyyy h:MMtt Z' ) -> "Dec 5, 2017 12:00am GMT+0000"
 * @param {date} datetime
 * @returns {String}
 */
export const formatDate = datetime => dateFormat(datetime, 'mmm d, yyyy h:MMtt Z');

/**
 * Timeago uses underscores instead of dashes to separate language from country code.
 *
 * see https://github.com/hustcc/timeago.js/tree/v3.0.0/locales
 */
const timeagoLanguageCode = languageCode().replace(/-/g, '_');

let timeagoInstance;

/**
 * Sets a timeago Instance
 */
export const getTimeago = () => {
  if (!timeagoInstance) {
    const memoizedLocaleRemaining = () => {
      const cache = [];

      return (number, index) => {
        if (cache[index]) {
          return cache[index];
        }
        let result = [];
        if (index === 0) {
          result = [s__('Timeago|just now'), s__('Timeago|right now')];
        } else if (index === 1) {
          result = [s__('Timeago|%s seconds ago'), s__('Timeago|%s seconds remaining')];
        } else if (index === 2) {
          result = [s__('Timeago|1 minute ago'), s__('Timeago|1 minute remaining')];
        } else if (index === 3) {
          result = [s__('Timeago|%s minutes ago'), s__('Timeago|%s minutes remaining')];
        } else if (index === 4) {
          result = [s__('Timeago|1 hour ago'), s__('Timeago|1 hour remaining')];
        } else if (index === 5) {
          result = [s__('Timeago|%s hours ago'), s__('Timeago|%s hours remaining')];
        } else if (index === 6) {
          result = [s__('Timeago|1 day ago'), s__('Timeago|1 day remaining')];
        } else if (index === 7) {
          result = [s__('Timeago|%s days ago'), s__('Timeago|%s days remaining')];
        } else if (index === 8) {
          result = [s__('Timeago|1 week ago'), s__('Timeago|1 week remaining')];
        } else if (index === 9) {
          result = [s__('Timeago|%s weeks ago'), s__('Timeago|%s weeks remaining')];
        } else if (index === 10) {
          result = [s__('Timeago|1 month ago'), s__('Timeago|1 month remaining')];
        } else if (index === 11) {
          result = [s__('Timeago|%s months ago'), s__('Timeago|%s months remaining')];
        } else if (index === 12) {
          result = [s__('Timeago|1 year ago'), s__('Timeago|1 year remaining')];
        } else if (index === 13) {
          result = [s__('Timeago|%s years ago'), s__('Timeago|%s years remaining')];
        }
        cache[index] = result;
        return result;
      };
    };

    const memoizedLocale = () => {
      const cache = [];

      return (number, index) => {
        if (cache[index]) {
          return cache[index];
        }
        let result = [];
        if (index === 0) {
          result = [s__('Timeago|just now'), s__('Timeago|right now')];
        } else if (index === 1) {
          result = [s__('Timeago|%s seconds ago'), s__('Timeago|in %s seconds')];
        } else if (index === 2) {
          result = [s__('Timeago|1 minute ago'), s__('Timeago|in 1 minute')];
        } else if (index === 3) {
          result = [s__('Timeago|%s minutes ago'), s__('Timeago|in %s minutes')];
        } else if (index === 4) {
          result = [s__('Timeago|1 hour ago'), s__('Timeago|in 1 hour')];
        } else if (index === 5) {
          result = [s__('Timeago|%s hours ago'), s__('Timeago|in %s hours')];
        } else if (index === 6) {
          result = [s__('Timeago|1 day ago'), s__('Timeago|in 1 day')];
        } else if (index === 7) {
          result = [s__('Timeago|%s days ago'), s__('Timeago|in %s days')];
        } else if (index === 8) {
          result = [s__('Timeago|1 week ago'), s__('Timeago|in 1 week')];
        } else if (index === 9) {
          result = [s__('Timeago|%s weeks ago'), s__('Timeago|in %s weeks')];
        } else if (index === 10) {
          result = [s__('Timeago|1 month ago'), s__('Timeago|in 1 month')];
        } else if (index === 11) {
          result = [s__('Timeago|%s months ago'), s__('Timeago|in %s months')];
        } else if (index === 12) {
          result = [s__('Timeago|1 year ago'), s__('Timeago|in 1 year')];
        } else if (index === 13) {
          result = [s__('Timeago|%s years ago'), s__('Timeago|in %s years')];
        }
        cache[index] = result;
        return result;
      };
    };

    timeago.register(timeagoLanguageCode, memoizedLocale());
    timeago.register(`${timeagoLanguageCode}-remaining`, memoizedLocaleRemaining());

    timeagoInstance = timeago();
  }

  return timeagoInstance;
};

/**
 * For the given elements, sets a tooltip with a formatted date.
 * @param {JQuery} $timeagoEls
 * @param {Boolean} setTimeago
 */
export const localTimeAgo = ($timeagoEls, setTimeago = true) => {
  getTimeago().render($timeagoEls, timeagoLanguageCode);

  if (!setTimeago) {
    return;
  }

  function addTimeAgoTooltip() {
    $timeagoEls.each((i, el) => {
      // Recreate with custom template
      $(el).tooltip({
        template:
          '<div class="tooltip local-timeago" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>',<commit_msg>Remove big if-else structure in favor of array<commit_after>import $ from 'jquery';
import _ from 'underscore';
import timeago from 'timeago.js';
import dateFormat from 'dateformat';
import { pluralize } from './text_utility';
import { languageCode, s__ } from '../../locale';

window.timeago = timeago;

/**
 * This method allows you to create new Date instance from existing
 * date instance without keeping the reference.
 *
 * @param {Date} date
 */
export const newDate = date => (date instanceof Date ? new Date(date.getTime()) : new Date());

/**
 * Returns i18n month names array.
 * If `abbreviated` is provided, returns abbreviated
 * name.
 *
 * @param {Boolean} abbreviated
 */
export const getMonthNames = abbreviated => {
  if (abbreviated) {
    return [
      s__('Jan'),
      s__('Feb'),
      s__('Mar'),
      s__('Apr'),
      s__('May'),
      s__('Jun'),
      s__('Jul'),
      s__('Aug'),
      s__('Sep'),
      s__('Oct'),
      s__('Nov'),
      s__('Dec'),
    ];
  }
  return [
    s__('January'),
    s__('February'),
    s__('March'),
    s__('April'),
    s__('May'),
    s__('June'),
    s__('July'),
    s__('August'),
    s__('September'),
    s__('October'),
    s__('November'),
    s__('December'),
  ];
};

export const pad = (val, len = 2) => `0${val}`.slice(-len);

/**
 * Given a date object returns the day of the week in English
 * @param {date} date
 * @returns {String}
 */
export const getDayName = date =>
  ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];

/**
 * @example
 * dateFormat('2017-12-05','mmm d, yyyy h:MMtt Z' ) -> "Dec 5, 2017 12:00am GMT+0000"
 * @param {date} datetime
 * @returns {String}
 */
export const formatDate = datetime => dateFormat(datetime, 'mmm d, yyyy h:MMtt Z');

/**
 * Timeago uses underscores instead of dashes to separate language from country code.
 *
 * see https://github.com/hustcc/timeago.js/tree/v3.0.0/locales
 */
const timeagoLanguageCode = languageCode().replace(/-/g, '_');

let timeagoInstance;

/**
 * Sets a timeago Instance
 */
export const getTimeago = () => {
  if (!timeagoInstance) {
    const memoizedLocaleRemaining = () => {
      const cache = [];

      const timeAgoLocaleRemaining = [
        () => [s__('Timeago|just now'), s__('Timeago|right now')],
        () => [s__('Timeago|%s seconds ago'), s__('Timeago|%s seconds remaining')],
        () => [s__('Timeago|1 minute ago'), s__('Timeago|1 minute remaining')],
        () => [s__('Timeago|%s minutes ago'), s__('Timeago|%s minutes remaining')],
        () => [s__('Timeago|1 hour ago'), s__('Timeago|1 hour remaining')],
        () => [s__('Timeago|%s hours ago'), s__('Timeago|%s hours remaining')],
        () => [s__('Timeago|1 day ago'), s__('Timeago|1 day remaining')],
        () => [s__('Timeago|%s days ago'), s__('Timeago|%s days remaining')],
        () => [s__('Timeago|1 week ago'), s__('Timeago|1 week remaining')],
        () => [s__('Timeago|%s weeks ago'), s__('Timeago|%s weeks remaining')],
        () => [s__('Timeago|1 month ago'), s__('Timeago|1 month remaining')],
        () => [s__('Timeago|%s months ago'), s__('Timeago|%s months remaining')],
        () => [s__('Timeago|1 year ago'), s__('Timeago|1 year remaining')],
        () => [s__('Timeago|%s years ago'), s__('Timeago|%s years remaining')],
      ];

      return (number, index) => {
        if (cache[index]) {
          return cache[index];
        }
        cache[index] = timeAgoLocaleRemaining[index] && timeAgoLocaleRemaining[index]();
        return cache[index];
      };
    };

    const memoizedLocale = () => {
      const cache = [];

      const timeAgoLocale = [
        () => [s__('Timeago|just now'), s__('Timeago|right now')],
        () => [s__('Timeago|%s seconds ago'), s__('Timeago|in %s seconds')],
        () => [s__('Timeago|1 minute ago'), s__('Timeago|in 1 minute')],
        () => [s__('Timeago|%s minutes ago'), s__('Timeago|in %s minutes')],
        () => [s__('Timeago|1 hour ago'), s__('Timeago|in 1 hour')],
        () => [s__('Timeago|%s hours ago'), s__('Timeago|in %s hours')],
        () => [s__('Timeago|1 day ago'), s__('Timeago|in 1 day')],
        () => [s__('Timeago|%s days ago'), s__('Timeago|in %s days')],
        () => [s__('Timeago|1 week ago'), s__('Timeago|in 1 week')],
        () => [s__('Timeago|%s weeks ago'), s__('Timeago|in %s weeks')],
        () => [s__('Timeago|1 month ago'), s__('Timeago|in 1 month')],
        () => [s__('Timeago|%s months ago'), s__('Timeago|in %s months')],
        () => [s__('Timeago|1 year ago'), s__('Timeago|in 1 year')],
        () => [s__('Timeago|%s years ago'), s__('Timeago|in %s years')],
      ];

      return (number, index) => {
        if (cache[index]) {
          return cache[index];
        }
        cache[index] = timeAgoLocale[index] && timeAgoLocale[index]();
        return cache[index];
      };
    };

    timeago.register(timeagoLanguageCode, memoizedLocale());
    timeago.register(`${timeagoLanguageCode}-remaining`, memoizedLocaleRemaining());

    timeagoInstance = timeago();
  }

  return timeagoInstance;
};

/**
 * For the given elements, sets a tooltip with a formatted date.
 * @param {JQuery} $timeagoEls
 * @param {Boolean} setTimeago
 */
export const localTimeAgo = ($timeagoEls, setTimeago = true) => {
  getTimeago().render($timeagoEls, timeagoLanguageCode);

  if (!setTimeago) {
    return;
  }

  function addTimeAgoTooltip() {
    $timeagoEls.each((i, el) => {
      // Recreate with custom template
      $(el).tooltip({
        template:
          '<div class="tooltip local-timeago" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>',
{'fraction_non_alphanumeric': 0.19082047116165718, 'fraction_numerical': 0.011372867587327376, 'mean_word_length': 2.942042907460775, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 69, 'https://': 2, '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': '2136393', 'n_tokens_mistral': 4190, 'n_tokens_neox': 4015, 'n_words': 1030}