Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 0e07fab

Browse files
Trim column headers by default, and bump major version (#32)
Add the option to trim column headers when reading from the input text, and make it the default. This is a potentially breaking change, so increment the major version as well.
1 parent 310dd82 commit 0e07fab

File tree

6 files changed

+65
-3
lines changed

6 files changed

+65
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ while (reader.Read())
3636

3737
* `FieldSeparator` - the character used as field delimiter in the text file. Default: `,` (i.e., CSV).
3838
* `UseFirstRowAsColumnHeaders` - specifies whether the first row of the text file should be treated as a header row. Default: `true`.
39+
* `TrimColumnHeaders` - specifies whether the column headers, if present, should have whitespace trimmed before being used as a key.
3940

4041
## Exporter
4142

src/DelimitedDataParser/DelimitedDataParser.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<CodeAnalysisRuleSet>..\..\CustomExtendedCorrectnessRules.ruleset</CodeAnalysisRuleSet>
77
<Authors>Enable · enable.com</Authors>
88
<Company>Enable</Company>
9-
<Version>4.2.1</Version>
9+
<Version>5.0.0</Version>
1010
<Description>C# library for parsing and exporting tabular data in delimited format (e.g. CSV).</Description>
1111
<Copyright>Copyright © 2018</Copyright>
1212
<PackageIconUrl>https://github.com/EnableSoftware.png</PackageIconUrl>

src/DelimitedDataParser/DelimitedDataReader.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Data.Common;
66
using System.Globalization;
77
using System.IO;
8+
using System.Linq;
89
using System.Text;
910
using System.Threading;
1011

@@ -20,6 +21,7 @@ internal class DelimitedDataReader : DbDataReader
2021
private readonly Encoding _encoding;
2122
private readonly char _fieldSeparator;
2223
private readonly bool _useFirstRowAsColumnHeaders;
24+
private readonly bool _trimColumnHeaders;
2325
private readonly CancellationToken _cancellationToken;
2426
private readonly char[] _buffer = new char[4096];
2527

@@ -37,12 +39,14 @@ public DelimitedDataReader(
3739
Encoding encoding,
3840
char fieldSeparator,
3941
bool useFirstRowAsColumnHeaders,
42+
bool trimColumnHeaders,
4043
CancellationToken cancellationToken = default(CancellationToken))
4144
{
4245
_textReader = textReader ?? throw new ArgumentNullException(nameof(textReader));
4346
_encoding = encoding ?? throw new ArgumentException(nameof(encoding));
4447
_fieldSeparator = fieldSeparator;
4548
_useFirstRowAsColumnHeaders = useFirstRowAsColumnHeaders;
49+
_trimColumnHeaders = trimColumnHeaders;
4650
_cancellationToken = cancellationToken;
4751
}
4852

@@ -601,7 +605,14 @@ private void EnsureInitialised()
601605
private void GenerateFieldLookup()
602606
{
603607
// Here we assume that the current row is the header row.
604-
_fieldNameLookup = new List<string>(_currentRow).AsReadOnly();
608+
if (_trimColumnHeaders)
609+
{
610+
_fieldNameLookup = _currentRow.Select(o => o?.Trim()).ToList().AsReadOnly();
611+
}
612+
else
613+
{
614+
_fieldNameLookup = new List<string>(_currentRow).AsReadOnly();
615+
}
605616
}
606617

607618
private void GenerateDefaultFieldNameLookup()

src/DelimitedDataParser/Parser.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Parser
2222
private ISet<string> _columnNamesAsText;
2323
private char _fieldSeparator = ',';
2424
private bool _useFirstRowAsColumnHeaders = true;
25+
private bool _trimColumnHeaders = true;
2526

2627
/// <summary>
2728
/// Initializes a new instance of the <see cref="Parser"/> class.
@@ -64,6 +65,23 @@ public virtual bool UseFirstRowAsColumnHeaders
6465
}
6566
}
6667

68+
/// <summary>
69+
/// Gets or sets a value indicating whether the column headers, if present, should have whitespace trimmed before being used as a key
70+
/// The default value is <c>true</c>.
71+
/// </summary>
72+
public virtual bool TrimColumnHeaders
73+
{
74+
get
75+
{
76+
return _trimColumnHeaders;
77+
}
78+
79+
set
80+
{
81+
_trimColumnHeaders = value;
82+
}
83+
}
84+
6785
/// <summary>
6886
/// Clear all "columns as text" settings.
6987
/// </summary>
@@ -183,6 +201,7 @@ public virtual void ClearColumnsAsText()
183201
encoding,
184202
_fieldSeparator,
185203
_useFirstRowAsColumnHeaders,
204+
_trimColumnHeaders,
186205
cancellationToken);
187206
}
188207

@@ -207,6 +226,7 @@ public virtual void ClearColumnsAsText()
207226
streamReader.CurrentEncoding,
208227
_fieldSeparator,
209228
_useFirstRowAsColumnHeaders,
229+
_trimColumnHeaders,
210230
cancellationToken);
211231
}
212232

test/DelimitedDataParser.Test/ExporterTest.Reader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ public void ExportReader_Supports_Large_Dataset()
603603
{
604604
stringReader = new StringReader(expected.ToString());
605605

606-
using (var dataReader = new DelimitedDataReader(stringReader, Encoding.UTF8, ',', true))
606+
using (var dataReader = new DelimitedDataReader(stringReader, Encoding.UTF8, ',', true, false))
607607
{
608608
stringReader = null;
609609

test/DelimitedDataParser.Test/ParserTest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,36 @@ public void Can_Parse_Column_Names_From_First_Row()
5656
Assert.Equal("Field 3", output.Columns[2].ColumnName);
5757
}
5858

59+
[Fact]
60+
public void Will_Trim_Column_Names_By_Default()
61+
{
62+
string input = @"Field 1 , Field 2, Field 3 ";
63+
64+
var parser = new Parser();
65+
var output = parser.Parse(GetTextReader(input));
66+
67+
Assert.Equal(3, output.Columns.Count);
68+
Assert.Equal("Field 1", output.Columns[0].ColumnName);
69+
Assert.Equal("Field 2", output.Columns[1].ColumnName);
70+
Assert.Equal("Field 3", output.Columns[2].ColumnName);
71+
}
72+
73+
[Fact]
74+
public void Will_Not_Trim_Column_Names_If_Prevented()
75+
{
76+
string input = @"Field 1 , Field 2, Field 3 ";
77+
78+
var parser = new Parser();
79+
parser.TrimColumnHeaders = false;
80+
81+
var output = parser.Parse(GetTextReader(input));
82+
83+
Assert.Equal(3, output.Columns.Count);
84+
Assert.Equal("Field 1 ", output.Columns[0].ColumnName);
85+
Assert.Equal(" Field 2", output.Columns[1].ColumnName);
86+
Assert.Equal(" Field 3 ", output.Columns[2].ColumnName);
87+
}
88+
5989
[Fact]
6090
public void Can_Parse_Empty_Column_Names()
6191
{

0 commit comments

Comments
 (0)