Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Pathy.Specs/ChainablePathSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,36 @@ public void Checking_for_an_extension_requires_a_valid_extension(string extensio
act.Should().Throw<ArgumentException>("*null*empty*");
}

[Theory]
[InlineData("SomeFile.txt", true)]
[InlineData("somefile.txt", true)]
[InlineData("SOMEFILE.TXT", true)]
[InlineData("SomeFile", false)]
[InlineData("OtherFile.txt", false)]
public void Can_check_for_a_name(string name, bool shouldMatch)
{
// Act
var path = ChainablePath.Temp / "SomeFile.txt";

// Assert
path.HasName(name).Should().Be(shouldMatch);
}

[Theory]
[InlineData(null)]
[InlineData("")]
public void Checking_for_a_name_requires_a_valid_name(string name)
{
// Arrange
var path = ChainablePath.Temp / "SomeFile.txt";

// Act
Action act = () => path.HasName(name);

// Assert
act.Should().Throw<ArgumentException>("*null*empty*");
}

#if NET6_0_OR_GREATER
[Fact]
public void Can_get_the_difference_as_a_relative_path()
Expand Down
16 changes: 16 additions & 0 deletions Pathy/ChainablePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,22 @@ public bool HasExtension(string extension)
return string.Equals(Extension, extension, StringComparison.OrdinalIgnoreCase);
}

/// <summary>
/// Determines if the current path has the specified file or directory name (case-insensitive).
/// </summary>
/// <param name="name">
/// The name to check for (e.g., "MyFile.txt").
/// </param>
public bool HasName(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException("Name cannot be null or empty", nameof(name));
}

return string.Equals(Name, name, StringComparison.OrdinalIgnoreCase);
}

/// <summary>
/// Converts the current <see cref="ChainablePath"/> to an absolute path using the current working directory.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Given an instance of `ChainablePath`, you can get a lot of useful information:
* Not sure if a path points to an actual file system entry? Use `IsFile`, `IsDirectory` or `Exists`
* Want to know the delta between two paths? Use `AsRelativeTo`.
* To determine if a file has a case-insensitive extension, use `HasExtension(".txt")` or `HasExtension("txt")`.
* To check if a path has a specific file or directory name (case-insensitive), use `HasName("MyFile.txt")`.

And if the built-in functionality really isn't enough, you can always call `ToDirectoryInfo` or `ToFileInfo` to continue with an instance of `DirectoryInfo` and `FileInfo`.

Expand Down
Loading