mirror of
https://github.com/FourierTransformer/ftcsv.git
synced 2024-11-19 19:54:23 +00:00
fixed bug with no header and no newline at end (#15)
This commit is contained in:
parent
9ef72b8810
commit
ca292adee0
11
ftcsv.lua
11
ftcsv.lua
@ -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
|
||||||
|
@ -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] = {}
|
||||||
|
Loading…
Reference in New Issue
Block a user