@ministryofjustice/hmpps-digital-prison-reporting-frontend
Version:
The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.
110 lines (107 loc) • 4.3 kB
JavaScript
import dayjs from 'dayjs';
import customParse from '../../../_virtual/customParseFormat.js';
import logger from '../logger.js';
class DateMapper {
isoDateRegEx = /^(\d{4}[/-](0?[1-9]|1[012])[/-](0?[1-9]|[12][0-9]|3[01]))/;
localDateRegEx = /^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]\d{4}$/;
localDateShortYearRegEx = /^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]\d{2}$/;
localDateTimeRegEx = /^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]\d{4} [0-2]\d:[0-5]\d$/;
localDateTimeShortYearRegEx = /^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]\d{2} [0-2]\d:[0-5]\d$/;
isoFormat = 'YYYY-MM-DD';
localDateFormatInput = 'D/M/YYYY';
localDateFormatOutput = 'DD/MM/YYYY';
localDateShortYearFormatInput = 'D/M/YY';
localDateShortYearFormatOutput = 'DD/MM/YY';
localDateTimeFormatInput = 'D/M/YYYY HH:mm';
localDateTimeFormatOutput = 'DD/MM/YYYY HH:mm';
localDateTimeShortYearFormatInput = 'D/M/YY HH:mm';
localDateTimeShortYearFormatOutput = 'DD/MM/YY HH:mm';
getDateType(value) {
if (value && value.match) {
if (value.match(this.isoDateRegEx)) {
return 'iso';
}
if (value.match(this.localDateRegEx)) {
return 'local-date';
}
if (value.match(this.localDateShortYearRegEx)) {
return 'local-date-short-year';
}
if (value.match(this.localDateTimeRegEx)) {
return 'local-datetime';
}
if (value.match(this.localDateTimeShortYearRegEx)) {
return 'local-datetime-short-year';
}
}
return 'none';
}
getDateWrapper(value) {
dayjs.extend(customParse);
if (!value) {
return null;
}
switch (this.getDateType(value)) {
case 'iso':
return dayjs(value);
case 'local-date':
return dayjs(value, this.localDateFormatInput);
case 'local-date-short-year':
return dayjs(value, this.localDateShortYearFormatInput);
case 'local-datetime':
return dayjs(value, this.localDateTimeFormatInput);
case 'local-datetime-short-year':
return dayjs(value, this.localDateTimeShortYearFormatInput);
default:
return null;
}
}
getFormats = () => {
return [
this.localDateFormatInput,
this.localDateFormatOutput,
this.localDateShortYearFormatInput,
this.localDateShortYearFormatOutput,
this.localDateTimeFormatInput,
this.localDateTimeFormatInput,
this.localDateTimeFormatOutput,
this.localDateTimeShortYearFormatInput,
this.localDateTimeShortYearFormatOutput,
];
};
isDate(value) {
return this.getDateType(value) !== 'none';
}
parseIfDate = (value) => {
if (typeof value !== 'string')
return null;
const formats = this.getFormats();
return formats.map(fmt => dayjs(value, fmt, true)).find(d => d.isValid()) ?? null;
};
toDateString(value, type) {
const dateWrapper = this.getDateWrapper(value);
if (dateWrapper) {
switch (type) {
case 'iso':
return dateWrapper.format(this.isoFormat);
case 'local-date':
return dateWrapper.format(this.localDateFormatOutput);
case 'local-date-short-year':
return dateWrapper.format(this.localDateShortYearFormatOutput);
case 'local-datetime':
return dateWrapper.format(this.localDateTimeFormatOutput);
case 'local-datetime-short-year':
return dateWrapper.format(this.localDateTimeShortYearFormatOutput);
default:
logger.warn(`Invalid date type: ${type}`);
return undefined;
}
}
if (value) {
logger.warn(`Could not map non-date value : ${value}`);
}
return undefined;
}
}
export { DateMapper, DateMapper as default };
//# sourceMappingURL=DateMapper.js.map