aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/static/csv.js
blob: 310bdca810ae1261d8f6b3df7ed06f6539d202cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * One CSV cell, quoted when it has to be, and never a formula.
 *
 * A spreadsheet runs a cell that starts with `=`, `+`, `-` or `@` (or a tab or a
 * carriage return in front of one) as a formula. The audit export carries text
 * a member chose — a refused blob's kind, a file name — so such a cell is given
 * a leading apostrophe, which spreadsheets read as "this is text", and which is
 * what other exports do.
 */
export function csvCell(value) {
  let s = value == null ? '' : String(value);
  if (/^[=+\-@\t\r]/.test(s)) s = `'${s}`;
  return /[",\n\r]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
}