Skip to content

Commit

Permalink
Allow passing a null string to TryParse
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
kzu committed Jan 2, 2022
1 parent 9a7ae9e commit 12ba476
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ISBN/ISBN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ static ISBN()
/// <param name="isbn">The ISBN to parse.</param>
/// <param name="result">The parsed result, if valid.</param>
/// <returns><see langword="false"/> if the <paramref name="isbn"/> was not a valid ISBN. <see langword="true"/> otherwise.</returns>
public static bool TryParse(string isbn, [NotNullWhen(true)] out ISBN? result)
public static bool TryParse(string? isbn, [NotNullWhen(true)] out ISBN? result)
{
result = default;
if (isbn == null)
return false;

isbn = new string(isbn.Where(c => c != ' ' && c != '-').ToArray());
if (isbn.Length == 10)
isbn += "978";
Expand Down
3 changes: 3 additions & 0 deletions src/Tests/ISBNTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ public void ParseIsbn(string isbn)
Assert.True(false, "Failed to parse " + isbn);
}
}

[Fact]
public void ParseNullIsbnReturnsFalse() => Assert.False(ISBN.TryParse(null, out var _));
}

0 comments on commit 12ba476

Please sign in to comment.