Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/containers/Tenant/Query/utils/getPreparedResult.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {KeyValueRow} from '../../../../types/api/query';

export const getPreparedResult = (data: KeyValueRow[] | undefined) => {
export function getPreparedResult(data: KeyValueRow[] | undefined) {
const columnDivider = '\t';
const rowDivider = '\n';

Expand All @@ -9,21 +9,22 @@ export const getPreparedResult = (data: KeyValueRow[] | undefined) => {
}

const columnHeaders = Object.keys(data[0]);
const rows = Array<string[] | KeyValueRow[]>(columnHeaders).concat(data);

return rows
.map((item) => {
const row = [];

for (const field in item) {
if (typeof item[field] === 'object' || Array.isArray(item[field])) {
row.push(JSON.stringify(item[field]));
} else {
row.push(item[field]);
}
}
const rows = [columnHeaders.map(escapeValue).join(columnDivider)];
for (const row of data) {
const value = [];
for (const column of columnHeaders) {
const v = row[column];
value.push(escapeValue(typeof v === 'object' ? JSON.stringify(v) : `${v}`));
}
rows.push(value.join(columnDivider));
}
return rows.join(rowDivider);
}

return row.join(columnDivider);
})
.join(rowDivider);
};
function escapeValue(value: string) {
return value
.replaceAll('\\', '\\\\')
.replaceAll('\n', '\\n')
.replaceAll('\r', '\\r')
.replaceAll('\t', '\\t');
}