Skip to content

Commit 82bda52

Browse files
committed
Update conversion code for columns config for missing values and display patterns
1 parent 9700848 commit 82bda52

File tree

1 file changed

+58
-14
lines changed

1 file changed

+58
-14
lines changed

src/lib/components/Table/shared.ts

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,35 +107,79 @@ export const resetResize = (
107107
}
108108
};
109109

110-
export const missingValuesFn = (key: number, missingValues: { [key: string | number]: string }) => {
111-
return key in missingValues ? missingValues[key] : key.toString();
110+
export const missingValuesFn = (
111+
key: number | string,
112+
missingValues: { [key: string | number]: string }
113+
) => {
114+
const foundKey =
115+
typeof key === 'number' && key.toString().includes('e')
116+
? Object.keys(missingValues).find((item) => {
117+
return (item as string).toLowerCase() === key.toString().toLowerCase();
118+
})
119+
: typeof key === 'string' && parseInt(key).toString().length !== key.length && new Date(key)
120+
? Object.keys(missingValues).find(
121+
(item) => new Date(item).getTime() === new Date(key).getTime()
122+
)
123+
: key in missingValues
124+
? key
125+
: undefined;
126+
127+
return foundKey ? missingValues[foundKey] : key;
112128
};
113129

114-
export const convertServerColumns = (columns: ServerColumn[]) => {
130+
export const convertServerColumns = (
131+
serverColumns: ServerColumn[],
132+
columns: Columns | undefined
133+
) => {
115134
const columnsConfig: Columns = {};
116135

117-
columns.forEach((col) => {
136+
serverColumns.forEach((col) => {
118137
let instructions = {};
119138

120139
if (col.instructions?.displayPattern) {
140+
let dp = col.instructions.displayPattern;
141+
142+
// Swap 'm' and 'M' to match the backend date format
143+
for (let i = 0; i < col.instructions.displayPattern.length; i++) {
144+
if (col.instructions.displayPattern[i] === 'm') {
145+
dp = `${dp.slice(0, i)}M${dp.slice(i + 1)}`;
146+
} else if (col.instructions.displayPattern[i] === 'M') {
147+
dp = `${dp.slice(0, i)}m${dp.slice(i + 1)}`;
148+
}
149+
}
150+
121151
instructions = {
122-
toStringFn: (date: Date) => dateFormat(date, col.instructions?.displayPattern || ''),
123-
toSortableValueFn: (date: Date) => date.getTime(),
124-
toFilterableValueFn: (date: Date) => date
152+
toStringFn: (date: string) => {
153+
if (col.instructions?.missingValues) {
154+
const missingValue = missingValuesFn(date, col.instructions?.missingValues || {});
155+
if (missingValue === date) {
156+
return dateFormat(new Date(date), dp);
157+
}
158+
return missingValue;
159+
} else {
160+
return dateFormat(new Date(date), dp);
161+
}
162+
},
163+
toSortableValueFn: (date: string) => new Date(date).getTime(),
164+
toFilterableValueFn: (date: string) => new Date(date)
125165
};
126-
}
127-
128-
if (col.instructions?.missingValues) {
166+
} else if (col.instructions?.missingValues) {
129167
instructions = {
130168
...instructions,
131169
toStringFn: (key) => missingValuesFn(key, col.instructions?.missingValues || {})
132170
};
133171
}
134172

135-
columnsConfig[col.column] = {
136-
exclude: col.exclude,
137-
instructions
138-
};
173+
if (columns && col.column in columns) {
174+
columnsConfig[col.column] = {
175+
...columns[col.column],
176+
instructions
177+
};
178+
} else {
179+
columnsConfig[col.column] = {
180+
instructions
181+
};
182+
}
139183
});
140184

141185
return columnsConfig;

0 commit comments

Comments
 (0)