diff --git a/ftcsv.lua b/ftcsv.lua index 760456a..1e9d579 100644 --- a/ftcsv.lua +++ b/ftcsv.lua @@ -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() @@ -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 diff --git a/spec/feature_spec.lua b/spec/feature_spec.lua index 9ef5212..70c1bc4 100644 --- a/spec/feature_spec.lua +++ b/spec/feature_spec.lua @@ -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] = {}