Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#41) PathExtensions: add get filename without extension method #54

Merged
merged 2 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .idea/.idea.TruePath/.idea/dictionaries/fried.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased] (1.2.0)
### Added
- New `IPath` and `IPath<TPath>` interfaces that allow to process any paths (`LocalPath` and `AbsolutePath`) in a polymorphic way.
- [#41](https://github.com/ForNeVeR/TruePath/issues/41): `GetFileNameWithoutExtension` extension method for `IPath`.

Thanks to [@Komroncube][komroncube].
- [#42](https://github.com/ForNeVeR/TruePath/issues/42): `GetExtensionWithDot` and `GetExtensionWithoutDot` extension methods for `IPath`.

Thanks to @Komroncube.
Expand Down Expand Up @@ -53,6 +56,8 @@ This is the first published version of the package. It doesn't contain any featu

[docs.readme]: README.md

[komroncube]: https://github.com/Komroncube

[0.0.0]: https://github.com/ForNeVeR/TruePath/releases/tag/v0.0.0
[1.0.0]: https://github.com/ForNeVeR/TruePath/compare/v0.0.0...v1.0.0
[1.1.0]: https://github.com/ForNeVeR/TruePath/compare/v1.0.0...v1.1.0
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ Aside from the strict types, the following features are supported for the paths:
- `LocalPath::IsAbsolute` to check the path kind (since it supports both kinds);
- `LocalPath::IsPrefixOf` to check path prefixes;
- `LocalPath::RelativeTo` to get a relative part between two paths, if possible;
- extension methods on `IPath`: `GetExtensionWithDot` and `GetExtensionWithoutDot` to get the file extension with or without the leading dot (note that `GetExtensionWithDot` will behave differently for paths ending with dots and paths without dot at all, which allows to reconstruct such a file name from its part without extension and the "extension with dot").
- extension methods on `IPath`:
- `GetExtensionWithDot` and `GetExtensionWithoutDot` to get the file extension with or without the leading dot (note that `GetExtensionWithDot` will behave differently for paths ending with dots and paths without dot at all);
- `GetFileNameWithoutExtension` to get the file name without the extension (and without the trailing dot, if any)

(Note how `GetFileNameWithoutExtension()` works nicely together with `GetExtensionWithDot()` to reconstruct the resulting path from their concatenation, however weird the initial name was — no extension, trailing dot, no base name.)

Documentation
-------------
Expand Down
31 changes: 31 additions & 0 deletions TruePath.Tests/PathExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,35 @@ public void GetExtensionWithoutDotTests(string path, string expected)
IPath a = new AbsolutePath(path);
Assert.Equal(expected, a.GetExtensionWithoutDot());
}

[Theory]
[InlineData("..", ".")]
[InlineData("foo/bar.txt", "bar")]
[InlineData("/foo/bar.txt", "bar")]
[InlineData("foo/bar.", "bar")]
[InlineData("foo/bar", "bar")]
[InlineData(".gitignore", "")]
public void GetFilenameWithoutExtensionTests(string path, string expected)
{
IPath l = new LocalPath(path);
Assert.Equal(expected, l.GetFilenameWithoutExtension());

if (!path.StartsWith('/')) return;

IPath a = new AbsolutePath(path);
Assert.Equal(expected, a.GetFilenameWithoutExtension());
}

[Theory]
[InlineData("..")]
[InlineData("file.txt")]
[InlineData("file..txt")]
[InlineData(".gitignore")]
[InlineData("gitignore.")]
public void FileNameInvariantTests(string inputPath)
{
var path = new LocalPath(inputPath);
var fileName = path.FileName;
Assert.Equal(fileName, path.GetFilenameWithoutExtension() + path.GetExtensionWithDot());
}
}
11 changes: 11 additions & 0 deletions TruePath/PathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ public static string GetExtensionWithDot(this IPath path) =>
/// reconstruct the original name from its name without extension and its extension without dot.
/// </remarks>
public static string GetExtensionWithoutDot(this IPath path) => GetExtensionWithDot(path).TrimStart('.');

/// <summary>
/// <para>Gets the file name of the <paramref name="path"/> without the extension.</para>
/// <para>For example, for the path <c>file.txt</c>, this method will return a string <c>file</c>.</para>
/// </summary>
/// <returns>
/// The file name of the path without the extension. If the path has no extension, the file name is returned as-is
/// (one trailing dot will be stripped, though).
/// </returns>
public static string GetFilenameWithoutExtension(this IPath path) =>
Path.GetFileNameWithoutExtension(path.FileName);
}