@kubit-ui-web/react-components
Version:
Kubit React Components is a customizable, accessible library of React web components, designed to enhance your application's user experience
45 lines • 2.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyFormat = exports.formatValueDate = void 0;
const validateDate_1 = require("../../../utils/date/validateDate");
const verifyYear_1 = require("./verifyYear");
const formatValueDate = (value, today, format, dateSeparator = '') => {
let formattedValue = value.replace(dateSeparator, '').replace(today, '');
if (value.length > format?.length) {
formattedValue = formattedValue.replace(/\s/g, '');
}
return formattedValue;
};
exports.formatValueDate = formatValueDate;
const matchFormatValueDate = (value, today, format, dateSeparator = '') => {
return (0, exports.formatValueDate)(value, today, format, dateSeparator).match(new RegExp('.{1,' + format.length + '}', 'g'));
};
const verifyFormatDate = (value, minDate, maxDate, today, dateSeparator, format) => {
const valueFormatted = (0, exports.formatValueDate)(value, today, format, dateSeparator);
const date = new Date(valueFormatted);
const hasError = !(0, validateDate_1.isValidDate)(date) ||
valueFormatted.length !== format.length ||
!(0, verifyYear_1.verifyYear)(valueFormatted, minDate, maxDate);
return { valueFormatted, hasError };
};
const verifyFormatRangeDate = (value, minDate, maxDate, today, dateSeparator, format) => {
const valueFormatted = matchFormatValueDate(value, today, format, dateSeparator);
if ((valueFormatted?.[0] && !(0, validateDate_1.isValidDate)(new Date(valueFormatted[0]))) ||
(valueFormatted?.[1] && !(0, validateDate_1.isValidDate)(new Date(valueFormatted[1])))) {
return { valueFormatted, hasError: true };
}
const condition1 = valueFormatted?.[0]?.length === format.length && valueFormatted?.[1]?.length === format.length;
const condition2 = valueFormatted &&
(0, verifyYear_1.verifyYear)(valueFormatted[0], minDate, maxDate) &&
(0, verifyYear_1.verifyYear)(valueFormatted[1], minDate, maxDate);
const hasError = !condition1 || !condition2;
return { valueFormatted, hasError };
};
const verifyFormat = (format, value, minDate, maxDate, today = '', dateSeparator = '', isRange) => {
if (isRange) {
return verifyFormatRangeDate(value, minDate, maxDate, today, dateSeparator, format);
}
return verifyFormatDate(value, minDate, maxDate, today, dateSeparator, format);
};
exports.verifyFormat = verifyFormat;
//# sourceMappingURL=verifyFormat.js.map