Skip to content

Commit c5ba66a

Browse files
committed
Adding Prefer enum to tweak behavior. Issue #20
1 parent 9596b7c commit c5ba66a

File tree

4 files changed

+65
-46
lines changed

4 files changed

+65
-46
lines changed

NameParser/NameParser/NameParser.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
<PropertyGroup>
33
<TargetFrameworks>netstandard20;net45</TargetFrameworks>
44
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
5-
<AssemblyVersion>1.4.0.0</AssemblyVersion>
6-
<FileVersion>1.4.0.0</FileVersion>
5+
<AssemblyVersion>1.5.0.0</AssemblyVersion>
6+
<FileVersion>1.5.0.0</FileVersion>
77
<PackageLicenseUrl>https://licenses.nuget.org/LGPL-2.1-or-later</PackageLicenseUrl>
88
<PackageProjectUrl>https://github.com/aeshirey/NameParserSharp</PackageProjectUrl>
99
<Description>Parses full name into first name, middle name, last name, nickname, title, and suffix fields</Description>
10-
<Version>1.4.0</Version>
10+
<Version>1.5.0</Version>
1111
<PackageId>NameParserSharp</PackageId>
1212
<Authors>Adam Shirey</Authors>
1313
<PackageTags>human name parser parsing first middle last nickname</PackageTags>

NameParser/NameParser/Parser.cs

+27-37
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,17 @@ private set
4848
}
4949
}
5050

51-
public string Title
52-
{
53-
get { return string.Join(" ", _TitleList); }
54-
}
51+
public string Title => string.Join(" ", _TitleList);
5552

56-
public string First
57-
{
58-
get { return string.Join(" ", _FirstList); }
59-
}
53+
public string First => string.Join(" ", _FirstList);
6054

61-
public string Middle
62-
{
63-
get { return string.Join(" ", _MiddleList); }
64-
}
55+
public string Middle => string.Join(" ", _MiddleList);
6556

66-
public string Last
67-
{
68-
get { return string.Join(" ", _LastList); }
69-
}
57+
public string Last => string.Join(" ", _LastList);
7058

71-
public string Suffix
72-
{
73-
get { return string.Join(" ", _SuffixList); }
74-
}
59+
public string Suffix => string.Join(" ", _SuffixList);
7560

76-
public string Nickname
77-
{
78-
get { return string.Join(" ", _NicknameList); }
79-
}
61+
public string Nickname => string.Join(" ", _NicknameList);
8062

8163
/// <summary>
8264
/// If <see cref="ParseMultipleNames"/> is true and the input contains "&" or "and", the additional
@@ -86,17 +68,8 @@ public string Nickname
8668
/// </summary>
8769
public HumanName AdditionalName { get; private set; }
8870

89-
//public string LastBase { get; private set; }
90-
public string LastBase
91-
{
92-
get { return string.Join(" ", _LastBaseList); }
93-
}
94-
95-
//public string LastPrefixes { get; private set; }
96-
public string LastPrefixes
97-
{
98-
get { return string.Join(" ", _LastPrefixList); }
99-
}
71+
public string LastBase => string.Join(" ", _LastBaseList);
72+
public string LastPrefixes => string.Join(" ", _LastPrefixList);
10073
#endregion
10174

10275
private string _FullName, _OriginalName;
@@ -109,14 +82,17 @@ public string LastPrefixes
10982
private IList<string> _NicknameList;
11083
private IList<string> _LastBaseList;
11184
private IList<string> _LastPrefixList;
85+
private Prefer prefs;
11286

113-
public HumanName(string fullName)
87+
public HumanName(string fullName, Prefer prefs = Prefer.Default)
11488
{
11589
if (fullName == null)
11690
{
11791
throw new ArgumentNullException("fullName");
11892
}
11993

94+
this.prefs = prefs;
95+
12096
FullName = fullName;
12197
}
12298

@@ -300,7 +276,21 @@ private void PostProcessLastname()
300276
prefixCount++;
301277
}
302278

303-
_LastPrefixList = words.Take(prefixCount).ToList();
279+
if (this.prefs.HasFlag(Prefer.FirstOverPrefix)
280+
&& this._FirstList.Count == 0
281+
&& prefixCount == 1
282+
&& words.Count > 1)
283+
{
284+
_FirstList = words.Take(1).ToList();
285+
286+
_LastList = words.Skip(1).ToList();
287+
}
288+
else
289+
{
290+
291+
_LastPrefixList = words.Take(prefixCount).ToList();
292+
}
293+
304294
_LastBaseList = words.Skip(prefixCount).ToList();
305295
}
306296

NameParser/NameParser/Prefer.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace NameParser
6+
{
7+
[Flags]
8+
public enum Prefer
9+
{
10+
Default = 0,
11+
12+
/// <summary>
13+
/// For Issue #20, when the parser detects a Title and a Last with prefixes (eg, "Mr. Del Richards"),
14+
/// convert the prefix to a first name.
15+
///
16+
/// This can cause incorrect flipping of prefix to first (eg, "Mr. Van Rossum"), so you should use
17+
/// this flag only when you know your data has a first name.
18+
/// </summary>
19+
FirstOverPrefix = 1,
20+
}
21+
}

NameParser/NameParserTest/NameParserTests.cs

+14-6
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,22 @@ public void AddToLists()
367367
/// <summary>
368368
/// https://github.com/aeshirey/NameParserSharp/issues/20
369369
/// </summary>
370-
//[TestMethod]
370+
[TestMethod]
371371
public void FirstNameIsPrefix()
372372
{
373-
var parsed = new HumanName("Mr. Del Richards");
374-
Assert.AreEqual(parsed.Title, "Mr.");
375-
Assert.AreEqual(parsed.First, "Del");
376-
Assert.AreEqual(parsed.Last, "Richards");
377-
Assert.AreEqual(parsed.LastPrefixes, "");
373+
// Default behavior
374+
var parsed_prefix = new HumanName("Mr. Del Richards");
375+
Assert.AreEqual(parsed_prefix.Title, "Mr.");
376+
Assert.AreEqual(parsed_prefix.First, "");
377+
Assert.AreEqual(parsed_prefix.Last, "Del Richards");
378+
Assert.AreEqual(parsed_prefix.LastPrefixes, "Del");
379+
380+
// A single prefix should be treated as a first name when no first exists
381+
var parsed_first = new HumanName("Mr. Del Richards", Prefer.FirstOverPrefix);
382+
Assert.AreEqual(parsed_first.Title, "Mr.");
383+
Assert.AreEqual(parsed_first.First, "Del");
384+
Assert.AreEqual(parsed_first.Last, "Richards");
385+
Assert.AreEqual(parsed_first.LastPrefixes, "");
378386
}
379387
}
380388
}

0 commit comments

Comments
 (0)