diff --git a/.idea/.idea.TruePath/.idea/dictionaries/fried.xml b/.idea/.idea.TruePath/.idea/dictionaries/fried.xml index b62ce81..f399200 100644 --- a/.idea/.idea.TruePath/.idea/dictionaries/fried.xml +++ b/.idea/.idea.TruePath/.idea/dictionaries/fried.xml @@ -3,6 +3,7 @@ andivionian fornever + komroncube ventis diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e3068a..ec593c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` 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. @@ -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 diff --git a/README.md b/README.md index 6c57a99..4a854bc 100644 --- a/README.md +++ b/README.md @@ -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 ------------- diff --git a/TruePath.Tests/PathExtensionsTests.cs b/TruePath.Tests/PathExtensionsTests.cs index 0f67054..64fc0a4 100644 --- a/TruePath.Tests/PathExtensionsTests.cs +++ b/TruePath.Tests/PathExtensionsTests.cs @@ -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()); + } } diff --git a/TruePath/PathExtensions.cs b/TruePath/PathExtensions.cs index 460c4ae..03aa0fa 100644 --- a/TruePath/PathExtensions.cs +++ b/TruePath/PathExtensions.cs @@ -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. /// public static string GetExtensionWithoutDot(this IPath path) => GetExtensionWithDot(path).TrimStart('.'); + + /// + /// Gets the file name of the without the extension. + /// For example, for the path file.txt, this method will return a string file. + /// + /// + /// 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). + /// + public static string GetFilenameWithoutExtension(this IPath path) => + Path.GetFileNameWithoutExtension(path.FileName); }