Commit 503acc86 authored by Rishabh Gupta's avatar Rishabh Gupta Committed by Ezekiel Kigbo

Refactor: moving and renaming timeSummaryForPathNavigation

parent 3d1fac55
import dateFormat from 'dateformat'; import dateFormat from 'dateformat';
import { unescape } from 'lodash';
import { dateFormats } from '~/analytics/shared/constants'; import { dateFormats } from '~/analytics/shared/constants';
import { hideFlash } from '~/flash'; import { hideFlash } from '~/flash';
import { sanitize } from '~/lib/dompurify';
import { roundToNearestHalf } from '~/lib/utils/common_utils';
import { getDateInPast } from '~/lib/utils/datetime/date_calculation_utility'; import { getDateInPast } from '~/lib/utils/datetime/date_calculation_utility';
import { parseSeconds } from '~/lib/utils/datetime_utility'; import { parseSeconds } from '~/lib/utils/datetime_utility';
import { formatTimeAsSummary } from '~/lib/utils/datetime/date_format_utility';
import { slugify } from '~/lib/utils/text_utility'; import { slugify } from '~/lib/utils/text_utility';
import { s__, sprintf } from '../locale';
export const removeFlash = (type = 'alert') => { export const removeFlash = (type = 'alert') => {
const flashEl = document.querySelector(`.flash-${type}`); const flashEl = document.querySelector(`.flash-${type}`);
...@@ -45,29 +42,6 @@ export const transformStagesForPathNavigation = ({ ...@@ -45,29 +42,6 @@ export const transformStagesForPathNavigation = ({
return formattedStages; return formattedStages;
}; };
export const timeSummaryForPathNavigation = ({ seconds, hours, days, minutes, weeks, months }) => {
if (months) {
return sprintf(s__('ValueStreamAnalytics|%{value}M'), {
value: roundToNearestHalf(months),
});
} else if (weeks) {
return sprintf(s__('ValueStreamAnalytics|%{value}w'), {
value: roundToNearestHalf(weeks),
});
} else if (days) {
return sprintf(s__('ValueStreamAnalytics|%{value}d'), {
value: roundToNearestHalf(days),
});
} else if (hours) {
return sprintf(s__('ValueStreamAnalytics|%{value}h'), { value: hours });
} else if (minutes) {
return sprintf(s__('ValueStreamAnalytics|%{value}m'), { value: minutes });
} else if (seconds) {
return unescape(sanitize(s__('ValueStreamAnalytics|<1m'), { ALLOWED_TAGS: [] }));
}
return '-';
};
/** /**
* Takes a raw median value in seconds and converts it to a string representation * Takes a raw median value in seconds and converts it to a string representation
* ie. converts 172800 => 2d (2 days) * ie. converts 172800 => 2d (2 days)
...@@ -76,7 +50,7 @@ export const timeSummaryForPathNavigation = ({ seconds, hours, days, minutes, we ...@@ -76,7 +50,7 @@ export const timeSummaryForPathNavigation = ({ seconds, hours, days, minutes, we
* @returns {String} String representation ie 2w * @returns {String} String representation ie 2w
*/ */
export const medianTimeToParsedSeconds = (value) => export const medianTimeToParsedSeconds = (value) =>
timeSummaryForPathNavigation({ formatTimeAsSummary({
...parseSeconds(value, { daysPerWeek: 7, hoursPerDay: 24 }), ...parseSeconds(value, { daysPerWeek: 7, hoursPerDay: 24 }),
seconds: value, seconds: value,
}); });
......
import dateFormat from 'dateformat'; import dateFormat from 'dateformat';
import { isString, mapValues, reduce, isDate } from 'lodash'; import { isString, mapValues, reduce, isDate, unescape } from 'lodash';
import { s__, n__, __ } from '../../../locale'; import { roundToNearestHalf } from '~/lib/utils/common_utils';
import { sanitize } from '~/lib/dompurify';
import { s__, n__, __, sprintf } from '../../../locale';
/** /**
* Returns i18n month names array. * Returns i18n month names array.
...@@ -361,3 +363,26 @@ export const dateToTimeInputValue = (date) => { ...@@ -361,3 +363,26 @@ export const dateToTimeInputValue = (date) => {
hour12: false, hour12: false,
}); });
}; };
export const formatTimeAsSummary = ({ seconds, hours, days, minutes, weeks, months }) => {
if (months) {
return sprintf(s__('ValueStreamAnalytics|%{value}M'), {
value: roundToNearestHalf(months),
});
} else if (weeks) {
return sprintf(s__('ValueStreamAnalytics|%{value}w'), {
value: roundToNearestHalf(weeks),
});
} else if (days) {
return sprintf(s__('ValueStreamAnalytics|%{value}d'), {
value: roundToNearestHalf(days),
});
} else if (hours) {
return sprintf(s__('ValueStreamAnalytics|%{value}h'), { value: hours });
} else if (minutes) {
return sprintf(s__('ValueStreamAnalytics|%{value}m'), { value: minutes });
} else if (seconds) {
return unescape(sanitize(s__('ValueStreamAnalytics|<1m'), { ALLOWED_TAGS: [] }));
}
return '-';
};
import { useFakeDate } from 'helpers/fake_date'; import { useFakeDate } from 'helpers/fake_date';
import { import {
transformStagesForPathNavigation, transformStagesForPathNavigation,
timeSummaryForPathNavigation,
medianTimeToParsedSeconds, medianTimeToParsedSeconds,
formatMedianValues, formatMedianValues,
filterStagesByHiddenStatus, filterStagesByHiddenStatus,
...@@ -47,21 +46,6 @@ describe('Value stream analytics utils', () => { ...@@ -47,21 +46,6 @@ describe('Value stream analytics utils', () => {
}); });
}); });
describe('timeSummaryForPathNavigation', () => {
it.each`
unit | value | result
${'months'} | ${1.5} | ${'1.5M'}
${'weeks'} | ${1.25} | ${'1.5w'}
${'days'} | ${2} | ${'2d'}
${'hours'} | ${10} | ${'10h'}
${'minutes'} | ${20} | ${'20m'}
${'seconds'} | ${10} | ${'<1m'}
${'seconds'} | ${0} | ${'-'}
`('will format $value $unit to $result', ({ unit, value, result }) => {
expect(timeSummaryForPathNavigation({ [unit]: value })).toBe(result);
});
});
describe('medianTimeToParsedSeconds', () => { describe('medianTimeToParsedSeconds', () => {
it.each` it.each`
value | result value | result
......
...@@ -118,3 +118,18 @@ describe('date_format_utility.js', () => { ...@@ -118,3 +118,18 @@ describe('date_format_utility.js', () => {
}); });
}); });
}); });
describe('formatTimeAsSummary', () => {
it.each`
unit | value | result
${'months'} | ${1.5} | ${'1.5M'}
${'weeks'} | ${1.25} | ${'1.5w'}
${'days'} | ${2} | ${'2d'}
${'hours'} | ${10} | ${'10h'}
${'minutes'} | ${20} | ${'20m'}
${'seconds'} | ${10} | ${'<1m'}
${'seconds'} | ${0} | ${'-'}
`('will format $value $unit to $result', ({ unit, value, result }) => {
expect(utils.formatTimeAsSummary({ [unit]: value })).toBe(result);
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment