Skip to content

Commit

Permalink
Merge pull request #7 from Oozlum/return_headers
Browse files Browse the repository at this point in the history
ftcsv.parse also returns table of column headers
  • Loading branch information
FourierTransformer authored Nov 6, 2017
2 parents 1a40dd1 + 62b1c1b commit 29675f4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ftcsv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/feature_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
Expand Down

0 comments on commit 29675f4

Please sign in to comment.