Lowercase the csv options
This commit is contained in:
@@ -50,8 +50,8 @@ end
|
|||||||
|
|
||||||
--- @class ParserOptions
|
--- @class ParserOptions
|
||||||
--- @field delimiter string? The field delimiter (default: ",").
|
--- @field delimiter string? The field delimiter (default: ",").
|
||||||
--- @field hasHeaders boolean? If true, first non-comment row is treated as headers (default: false).
|
--- @field hasheader boolean? If true, first non-comment row is treated as headers (default: false).
|
||||||
--- @field hasComments boolean? If true, lines starting with '#' are skipped (default: false).
|
--- @field hascomments boolean? If true, lines starting with '#' are skipped (default: false).
|
||||||
|
|
||||||
--- Parses CSV text into rows and fields using a minimal RFC 4180 state machine.
|
--- Parses CSV text into rows and fields using a minimal RFC 4180 state machine.
|
||||||
---
|
---
|
||||||
@@ -74,8 +74,8 @@ end
|
|||||||
function fromCSV(csv, options)
|
function fromCSV(csv, options)
|
||||||
if options == nil then options = {} end
|
if options == nil then options = {} end
|
||||||
local delimiter = options.delimiter or ","
|
local delimiter = options.delimiter or ","
|
||||||
local hasHeaders = options.hasHeaders or false
|
local hasheader = options.hasheader or false
|
||||||
local hasComments = options.hasComments or false
|
local hascomments = options.hascomments or false
|
||||||
|
|
||||||
local allRows = {}
|
local allRows = {}
|
||||||
local fields = {}
|
local fields = {}
|
||||||
@@ -104,7 +104,7 @@ function fromCSV(csv, options)
|
|||||||
table.insert(fields, table.concat(field))
|
table.insert(fields, table.concat(field))
|
||||||
field = {}
|
field = {}
|
||||||
local shouldAdd = true
|
local shouldAdd = true
|
||||||
if hasComments and #fields > 0 then
|
if hascomments and #fields > 0 then
|
||||||
local firstField = fields[1]
|
local firstField = fields[1]
|
||||||
local trimmed = string.gsub(firstField, "^%s*(.-)%s*$", "%1")
|
local trimmed = string.gsub(firstField, "^%s*(.-)%s*$", "%1")
|
||||||
if string.sub(trimmed, 1, 1) == "#" then shouldAdd = false end
|
if string.sub(trimmed, 1, 1) == "#" then shouldAdd = false end
|
||||||
@@ -142,7 +142,7 @@ function fromCSV(csv, options)
|
|||||||
table.insert(fields, table.concat(field))
|
table.insert(fields, table.concat(field))
|
||||||
field = {}
|
field = {}
|
||||||
local shouldAdd = true
|
local shouldAdd = true
|
||||||
if hasComments and #fields > 0 then
|
if hascomments and #fields > 0 then
|
||||||
local firstField = fields[1]
|
local firstField = fields[1]
|
||||||
local trimmed = string.gsub(firstField, "^%s*(.-)%s*$", "%1")
|
local trimmed = string.gsub(firstField, "^%s*(.-)%s*$", "%1")
|
||||||
if string.sub(trimmed, 1, 1) == "#" then shouldAdd = false end
|
if string.sub(trimmed, 1, 1) == "#" then shouldAdd = false end
|
||||||
@@ -165,7 +165,7 @@ function fromCSV(csv, options)
|
|||||||
if #field > 0 or #fields > 0 then
|
if #field > 0 or #fields > 0 then
|
||||||
table.insert(fields, table.concat(field))
|
table.insert(fields, table.concat(field))
|
||||||
local shouldAdd = true
|
local shouldAdd = true
|
||||||
if hasComments and #fields > 0 then
|
if hascomments and #fields > 0 then
|
||||||
local firstField = fields[1]
|
local firstField = fields[1]
|
||||||
local trimmed = string.gsub(firstField, "^%s*(.-)%s*$", "%1")
|
local trimmed = string.gsub(firstField, "^%s*(.-)%s*$", "%1")
|
||||||
if string.sub(trimmed, 1, 1) == "#" then shouldAdd = false end
|
if string.sub(trimmed, 1, 1) == "#" then shouldAdd = false end
|
||||||
@@ -173,12 +173,12 @@ function fromCSV(csv, options)
|
|||||||
if shouldAdd then table.insert(allRows, fields) end
|
if shouldAdd then table.insert(allRows, fields) end
|
||||||
end
|
end
|
||||||
|
|
||||||
if hasHeaders and #allRows > 0 then
|
if hasheader and #allRows > 0 then
|
||||||
local headers = allRows[1]
|
local headers = allRows[1]
|
||||||
local rows = {}
|
local rows = {}
|
||||||
for i = 2, #allRows do
|
for ii = 2, #allRows do
|
||||||
local row = {}
|
local row = {}
|
||||||
local dataRow = allRows[i]
|
local dataRow = allRows[ii]
|
||||||
for j = 1, #dataRow do
|
for j = 1, #dataRow do
|
||||||
row[j] = dataRow[j]
|
row[j] = dataRow[j]
|
||||||
if headers[j] ~= nil and headers[j] ~= "" then row[headers[j]] = dataRow[j] end
|
if headers[j] ~= nil and headers[j] ~= "" then row[headers[j]] = dataRow[j] end
|
||||||
|
|||||||
Reference in New Issue
Block a user