Sha256: 611890eabb212b896c2488eb776cd75443fe4fc757a82c0137c8ba937e435396

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

import { translate as __, documentLocale } from 'foremanReact/common/I18n';
import React from 'react';
import { FormattedRelative } from 'react-intl';
import humanizeDuration from 'humanize-duration';

const formatDate = date => {
  // Firefox doesnt format dd-mm-yyyy type strings to date
  if (typeof date === 'string' || date instanceof String)
    return new Date(date.replace(/-/, '/').replace(/-/, '/'));
  return date;
};

export const timeInWords = time => {
  if (!time) return __('N/A');
  time = formatDate(time);
  return <FormattedRelative value={time} />;
};

export const durationInWords = (
  start,
  finish,
  selectedLocale = documentLocale()
) => {
  if (!start) return __('N/A');
  start = formatDate(start).getTime();
  finish = formatDate(finish).getTime();
  return {
    text: humanizeDuration(new Date(finish - start).getTime(), {
      largest: 1,
      language: selectedLocale,
      fallbacks: ['en'],
    }),
    tooltip: `${numberWithDelimiter((finish - start) / 1000)} ${__('seconds')}`,
  };
};

const numberWithDelimiter = x =>
  x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
foreman-tasks-0.16.3 webpack/ForemanTasks/Components/TaskDetails/Components/TaskHelper.js
foreman-tasks-0.16.2 webpack/ForemanTasks/Components/TaskDetails/Components/TaskHelper.js
foreman-tasks-0.16.1 webpack/ForemanTasks/Components/TaskDetails/Components/TaskHelper.js
foreman-tasks-0.17.0 webpack/ForemanTasks/Components/TaskDetails/Components/TaskHelper.js