Skip to content

Commit

Permalink
fixed bug with no header and no newline at end (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
FourierTransformer authored May 27, 2018
1 parent ee38417 commit 77e4c25
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
11 changes: 8 additions & 3 deletions ftcsv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,16 @@ local function parseString(inputString, inputLength, delimiter, i, headerField,

local assignValue
local outResults
-- outResults[1] = {}
-- the headers haven't been set yet.
-- aka this is the first run!
if headerField == nil then
-- print("this is for headers")
headerField = {}
assignValue = function()
headerField[fieldNum] = field
return true
end
else
-- print("this is for magic")
outResults = {}
outResults[1] = {}
assignValue = function()
Expand Down Expand Up @@ -267,11 +266,17 @@ local function parseString(inputString, inputLength, delimiter, i, headerField,
assignValue()
end

-- if there's no newline, the parser doesn't return headers correctly...
-- ex: a,b,c
if outResults == nil then
return headerField, i
end

-- clean up last line if it's weird (this happens when there is a CRLF newline at end of file)
-- doing a count gets it to pick up the oddballs
local finalLineCount = 0
local lastValue = nil
for k, v in pairs(outResults[lineNum]) do
for _, v in pairs(outResults[lineNum]) do
finalLineCount = finalLineCount + 1
lastValue = v
end
Expand Down
44 changes: 44 additions & 0 deletions spec/feature_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,50 @@ describe("csv features", function()
assert.are.same(expected, actual)
end)

it("should handle files without (headers and newlines)", function()
local expected = {}
expected[1] = {}
expected[1][1] = "apple"
expected[1][2] = "banana"
expected[1][3] = "carrot"
local options = {loadFromString=true, headers=false}
local actual = ftcsv.parse("apple>banana>carrot", ">", options)
assert.are.same(expected, actual)
end)

it("should handle files without (headers and newlines) w/newline at end", function()
local expected = {}
expected[1] = {}
expected[1][1] = "apple"
expected[1][2] = "banana"
expected[1][3] = "carrot"
local options = {loadFromString=true, headers=false}
local actual = ftcsv.parse("apple>banana>carrot\n", ">", options)
assert.are.same(expected, actual)
end)

it("should handle files without (headers and newlines) w/crlf", function()
local expected = {}
expected[1] = {}
expected[1][1] = "apple"
expected[1][2] = "banana"
expected[1][3] = "carrot"
local options = {loadFromString=true, headers=false}
local actual = ftcsv.parse("apple>banana>carrot\r\n", ">", options)
assert.are.same(expected, actual)
end)

it("should handle files without (headers and newlines) w/cr", function()
local expected = {}
expected[1] = {}
expected[1][1] = "apple"
expected[1][2] = "banana"
expected[1][3] = "carrot"
local options = {loadFromString=true, headers=false}
local actual = ftcsv.parse("apple>banana>carrot\r", ">", options)
assert.are.same(expected, actual)
end)

it("should handle only renaming fields from files without headers", function()
local expected = {}
expected[1] = {}
Expand Down

0 comments on commit 77e4c25

Please sign in to comment.