<commit_before>var app = require('express');
// var path = require('path');
var router = app.Router();
var csv = require('csv');
var fs = require('fs');

/* GET csv handling site. */
router.get('/', function (req, res, next) {
  res.render('csv', {});
});

/* POST csv users listing. */
router.post('/', function (req, res, next) {
  if (req.files && req.files.csvFile) { 
    if (req.files.csvFile.extension === 'csv') {
      var fileName = req.files.csvFile.name,
      filePath = req.files.csvFile.path;
      
      fs.readFile(filePath, {encoding: 'utf-8'}, function (err, data) {
        if (err) res.status(400).send('Error processing file');
        else {
          csv.parse(data, {comment: '//', delimiter: ';', rowDelimiter: '\n'}, function (err, output) {
            if (err) res.status(400).send('Error parsing CSV file. Check file syntax before trying again');
            else {
              var transformedData = transformData(output); 
              csv.stringify(transformedData, {}, function (err, output) {
                if (err) res.status(400).send('The CSV file seemed fine, but data transformation failed. Check file formatting or contact the developer');
                else {
                  var resultFilePath,
                    path = filePath.split('\\');
                  path[1] = 'result_'+path[1];
                  resultFilePath = path.join('//');
                  fs.writeFile(resultFilePath, output, function (err) {
                    if (err) res.status(400).send('Error writing results file. This is embarassing.');
                    else {
                      res.status(200).download(resultFilePath, 'result.csv');
                      fs.unlinkSync(filePath);
                    }
                  });
                }
              })
            }
          });
        }
      });
    } 
    else res.status(400).send('Wrong file type! Only CSV files are accepted.');
  } else res.status(400).send('No file was sent.');
});

function transformData(array) {
  var chunk,
    result = [],
    i;
  for (i = 0; i < array.length; i++) {
    if (array[i].length === 3) chunk = [];
    chunk.push(array[i]);<commit_msg>~ prettier error handling<commit_after>var app = require('express');
// var path = require('path');
var router = app.Router();
var csv = require('csv');
var fs = require('fs');

/* GET csv handling site. */
router.get('/', function (req, res, next) {
  res.render('csv', {});
});

/* POST csv users listing. */
router.post('/', function (req, res, next) {
  if (req.files && req.files.csvFile) { 
    var filePath = req.files.csvFile.path;
    if (req.files.csvFile.extension === 'csv') {
      var fileName = req.files.csvFile.name;
      
      fs.readFile(filePath, {encoding: 'utf-8'}, function (err, data) {
        if (err) res.status(400).render('error', { message: 'Error processing file', error: err });
        else {
          csv.parse(data, {comment: '//', delimiter: ';', rowDelimiter: '\n'}, function (err, output) {
            if (err) res.status(400).render('error', { message: 'Error parsing CSV file. Check file syntax before trying again', error: err });
            else {
              var transformedData = transformData(output); 
              csv.stringify(transformedData, {}, function (err, output) {
                if (err) res.status(400).render('error', { message: 'The CSV file seemed fine, but data transformation failed. Check file formatting or contact the developer', error: err });
                else {
                  var resultFilePath,
                    path = filePath.split('\\');
                  path[1] = 'result_'+path[1];
                  resultFilePath = path.join('//');
                  fs.writeFile(resultFilePath, output, function (err) {
                    if (err) res.status(400).render('error', { message: 'Error writing results file. This is embarassing.', error: err });
                    else {
                      res.status(200).download(resultFilePath, 'result.csv');
                      fs.unlinkSync(filePath);
                    }
                  });
                }
              })
            }
          });
        }
      });
    } else {
      res.status(400).render('error', { message: 'Wrong file type! Only CSV files are accepted.', error: { status: 400 } });
      fs.unlinkSync(filePath);
    }
  } else res.status(400).render('error', { message: 'No file was sent.', error: { status: 400 } });
});

function transformData(array) {
  var chunk,
    result = [],
    i;
  for (i = 0; i < array.length; i++) {
    if (array[i].length === 3) chunk = [];
    chunk.push(array[i]);
{'fraction_non_alphanumeric': 0.15957903780068727, 'fraction_numerical': 0.01245704467353952, 'mean_word_length': 2.0319010416666665, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1261938', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1290, 'n_words': 350}