File size: 2,672 Bytes
48183e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import fs from "node:fs"

// Minimal RFC4180-ish CSV parser: handles quoted fields, embedded commas,
// embedded quotes ("" escaping), and embedded newlines inside quotes.
// Strips a leading UTF-8 BOM. Good enough for the well-formed spreadsheet
// exports this project produces -- not a general-purpose CSV library.
export function parseCsv(text) {
  const body = text.charCodeAt(0) === 0xfeff ? text.slice(1) : text
  const rows = []
  let row = []
  let field = ""
  let inQuotes = false

  for (let i = 0; i < body.length; i++) {
    const c = body[i]
    if (inQuotes) {
      if (c === '"') {
        if (body[i + 1] === '"') {
          field += '"'
          i++
        } else {
          inQuotes = false
        }
      } else {
        field += c
      }
      continue
    }
    if (c === '"') {
      inQuotes = true
    } else if (c === ",") {
      row.push(field)
      field = ""
    } else if (c === "\n" || c === "\r") {
      if (c === "\r" && body[i + 1] === "\n") i++
      row.push(field)
      rows.push(row)
      row = []
      field = ""
    } else {
      field += c
    }
  }
  if (field.length > 0 || row.length > 0) {
    row.push(field)
    rows.push(row)
  }
  return rows.filter((r) => !(r.length === 1 && r[0] === ""))
}

function csvEscape(value) {
  const s = value == null ? "" : String(value)
  if (/[",\n\r]/.test(s)) {
    return `"${s.replace(/"/g, '""')}"`
  }
  return s
}

// `leadingNotes` (optional): human-readable note rows written before the
// header, one full-width row per note (note text in the first column,
// blanks elsewhere) -- the same convention the data/ SAMPLE files used for
// "Illustrative only" banners. Purely for a human reader; a file written
// this way should not be re-parsed as CSV data by another script, since
// readCsvObjects would treat the first note row as the header.
export function writeCsv(path, header, rows, { leadingNotes = [] } = {}) {
  const lines = []
  for (const note of leadingNotes) {
    lines.push([note, ...Array(header.length - 1).fill("")].map(csvEscape).join(","))
  }
  lines.push(header.map(csvEscape).join(","))
  for (const row of rows) {
    lines.push(header.map((col) => csvEscape(row[col])).join(","))
  }
  fs.writeFileSync(path, "" + lines.join("\r\n") + "\r\n", "utf8")
}

export function readCsvObjects(path) {
  const text = fs.readFileSync(path, "utf8")
  const rows = parseCsv(text)
  if (rows.length === 0) return { header: [], records: [] }
  const [header, ...rest] = rows
  const records = rest.map((r) => {
    const obj = {}
    header.forEach((col, i) => {
      obj[col] = r[i] ?? ""
    })
    return obj
  })
  return { header, records }
}