JSFiddle - React, Tailwind, and code Playground

by Stephen Katulka

HTML

<textarea class="myText" rows="20" cols="80">"this","is",3,"right?",1
"isn't","this",1,"OK?",2
"this \"line\"","contains",2,"quotes",5</textarea>
<input type="button" class="action" value="OK"/>
<div class="result"></div>

CSS

div.result {
    width:400px;
    border:1px solid black;
}

JavaScript

var pivot = function(data){
    var result = [];
    for (var i = 0; i < data.length; i++){
        for (var j=0; j < data[i].length; j++){
            if (i === 0){
                result[j] = [];
            }
            result[j][i] = data[i][j];
        }
    }
    return result;
};

var getData = function() {
    var csvString = $(".myText").val();
    var csvLines = csvString.split(/\n?$/m);
    
    var dataTable = [];
    
    for (var i = 0; i < csvLines.length; i++){
        var values;
        eval("values = [" + csvLines[i] + "]");
        dataTable[i] = values;
    }
    
    return pivot(dataTable);
};

$(".action").on("click",function(){
    $(".result").html("<pre>" + DumpObjectIndented(getData(), "  ") + "</pre>");      
});

var DumpObjectIndented = function(obj, indent)
{
  var result = "";
  if (indent == null) indent = "";

  for (var property in obj)
  {
    var value = obj[property];
    if (typeof value == 'string')
      value = "'" + value + "'";
    else if (typeof value == 'object')
    {
//      if (value instanceof Array)
//      {
//        // Just let JS convert the Array to a string!
//        value = "[ " + value + " ]";
//      }
//      else
//      {
        // Recursive dump
        // (replace "  " by "\t" or something else if you prefer)
        var od = DumpObjectIndented(value, indent + "  ");
        // If you like { on the same line as the key
        //value = "{\n" + od + "\n" + indent + "}";
        // If you prefer { and } to be aligned
        value = "\n" + indent + "{\n" + od + "\n" + indent + "}";
//      }
    }
    result += indent + "'" + property + "' : " + value + ",\n";
  }
  return result.replace(/,\n$/, "");
};