2021-12-29 11:42:07 +00:00
|
|
|
import { uniq, without, isNaN } from "lodash";
|
2021-04-07 01:03:30 +00:00
|
|
|
import { ColumnProperties } from "./Constants";
|
|
|
|
|
|
2021-08-10 07:57:29 +00:00
|
|
|
const removeSpecialChars = (value: string, limit?: number) => {
|
|
|
|
|
const separatorRegex = /\W+/;
|
2021-02-25 11:51:54 +00:00
|
|
|
return value
|
|
|
|
|
.split(separatorRegex)
|
|
|
|
|
.join("_")
|
|
|
|
|
.slice(0, limit || 30);
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-16 10:29:08 +00:00
|
|
|
export const getAllTableColumnKeys = (
|
2021-08-10 10:21:27 +00:00
|
|
|
tableData?: Array<Record<string, unknown>>,
|
2021-02-16 10:29:08 +00:00
|
|
|
) => {
|
|
|
|
|
const columnKeys: string[] = [];
|
2021-08-10 10:21:27 +00:00
|
|
|
if (tableData) {
|
|
|
|
|
for (let i = 0, tableRowCount = tableData.length; i < tableRowCount; i++) {
|
|
|
|
|
const row = tableData[i];
|
|
|
|
|
for (const key in row) {
|
|
|
|
|
// Replace all special characters to _, limit key length to 200 characters.
|
|
|
|
|
const sanitizedKey = removeSpecialChars(key, 200);
|
|
|
|
|
if (!columnKeys.includes(sanitizedKey)) {
|
|
|
|
|
columnKeys.push(sanitizedKey);
|
|
|
|
|
}
|
2021-02-16 10:29:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return columnKeys;
|
|
|
|
|
};
|
2021-04-07 01:03:30 +00:00
|
|
|
|
|
|
|
|
export const reorderColumns = (
|
|
|
|
|
columns: Record<string, ColumnProperties>,
|
|
|
|
|
columnOrder: string[],
|
|
|
|
|
) => {
|
|
|
|
|
const newColumnsInOrder: Record<string, ColumnProperties> = {};
|
|
|
|
|
uniq(columnOrder).forEach((id: string, index: number) => {
|
|
|
|
|
if (columns[id]) newColumnsInOrder[id] = { ...columns[id], index };
|
|
|
|
|
});
|
|
|
|
|
const remaining = without(
|
|
|
|
|
Object.keys(columns),
|
|
|
|
|
...Object.keys(newColumnsInOrder),
|
|
|
|
|
);
|
|
|
|
|
const len = Object.keys(newColumnsInOrder).length;
|
|
|
|
|
if (remaining && remaining.length > 0) {
|
|
|
|
|
remaining.forEach((id: string, index: number) => {
|
|
|
|
|
newColumnsInOrder[id] = { ...columns[id], index: len + index };
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return newColumnsInOrder;
|
|
|
|
|
};
|
2021-12-29 11:42:07 +00:00
|
|
|
|
|
|
|
|
// check and update column id if it is number
|
|
|
|
|
export const generateTableColumnId = (accessor: string) => {
|
|
|
|
|
return isNaN(Number(accessor)) ? accessor : `_${accessor}`;
|
|
|
|
|
};
|