Skip to content

Commit

Permalink
csvToQuery() ignores trim setting when reading from file Fixes #339
Browse files Browse the repository at this point in the history
  • Loading branch information
cfsimplicity committed Nov 4, 2023
1 parent 6e279e1 commit 2aa175e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Spreadsheet.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ component accessors="true"{
Throw( type=this.getExceptionType() & ".invalidArgumentCombination", message="Invalid argument 'queryColumnTypes'.", detail="When specifying 'queryColumnTypes' as a struct you must also set the 'firstRowIsHeader' argument to true OR provide 'queryColumnNames'" );
var format = getCsvHelper().getFormat( arguments.delimiter?:"" );
var parsed = csvIsFile?
getCsvHelper().parseFromFile( arguments.filepath, format ):
getCsvHelper().parseFromFile( arguments.filepath, arguments.trim, format ):
getCsvHelper().parseFromString( arguments.csv, arguments.trim, format );
var data = parsed.data;
var maxColumnCount = parsed.maxColumnCount;
Expand Down
4 changes: 2 additions & 2 deletions helpers/csv.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ component extends="base"{
}
}

struct function parseFromFile( required string path, required any format ){
struct function parseFromFile( required string path, required boolean trim, required any format ){
getFileHelper()
.throwErrorIFfileNotExists( arguments.path )
.throwErrorIFnotCsvOrTextFile( arguments.path );
return parseFromString( FileRead( arguments.path ), false, arguments.format );
return parseFromString( FileRead( arguments.path ), arguments.trim, arguments.format );
}

/* Private */
Expand Down
37 changes: 37 additions & 0 deletions test/specs/csvToQuery.cfm
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,43 @@ describe( "csvToQuery", function(){
expect( actual.getColumnNames()[ 1 ] ).toBeWithCase( "1st Name" );
});
describe( "trimming", function(){
it( "will trim the csv string by default", function(){
var csv = crlf & '"Frumpo McNugget",12345' & crlf;
var actual = s.csvToQuery( csv );
expect( actual ).toBe( basicExpectedQuery );
});
it( "will trim the csv file by default", function(){
var csv = crlf & '"Frumpo McNugget",12345' & crlf;
FileWrite( tempCsvPath, csv );
var actual = s.csvToQuery( filepath: tempCsvPath );
expect( actual ).toBe( basicExpectedQuery );
});
it( "can preserve a string's leading/trailing space", function(){
var csv = crlf & '"Frumpo McNugget",12345' & crlf;
var actual = s.csvToQuery( csv: csv, trim: false );
expected = QueryNew( "column1,column2", "", [ [ "", "" ], [ "Frumpo McNugget", "12345" ] ] );
expect( actual ).toBe( expected );
});
it( "can preserve a file's leading/trailing space", function(){
var csv = crlf & '"Frumpo McNugget",12345' & crlf;
FileWrite( tempCsvPath, csv );
var actual = s.csvToQuery( filepath: tempCsvPath, trim: false );
expected = QueryNew( "column1,column2", "", [ [ "", "" ], [ "Frumpo McNugget", "12345" ] ] );
expect( actual ).toBe( expected );
});
afterEach( function(){
if( FileExists( tempCsvPath ) )
FileDelete( tempCsvPath );
});
});
describe( "delimiter handling", function(){
it( "can accept an alternative delimiter", function(){
Expand Down

0 comments on commit 2aa175e

Please sign in to comment.