Skip to content

Commit

Permalink
renamed noQuotes
Browse files Browse the repository at this point in the history
  • Loading branch information
FourierTransformer committed Feb 20, 2023
1 parent 5be2f78 commit 807ac6c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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})
Expand Down
35 changes: 19 additions & 16 deletions ftcsv.lua
Expand Up @@ -645,15 +645,18 @@ local function delimitField(field)
end
end

local function delimitAndQuoteField(field)
field = tostring(field)
if field:find('"') then
return '"' .. field:gsub('"', '""') .. '"'
elseif field:find('[\n,]') then
return '"' .. field .. '"'
else
return field
local function generateDelimitAndQuoteField(delimiter)
local generatedFunction = function(field)
field = tostring(field)
if field:find('"') then
return '"' .. field:gsub('"', '""') .. '"'
elseif field:find('[\n' .. delimiter .. ']') then
return '"' .. field .. '"'
else
return field
end
end
return generatedFunction
end

local function escapeHeadersForLuaGenerator(headers)
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -716,19 +719,19 @@ 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'
end
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])
Expand Down Expand Up @@ -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
Expand Down
54 changes: 35 additions & 19 deletions spec/feature_spec.lua
Expand Up @@ -61,15 +61,15 @@ describe("csv features", function()
assert.are.same(expected, actual)
end)

it("should handle escaped doublequotes", function()
local expected = {}
expected[1] = {}
expected[1].a = 'A"B""C'
expected[1].b = 'A""B"C'
expected[1].c = 'A"""B""C'
local actual = ftcsv.parse('a;b;c\n"A""B""""C";"A""""B""C";"A""""""B""""C"', ";", {loadFromString=true})
assert.are.same(expected, actual)
end)
it("should handle escaped doublequotes", function()
local expected = {}
expected[1] = {}
expected[1].a = 'A"B""C'
expected[1].b = 'A""B"C'
expected[1].c = 'A"""B""C'
local actual = ftcsv.parse('a;b;c\n"A""B""""C";"A""""B""C";"A""""""B""""C"', ";", {loadFromString=true})
assert.are.same(expected, actual)
end)

it("should handle renaming a field", function()
local expected = {}
Expand Down Expand Up @@ -435,25 +435,41 @@ describe("csv features", function()
it("should handle encoding files (str test)", function()
local expected = '"a","b","c","d"\r\n"1","","foo","""quoted"""\r\n'
output = ftcsv.encode({
{ a = 1, b = '', c = 'foo', d = '"quoted"' };
}, ',')
assert.are.same(expected, output)
{ a = 1, b = '', c = 'foo', d = '"quoted"' };
}, ',')
assert.are.same(expected, output)
end)

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"' };
}, '>')
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'
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 = 'foo', d = '"quoted"' };
}, ',', {noQuotes=true})
assert.are.same(expected, output)
{ a = 1, b = '', c = 'fo,o', d = '"quoted"' };
}, '>', {onlyRequiredQuotes=true})
assert.are.same(expected, output)
end)

it("should handle encoding files without quotes with certain fields to keep (str test)", 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"}})
assert.are.same(expected, output)
{ a = 1, b = '', c = 'foo', d = '"quoted"' };
}, ',', {onlyRequiredQuotes=true, fieldsToKeep={"b", "c"}})
assert.are.same(expected, output)
end)

it("should handle headers attempting to escape", function()
Expand Down
2 changes: 1 addition & 1 deletion spec/parse_encode_spec.lua
Expand Up @@ -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
Expand Down

0 comments on commit 807ac6c

Please sign in to comment.