mirror of
https://github.com/janishutz/fundamentals-of-webengineering.git
synced 2025-11-25 22:04:24 +00:00
Task 3: React Frontend
This commit is contained in:
30
task_3_react/src/client/csv.ts
Normal file
30
task_3_react/src/client/csv.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { CSV_Data } from './types';
|
||||
import { csv2json } from 'json-2-csv';
|
||||
|
||||
const convertCSVtoJSON = async ( csvText: string ) => {
|
||||
// Type cast OK, as the typing of the external library is not perfect -> Actually it is.
|
||||
// NOTE: On transpilation to JS, it will be (more or less) disregarded anyway.
|
||||
// If you claim it isn't good typing, it's the same as expecting it to guess the typing,
|
||||
// which is literally impossible in TS. But sure...
|
||||
return ( await csv2json( csvText ) ) as CSV_Data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads a CSV file and returns the data as JSON.
|
||||
* @param event The change event of the file input.
|
||||
*/
|
||||
export const readCSV = async ( event: React.ChangeEvent<HTMLInputElement> ): Promise<CSV_Data> => {
|
||||
if ( !( event.target instanceof HTMLInputElement ) ) {
|
||||
throw new Error( 'Not an HTMLInputElement' );
|
||||
}
|
||||
|
||||
const file = event.target.files?.[0];
|
||||
|
||||
if ( file == null ) {
|
||||
throw new Error( 'No file selected' );
|
||||
}
|
||||
|
||||
const result = await file.text();
|
||||
|
||||
return await convertCSVtoJSON( result );
|
||||
};
|
||||
Reference in New Issue
Block a user