Skip to content

Commit

Permalink
Move the 'apply' function
Browse files Browse the repository at this point in the history
  • Loading branch information
fhennig committed Dec 26, 2024
1 parent d8741cf commit 6bda7bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
18 changes: 1 addition & 17 deletions website/src/components/Submission/DataUploadForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,6 @@ const DevExampleData = ({
);
};

// TODO test this function
async function createRemappedTsvFile(tsvFile: File, columnMapping: ColumnMapping): Promise<File> {
const text = await tsvFile.text();
const inputRows = text.split('\n');
const headersInFile = inputRows.splice(0, 1)[0].split('\t');
const headers: string[] = [];
const indicies: number[] = [];
columnMapping.entries().forEach(([k, v]) => {
headers.push(k);
indicies.push(headersInFile.findIndex((s) => s === v));
});
const newRows = inputRows.map((r) => r.split('\t')).map((row) => indicies.map((i) => row[i]));
const newFileContent = [headers, ...newRows].map((row) => row.join('\t')).join('\n');
return new File([newFileContent], 'remapped.tsv');
}

const InnerDataUploadForm = ({
accessToken,
organism,
Expand Down Expand Up @@ -197,7 +181,7 @@ const InnerDataUploadForm = ({
let finalMetadataFile = metadataFile;

if (columnMapping !== null) {
finalMetadataFile = await createRemappedTsvFile(metadataFile, columnMapping);
finalMetadataFile = await columnMapping.applyTo(metadataFile);
}

switch (action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ export class ColumnMapping {
newMapping.set(k, v);
return new ColumnMapping(newMapping);
}

public async applyTo(tsvFile: File): Promise<File> {
const text = await tsvFile.text();
const inputRows = text.split('\n');
const headersInFile = inputRows.splice(0, 1)[0].split('\t');
const headers: string[] = [];
const indicies: number[] = [];
this.entries().forEach(([k, v]) => {
headers.push(k);
indicies.push(headersInFile.findIndex((s) => s === v));
});
const newRows = inputRows.map((r) => r.split('\t')).map((row) => indicies.map((i) => row[i]));
const newFileContent = [headers, ...newRows].map((row) => row.join('\t')).join('\n');
return new File([newFileContent], 'remapped.tsv');
}
}

interface ColumnMappingModalProps {
Expand Down

0 comments on commit 6bda7bd

Please sign in to comment.