Skip to content

Header fields aren't trimmed when results returned as object #65

@kael-shipman

Description

@kael-shipman

I ran into a bank that (stupidly) produced a CSV whose header fields had spaces around them. Since these fields were not trimmed, the object keys reflected these senseless spaces. For example:

Date, Description, Amount
2022-01-01,"My description",55
2022-05-01,"Another description",23.44

Current State

Using the current code, the above produced the following results:

console.log(CSV.parse(csvStr), { output: "objects" });
[
  {
    "Date": "2022-01-01",
    " Description": "My description",
    " Amount": "55",
  },
  {
    "Date": "2022-05-01",
    " Description": "Another description",
    " Amount": "23.44",
  },
]

Expected Result

If we've asked for objects, we should trim header fields for those objects. However, we should NOT trim any row data, including the first row if we've requested tuples (rather than objects).

Using the above example, our expected return values should be the following:

// Objects
console.log(CSV.parse(csvStr), { output: "objects" });
[
  {
    "Date": "2022-01-01",
    "Description": "My description",
    "Amount": "55",
  },
  {
    "Date": "2022-05-01",
    "Description": "Another description",
    "Amount": "23.44",
  },
]

// Tuples
console.log(CSV.parse(csvStr), { output: "tuples" });
[
  [ "Date", " Description", " Amount" ],
  [ "2022-01-01", "My description", "55" ],
  [ "2022-05-01", "Another description", "23.44" ],
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions