Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backported header and BOM fix from parseLineIterator and added dynami… #19

Merged
merged 1 commit into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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