function getCSVRowFromEventObject()

in src/utils/batchSenders.js [324:369]


function getCSVRowFromEventObject(
  event: Object,
  columns: Array<string>,
): string {
  const currentIndex = {};
  const countColumns = {};
  columns.forEach(col => countColumns[col] = (countColumns[col] || 0) + 1);
  return columns.map(column => {
    const path = column.split('.');
    let value: any = event;
    path.forEach(p => value = value != null ? value[p] : null);

    const index = currentIndex[column] || 0;
    currentIndex[column] = index + 1;

    if (countColumns[column] > 1) {
      // multiple occurences, such as multiple emails, we get the i-th element.
      if (Array.isArray(value)) {
        value = value[index];
      } else {
        // if normalizer returns single value, use it for the first value.
        value = index === 0 ? value : null;
      }
    }

    if (value == null) {
      return '';
    }
    // For content_ids, we'll need to stringify it differntly if it's
    // normalized into an array.
    if (column === 'content_ids' && Array.isArray(value)) {
      value = value.map(v => String(v)).join(',');
    } else if (column === 'catalog_auto_population_info') {
      value = JSON.stringify(value);
    } else {
      value = String(value);
    }

    // Escape quotes for CSV.
    if (value.includes('"') || value.includes(',') || value.includes('\n')) {
      return `"${String(value).replace(/\"/g, '""')}"`;
    } else {
      return value;
    }
  }).join(',') + EOL;
}