Skip to content
Snippets Groups Projects
utils.js 960 B
Newer Older
  aSize = Math.abs(parseInt(aSize, 10));
  const def = [[1, 'octets', 0], [1024, 'ko', 0], [1024*1024, 'Mo', 1], [1024*1024*1024, 'Go', 2], [1024*1024*1024*1024, 'To', 2]];
  for (let i=0; i<def.length; i++) {
    if (aSize<def[i][0]) return (aSize/def[i-1][0]).toFixed(def[i-1][2]) + ' ' + def[i-1][1];
  }
  console.log(aSize);
  aSize = Math.abs(parseInt(aSize, 10));
  const def = [1024*1024, 'Mo', 1];
  return (aSize/def[0]).toFixed(def[2]);

export function csvToJson(csv) {
  let result = [];

  const allLines = csv.split('\n');
  const headers = allLines[0].split(',');
  const [, ...lines] = allLines;

  for (const line of lines) {
    let obj = {};
    const currentLine = line.split(',');

    for (let i = 0; i < headers.length; i++) {
      obj[headers[i]] = currentLine[i];
    }

    result.push(obj);
  }
  return JSON.parse(JSON.stringify(result));
}