Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ file:close()
--> ]]
```

- `headers`

If set to `false` it only outputs the data without the headers. This is useful
for streaming CSV in chunks, by outputting the header first (e.g., using an
empty table), and then one or more rows of data in a loop.

To ensure a correct data layout, this option must be used with `fieldsToKeep`
set to a constant ordering throughout the loop.

## Error Handling
ftcsv returns a litany of errors when passed a bad csv file or incorrect parameters. You can find a more detailed explanation of the more cryptic errors in [ERRORS.md](ERRORS.md)

Expand Down
15 changes: 14 additions & 1 deletion ftcsv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,21 @@ function ftcsv.encode(inputTable, delimiter, options)
local delimiter, options = determineArgumentOrder(delimiter, options)
local output, headers = initializeGenerator(inputTable, delimiter, options)

local offset
if (options or {}).headers == false then
if #(options.fieldsToKeep or {}) == 0 then
error(
"`fieldsToKeep` must be specified if generating a CSV " ..
"without a header."
)
end
output = {}
offset = 0
else offset = 1 end

for i, line in csvLineGenerator(inputTable, delimiter, headers, options) do
output[i+1] = line
-- Overwrite header with first line if headers is false
output[i + offset] = line
end

-- combine and return final string
Expand Down
5 changes: 5 additions & 0 deletions spec/csvs/no_header.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"E350",3000.00,1997
"Venture ""Extended Edition""",4900.00,1999
"Grand Cherokee",4799.00,1996
"Venture ""Extended Edition, Very Large""",5000.00,1999
"Venture ""Extended Edition""",4900.00,
17 changes: 17 additions & 0 deletions spec/parse_encode_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,20 @@ describe("csv encode with missing keys", function()
assert.are.same(jsonDecode, reEncoded)
end)
end)


describe("csv encode with selective omissions", function()
local jsonFile = loadFile("spec/json/correctness.json")
local jsonDecode = cjson.decode(jsonFile)

it("should output only data in a specific order", function()
local reEncoded = ftcsv.parse(ftcsv.encode(
jsonDecode, ",", {
fieldsToKeep = {"Model", "Price", "Year"},
headers = false,
}
), ",", {loadFromString=true, headers=false})
local exp = ftcsv.parse("spec/csvs/no_header.csv", {headers = false})
assert.are.same(exp, reEncoded)
end)
end)