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.

92 lines (91 loc) 4.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const dayjs_1 = __importDefault(require("dayjs")); const customParseFormat_1 = __importDefault(require("dayjs/plugin/customParseFormat")); const logger_1 = __importDefault(require("../logger")); class DateMapper { constructor() { this.isoDateRegEx = /^(\d{4}[/-](0?[1-9]|1[012])[/-](0?[1-9]|[12][0-9]|3[01]))/; this.localDateRegEx = /^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]\d{4}$/; this.localDateShortYearRegEx = /^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]\d{2}$/; this.localDateTimeRegEx = /^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]\d{4} [0-2]\d:[0-5]\d$/; this.localDateTimeShortYearRegEx = /^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]\d{2} [0-2]\d:[0-5]\d$/; this.isoFormat = 'YYYY-MM-DD'; this.localDateFormatInput = 'D/M/YYYY'; this.localDateFormatOutput = 'DD/MM/YYYY'; this.localDateShortYearFormatInput = 'D/M/YY'; this.localDateShortYearFormatOutput = 'DD/MM/YY'; this.localDateTimeFormatInput = 'D/M/YYYY HH:mm'; this.localDateTimeFormatOutput = 'DD/MM/YYYY HH:mm'; this.localDateTimeShortYearFormatInput = 'D/M/YY HH:mm'; this.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_1.default.extend(customParseFormat_1.default); switch (this.getDateType(value)) { case 'iso': return (0, dayjs_1.default)(value); case 'local-date': return (0, dayjs_1.default)(value, this.localDateFormatInput); case 'local-date-short-year': return (0, dayjs_1.default)(value, this.localDateShortYearFormatInput); case 'local-datetime': return (0, dayjs_1.default)(value, this.localDateTimeFormatInput); case 'local-datetime-short-year': return (0, dayjs_1.default)(value, this.localDateTimeShortYearFormatInput); default: return null; } } isDate(value) { return this.getDateType(value) !== 'none'; } 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_1.default.warn(`Invalid date type: ${type}`); return null; } } if (value) { logger_1.default.warn(`Could not map non-date value : ${value}`); } return null; } } exports.default = DateMapper;