Skip to content

Commit 64f23c5

Browse files
The long lost cousin of takeExtension has found its way home
1 parent ae0be14 commit 64f23c5

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

System/FilePath/Internal.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module System.FilePath.MODULE_NAME
7575
-- * Extension functions
7676
splitExtension,
7777
takeExtension, replaceExtension, (-<.>), dropExtension, addExtension, hasExtension, (<.>),
78-
splitExtensions, dropExtensions, takeExtensions, replaceExtensions,
78+
splitExtensions, dropExtensions, takeExtensions, replaceExtensions, isExtensionOf,
7979
stripExtension,
8080

8181
-- * Filename\/directory functions
@@ -313,6 +313,15 @@ hasExtension :: FilePath -> Bool
313313
hasExtension = any isExtSeparator . takeFileName
314314

315315

316+
-- | Is the given string the final extension of the filename?
317+
-- The extension should not include the separator.
318+
--
319+
-- > "png" `isExtensionOf` "/directory/file.png" == True
320+
-- > "png" `isExtensionOf` "/directory/file.png.jpg" == False
321+
-- > "csv" `isExtensionOf` "/directory/data.csv" == True
322+
isExtensionOf :: String -> FilePath -> Bool
323+
isExtensionOf ext = (== ext) . drop 1 . takeExtension
324+
316325
-- | Drop the given extension from a FilePath, and the @\".\"@ preceding it.
317326
-- Returns 'Nothing' if the FilePath does not have the given extension, or
318327
-- 'Just' and the part before the extension if it does.

tests/TestGen.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ tests =
106106
,("W.hasExtension \"/directory/path.ext\" == True", property $ W.hasExtension "/directory/path.ext" == True)
107107
,("P.hasExtension \"/directory/path\" == False", property $ P.hasExtension "/directory/path" == False)
108108
,("W.hasExtension \"/directory/path\" == False", property $ W.hasExtension "/directory/path" == False)
109+
,("W.isExtensionOf \"png\" \"/directory/file.png\" == True", property $ W.isExtensionOf "png" "/directory/file.png" == True)
110+
,("P.isExtensionOf \"png\" \"/directory/file.png\" == True", property $ P.isExtensionOf "png" "/directory/file.png" == True)
111+
,("W.isExtensionOf \"png\" \"/directory/file.png.jpg\" == False", property $ W.isExtensionOf "png" "/directory/file.png.jpg" == False)
112+
,("P.isExtensionOf \"png\" \"/directory/file.png.jpg\" == False", property $ P.isExtensionOf "png" "/directory/file.png.jpg" == False)
113+
,("W.isExtensionOf \"csv\" \"/directory/data.csv\" == True", property $ W.isExtensionOf "csv" "/directory/data.csv" == True)
114+
,("P.isExtensionOf \"csv\" \"/directory/data.csv\" == True", property $ P.isExtensionOf "csv" "/directory/data.csv" == True)
109115
,("null (P.takeExtension x) == not (P.hasExtension x)", property $ \(QFilePath x) -> null (P.takeExtension x) == not (P.hasExtension x))
110116
,("null (W.takeExtension x) == not (W.hasExtension x)", property $ \(QFilePath x) -> null (W.takeExtension x) == not (W.hasExtension x))
111117
,("P.stripExtension \"hs.o\" \"foo.x.hs.o\" == Just \"foo.x\"", property $ P.stripExtension "hs.o" "foo.x.hs.o" == Just "foo.x")

0 commit comments

Comments
 (0)