From ba4fa291ea753ce7c464c094598a2f8fef2bcc31 Mon Sep 17 00:00:00 2001 From: Abdujabborov Kamronbek Date: Sun, 5 May 2024 21:16:47 +0500 Subject: [PATCH 1/2] PathExtensions: add get filename without extension method --- TruePath.Tests/PathExtensionsTests.cs | 17 +++++++++++++++++ TruePath/PathExtensions.cs | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/TruePath.Tests/PathExtensionsTests.cs b/TruePath.Tests/PathExtensionsTests.cs index 0f67054..5445a94 100644 --- a/TruePath.Tests/PathExtensionsTests.cs +++ b/TruePath.Tests/PathExtensionsTests.cs @@ -42,4 +42,21 @@ public void GetExtensionWithoutDotTests(string path, string expected) IPath a = new AbsolutePath(path); Assert.Equal(expected, a.GetExtensionWithoutDot()); } + + [Theory] + [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()); + } } diff --git a/TruePath/PathExtensions.cs b/TruePath/PathExtensions.cs index 460c4ae..6769cbd 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. + /// + /// + public static string GetFilenameWithoutExtension(this IPath path) => + Path.GetFileNameWithoutExtension(path.FileName); } From 268b1f61e298627c0daade3b2ceb5f0d1201c476 Mon Sep 17 00:00:00 2001 From: Friedrich von Never Date: Sun, 5 May 2024 22:30:09 +0200 Subject: [PATCH 2/2] (#41) GetFileNameWithoutExtension: documentation + more tests --- .idea/.idea.TruePath/.idea/dictionaries/fried.xml | 1 + CHANGELOG.md | 5 +++++ README.md | 6 +++++- TruePath.Tests/PathExtensionsTests.cs | 14 ++++++++++++++ TruePath/PathExtensions.cs | 6 +++--- 5 files changed, 28 insertions(+), 4 deletions(-) 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 5445a94..64fc0a4 100644 --- a/TruePath.Tests/PathExtensionsTests.cs +++ b/TruePath.Tests/PathExtensionsTests.cs @@ -44,6 +44,7 @@ public void GetExtensionWithoutDotTests(string path, string expected) } [Theory] + [InlineData("..", ".")] [InlineData("foo/bar.txt", "bar")] [InlineData("/foo/bar.txt", "bar")] [InlineData("foo/bar.", "bar")] @@ -59,4 +60,17 @@ public void GetFilenameWithoutExtensionTests(string path, string expected) 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 6769cbd..03aa0fa 100644 --- a/TruePath/PathExtensions.cs +++ b/TruePath/PathExtensions.cs @@ -41,12 +41,12 @@ public static string GetExtensionWithDot(this IPath path) => /// /// Gets the file name of the without the extension. - /// For example, for the path file.txt, this method will return a string file + /// 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. + /// 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); }