mirror of
https://github.com/FourierTransformer/ftcsv.git
synced 2024-11-19 19:54:23 +00:00
Rename noQuotes to onlyRequiredQuotes and fix encoding bug (#35)
This commit is contained in:
parent
5be2f78119
commit
6238c5a0e8
@ -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})
|
||||
|
35
ftcsv.lua
35
ftcsv.lua
@ -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)
|
||||
@ -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
|
||||
|
@ -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 = {}
|
||||
@ -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 = '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 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)
|
||||
|
||||
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()
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user