Skip to content

Commit

Permalink
add more debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikGlinka committed Feb 26, 2024
1 parent 6d373b7 commit 3d6f1c9
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions js/rejseplanen/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,26 @@ export default class Csv extends EventTarget {
if (!response.ok) throw new Error('Failed to load csv: ' + response.status + '\n' + response.statusText);
let data = await response.arrayBuffer();

if (data.byteLength === 0) throw new Error('CSV: No data in file.');

const decoder = new TextDecoder(options.encoding ?? 'utf-8');

console.log('CSV: decoding data with: ' + (options.encoding ?? 'utf-8'));

data = decoder.decode(data);

if (data.length === 0) throw new Error('CSV: Decode failed.');

console.log('CSV: decoded data: ' + data);

if (options.regexReplace) console.log('CSV: replacing regex: ' + options.regexReplace);

if (options.regexReplace) options.regexReplace.forEach(pair => data = data.replace(pair[0], pair[1]));

const dataArray = data.split('\r\n').map( row => row.split(';') );

console.log('CSV: data array: ' + dataArray);

if (options.skipRows) dataArray.splice(0, options.skipRows);

const json = dataArray.map( row => {
Expand Down

0 comments on commit 3d6f1c9

Please sign in to comment.