Skip to content

Commit 8f3435c

Browse files
committed
perf: [10196] add custom function to parse dates
1 parent 1546f25 commit 8f3435c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/FileUtils/AgGridUtils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ const _getColTypeFromTypeArray = (typeArray) => {
8585
return 'string'; // Fall back to default type
8686
};
8787

88+
const validateDateFormat = (value, options = {}) => {
89+
if (typeof value !== 'string') return false;
90+
const { dateFormat = 'yyyy-MM-dd' } = options;
91+
92+
const formatRegex = dateFormat
93+
.replace(/yyyy/, '\\d{4}')
94+
.replace(/MM/, '(0[1-9]|1[0-2])')
95+
.replace(/dd/, '(0[1-9]|[12]\\d|3[01])')
96+
.replace(/\//g, '\\/')
97+
.replace(/-/g, '-');
98+
99+
const regex = new RegExp(`^${formatRegex}$`);
100+
return regex.test(value);
101+
};
102+
88103
const _validateFormat = (rows, hasHeader, cols, options) => {
89104
const colsData = cols.map((col) => ({ ...col, type: _getColTypeFromTypeArray(col.type) }));
90105
const errors = [];
@@ -116,6 +131,15 @@ const _validateFormat = (rows, hasHeader, cols, options) => {
116131
const value = row[colIndex];
117132
if (value === undefined) continue;
118133

134+
if (type === 'date' && !validateDateFormat(value, colOptions)) {
135+
const expectedFormat = colOptions.dateFormat || 'dd/MM/yyyy';
136+
const errorSummary = 'Incorrect date value';
137+
const errorContext = `Incorrect value: "${value}" for type date\nExpected format: ${expectedFormat}`;
138+
const errorLoc = `Line ${rowIndex + 1}, Column ${colIndex + 1} ("${field}")`;
139+
errors.push(new PanelError(errorSummary, errorLoc, errorContext));
140+
continue;
141+
}
142+
119143
const validationResult = ValidationUtils.isValid(value, type, colOptions, acceptsEmptyFields);
120144
if (validationResult !== true) {
121145
const { summary: errorSummary, context: errorContext } = validationResult;

0 commit comments

Comments
 (0)