Merge pull request #7 from Oozlum/return_headers

ftcsv.parse also returns table of column headers
This commit is contained in:
Shakil Thakur 2017-11-05 22:50:15 -06:00 committed by GitHub
commit 29675f4d61
3 changed files with 10 additions and 3 deletions

View File

@ -18,12 +18,12 @@ luarocks install ftcsv
## Parsing
### `ftcsv.parse(fileName, delimiter [, options])`
ftcsv will load the entire csv file into memory, then parse it in one go, returning a lua table with the parsed data. It has only two required parameters - a file name and delimiter (limited to one character). A few optional parameters can be passed in via a table (examples below).
ftcsv will load the entire csv file into memory, then parse it in one go, returning a lua table with the parsed data and a lua table containing the column headers. It has only two required parameters - a file name and delimiter (limited to one character). A few optional parameters can be passed in via a table (examples below).
Just loading a csv file:
```lua
local ftcsv = require('ftcsv')
local zipcodes = ftcsv.parse("free-zipcode-database.csv", ",")
local zipcodes, headers = ftcsv.parse("free-zipcode-database.csv", ",")
```
### Options

View File

@ -390,7 +390,7 @@ function ftcsv.parse(inputFile, delimiter, options)
end
local output = parseString(inputString, inputLength, delimiter, i, headerField, fieldsToKeep)
return output
return output, headerField
end
-- a function that delimits " to "", used by the writer

View File

@ -42,6 +42,13 @@ describe("csv features", function()
assert.are.same(expected, actual)
end)
it("should return a table with column headers", function()
local expected = { 'd', 'e', 'f' }
local options = {loadFromString=true, rename={["a"] = "d", ["b"] = "e", ["c"] = "f"}}
local _, actual = ftcsv.parse("a,b,c\r\napple,banana,carrot", ",", options)
assert.are.same(expected, actual)
end)
it("should handle renaming multiple fields to the same out value", function()
local expected = {}
expected[1] = {}