fixed bug with no header and no newline at end (#15)

This commit is contained in:
Shakil Thakur 2018-05-26 20:44:53 -05:00 committed by GitHub
parent 9ef72b8810
commit ca292adee0
2 changed files with 52 additions and 3 deletions

View File

@ -142,17 +142,16 @@ local function parseString(inputString, inputLength, delimiter, i, headerField,
local assignValue local assignValue
local outResults local outResults
-- outResults[1] = {}
-- the headers haven't been set yet. -- the headers haven't been set yet.
-- aka this is the first run! -- aka this is the first run!
if headerField == nil then if headerField == nil then
-- print("this is for headers")
headerField = {} headerField = {}
assignValue = function() assignValue = function()
headerField[fieldNum] = field headerField[fieldNum] = field
return true return true
end end
else else
-- print("this is for magic")
outResults = {} outResults = {}
outResults[1] = {} outResults[1] = {}
assignValue = function() assignValue = function()
@ -267,11 +266,17 @@ local function parseString(inputString, inputLength, delimiter, i, headerField,
assignValue() assignValue()
end 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) -- 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 -- doing a count gets it to pick up the oddballs
local finalLineCount = 0 local finalLineCount = 0
local lastValue = nil local lastValue = nil
for k, v in pairs(outResults[lineNum]) do for _, v in pairs(outResults[lineNum]) do
finalLineCount = finalLineCount + 1 finalLineCount = finalLineCount + 1
lastValue = v lastValue = v
end end

View File

@ -114,6 +114,50 @@ describe("csv features", function()
assert.are.same(expected, actual) assert.are.same(expected, actual)
end) 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() it("should handle only renaming fields from files without headers", function()
local expected = {} local expected = {}
expected[1] = {} expected[1] = {}