UNPKG

@ministryofjustice/hmpps-digital-prison-reporting-frontend

Version:

The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.

69 lines (66 loc) 2.46 kB
import { calcDatesForAPI } from '../../../utils/durationCalculator.js'; const setDateRangeValuesWithinMinMax = (filter, startValue, endValue) => { const { min, max } = filter; const start = min ? compareMin(min, startValue) : startValue; const end = max ? compareMax(max, endValue) : endValue; return { start: start || '', end: end || '', }; }; const compareMin = (min, dateValue) => { const minDate = new Date(min); const date = dateValue ? new Date(dateValue) : new Date(); return date < minDate ? min : dateValue; }; const compareMax = (max, dateValue) => { const maxDate = new Date(max); const date = dateValue ? new Date(dateValue) : new Date(); return date > maxDate ? max : dateValue; }; /** * Given the filter definition for a date-range with defaults * - will initialise the filter with correct start end dates * - if default relative range it will calc the correct start end dates * * @param {components['schemas']['FilterDefinition']} filter * @return {*} {DateRange} */ const getStartAndEndValueFromDefinition = (filter) => { const { min, max, defaultValue, defaultQuickFilterValue } = filter; let value = { start: '', end: '' }; if (defaultQuickFilterValue) { const relative = defaultQuickFilterValue; value.relative = relative; const calculated = calcDatesForAPI(relative); if (calculated) { value = { ...setDateRangeValuesWithinMinMax(filter, calculated.start, calculated.end), relative, }; } return value; } let startValue = min; let endValue = max; const dateRegEx = /^\d{1,4}-\d{1,2}-\d{2} - \d{1,4}-\d{1,2}-\d{1,2}$/; if (defaultValue && dateRegEx.test(defaultValue)) { [startValue, endValue] = defaultValue.split(' - '); } if (startValue || endValue) { value = setDateRangeValuesWithinMinMax(filter, startValue || undefined, endValue || undefined); } return value; }; function isDateRange(value) { return value.start !== undefined || value.end !== undefined; } var StartEndDatetUtils = { compareMax, compareMin, isDateRange, setDateRangeValuesWithinMinMax, getStartAndEndValueFromDefinition, }; export { compareMax, compareMin, StartEndDatetUtils as default, getStartAndEndValueFromDefinition, isDateRange, setDateRangeValuesWithinMinMax }; //# sourceMappingURL=utils.js.map