Rename noQuotes to onlyRequiredQuotes and fix encoding bug (#35)

This commit is contained in:
FourierTransformer 2023-02-20 11:00:39 -06:00 committed by GitHub
parent 5be2f78119
commit 6238c5a0e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 38 deletions

View File

@ -143,9 +143,9 @@ file:close()
local output = ftcsv.encode(everyUser, ",", {fieldsToKeep={"Name", "Phone", "City"}})
```
- `noQuotes`
- `onlyRequiredQuotes`
if `noQuotes` is set to `true`, the output will not include quotes around fields.
if `onlyRequiredQuotes` is set to `true`, the output will only include quotes around fields that are quotes, have newlines, or contain the delimter.
```lua
local output = ftcsv.encode(everyUser, ",", {noQuotes=true})

View File

@ -645,16 +645,19 @@ local function delimitField(field)
end
end
local function delimitAndQuoteField(field)
local function generateDelimitAndQuoteField(delimiter)
local generatedFunction = function(field)
field = tostring(field)
if field:find('"') then
return '"' .. field:gsub('"', '""') .. '"'
elseif field:find('[\n,]') then
elseif field:find('[\n' .. delimiter .. ']') then
return '"' .. field .. '"'
else
return field
end
end
return generatedFunction
end
local function escapeHeadersForLuaGenerator(headers)
local escapedHeaders = {}
@ -681,7 +684,7 @@ local function csvLineGenerator(inputTable, delimiter, headers, options)
delimiter .. [["' .. args.delimitField(args.t[i]["]]) ..
[["]) .. '"\r\n']]
if options and options.noQuotes == true then
if options and options.onlyRequiredQuotes == true then
outputFunc = [[
local args, i = ...
i = i + 1;
@ -696,8 +699,8 @@ local function csvLineGenerator(inputTable, delimiter, headers, options)
arguments.t = inputTable
-- we want to use the same delimitField throughout,
-- so we're just going to pass it in
if options and options.noQuotes == true then
arguments.delimitField = delimitAndQuoteField
if options and options.onlyRequiredQuotes == true then
arguments.delimitField = generateDelimitAndQuoteField(delimiter)
else
arguments.delimitField = delimitField
end
@ -716,7 +719,7 @@ end
local function initializeOutputWithEscapedHeaders(escapedHeaders, delimiter, options)
local output = {}
if options and options.noQuotes == true then
if options and options.onlyRequiredQuotes == true then
output[1] = table.concat(escapedHeaders, delimiter) .. '\r\n'
else
output[1] = '"' .. table.concat(escapedHeaders, '"' .. delimiter .. '"') .. '"\r\n'
@ -724,11 +727,11 @@ local function initializeOutputWithEscapedHeaders(escapedHeaders, delimiter, opt
return output
end
local function escapeHeadersForOutput(headers, options)
local function escapeHeadersForOutput(headers, delimiter, options)
local escapedHeaders = {}
local delimitField = delimitField
if options and options.noQuotes == true then
delimitField = delimitAndQuoteField
if options and options.onlyRequiredQuotes == true then
delimitField = generateDelimitAndQuoteField(delimiter)
end
for i = 1, #headers do
escapedHeaders[i] = delimitField(headers[i])
@ -771,7 +774,7 @@ local function initializeGenerator(inputTable, delimiter, options)
end
validateHeaders(headers, inputTable)
local escapedHeaders = escapeHeadersForOutput(headers, options)
local escapedHeaders = escapeHeadersForOutput(headers, delimiter, options)
local output = initializeOutputWithEscapedHeaders(escapedHeaders, delimiter, options)
return output, headers
end

View File

@ -440,11 +440,27 @@ describe("csv features", function()
assert.are.same(expected, output)
end)
it("should handle encoding files without quotes (str test)", function()
local expected = 'a,b,c,d\r\n1,,foo,"""quoted"""\r\n'
it("should handle encoding files (str test) with other delimiter", function()
local expected = '"a">"b">"c">"d"\r\n"1">"">"foo">"""quoted"""\r\n'
output = ftcsv.encode({
{ a = 1, b = '', c = 'foo', d = '"quoted"' };
}, ',', {noQuotes=true})
}, '>')
assert.are.same(expected, output)
end)
it("should handle encoding files without quotes (str test)", function()
local expected = 'a,b,c,d\r\n1,,"fo,o","""quoted"""\r\n'
output = ftcsv.encode({
{ a = 1, b = '', c = 'fo,o', d = '"quoted"' };
}, ',', {onlyRequiredQuotes=true})
assert.are.same(expected, output)
end)
it("should handle encoding files without quotes with other delimiter (str test)", function()
local expected = 'a>b>c>d\r\n1>>fo,o>"""quoted"""\r\n'
output = ftcsv.encode({
{ a = 1, b = '', c = 'fo,o', d = '"quoted"' };
}, '>', {onlyRequiredQuotes=true})
assert.are.same(expected, output)
end)
@ -452,7 +468,7 @@ describe("csv features", function()
local expected = "b,c\r\n,foo\r\n"
output = ftcsv.encode({
{ a = 1, b = '', c = 'foo', d = '"quoted"' };
}, ',', {noQuotes=true, fieldsToKeep={"b", "c"}})
}, ',', {onlyRequiredQuotes=true, fieldsToKeep={"b", "c"}})
assert.are.same(expected, output)
end)

View File

@ -94,7 +94,7 @@ describe("csv encode without quotes", function()
local jsonFile = loadFile("spec/json/" .. value .. ".json")
local jsonDecode = cjson.decode(jsonFile)
-- local parse = staecsv:ftcsv(contents, ",")
local reEncodedNoQuotes = ftcsv.parse(ftcsv.encode(jsonDecode, ",", {noQuotes=true}), ",", {loadFromString=true})
local reEncodedNoQuotes = ftcsv.parse(ftcsv.encode(jsonDecode, ",", {onlyRequiredQuotes=true}), ",", {loadFromString=true})
-- local f = csv.openstring(contents, {separator=",", header=true})
-- local parse = {}
-- for fields in f:lines() do