Skip to content
Snippets Groups Projects
utils.js 1 KiB
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++) {
Florent Lavelle's avatar
Florent Lavelle committed
    if (aSize<def[i][0]) {
      return (aSize/def[i-1][0]).toFixed(def[i-1][2]) + ' ' + def[i-1][1];
    }
  aSize = Math.abs(parseInt(aSize, 10));
  const def = [1024*1024, 'Mo', 1];
  return (aSize/def[0]).toFixed(def[2]);
Florent Lavelle's avatar
Florent Lavelle committed
export function csvToJson(csv, delimiter) {
Florent Lavelle's avatar
Florent Lavelle committed
  const result = [];

  const allLines = csv.split('\n');
Florent Lavelle's avatar
Florent Lavelle committed
  const headers = allLines[0].split(delimiter).map(el => {
    return el.replace('\r', '');
  });
  const [, ...lines] = allLines;

  for (const line of lines) {
Florent Lavelle's avatar
Florent Lavelle committed
    const obj = {};
Florent Lavelle's avatar
Florent Lavelle committed
    const currentLine = line.split(delimiter);

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

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