Skip to content

Commit

Permalink
returned headers should now be correct, BOM fix for headerless files …
Browse files Browse the repository at this point in the history
…and added dynamic_features_spec (#19)
  • Loading branch information
FourierTransformer authored May 30, 2018
1 parent 2dff1f2 commit 8bd8fbe
Show file tree
Hide file tree
Showing 2 changed files with 484 additions and 2 deletions.
23 changes: 21 additions & 2 deletions ftcsv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,24 @@ local function parseString(inputString, inputLength, delimiter, i, headerField,
return outResults
end

-- determine the real headers as opposed to the header mapping
local function determineRealHeaders(headerField, fieldsToKeep)
local realHeaders = {}
local headerSet = {}
for i = 1, #headerField do
if not headerSet[headerField[i]] then
if fieldsToKeep ~= nil and fieldsToKeep[headerField[i]] then
table.insert(realHeaders, headerField[i])
headerSet[headerField[i]] = true
elseif fieldsToKeep == nil then
table.insert(realHeaders, headerField[i])
headerSet[headerField[i]] = true
end
end
end
return realHeaders
end

-- runs the show!
function ftcsv.parse(inputFile, delimiter, options)
-- delimiter MUST be one character
Expand Down Expand Up @@ -373,7 +391,7 @@ function ftcsv.parse(inputFile, delimiter, options)

-- for files where there aren't headers!
if header == false then
i = 1
i = startLine
for j = 1, #headerField do
headerField[j] = j
end
Expand Down Expand Up @@ -404,7 +422,8 @@ function ftcsv.parse(inputFile, delimiter, options)
end

local output = parseString(inputString, inputLength, delimiter, i, headerField, fieldsToKeep)
return output, headerField
local realHeaders = determineRealHeaders(headerField, fieldsToKeep)
return output, realHeaders
end

-- a function that delimits " to "", used by the writer
Expand Down
Loading

0 comments on commit 8bd8fbe

Please sign in to comment.