File tree Expand file tree Collapse file tree 3 files changed +74
-0
lines changed
Expand file tree Collapse file tree 3 files changed +74
-0
lines changed Original file line number Diff line number Diff line change @@ -277,3 +277,11 @@ assert Path.ancestry(fs("../dir1"), fs("./dir2")) == Ok(Path.NoLineage)
277277assert Path.ancestry(fs("./dir1"), fs("../../dir2")) == Ok(Path.NoLineage)
278278assert Path.ancestry(fs("./dir1"), fs("/dir2")) == Err(Path.DifferentBases)
279279assert Path.ancestry(fs("C:/dir1"), fs("/dir2")) == Err(Path.DifferentRoots)
280+
281+ // Path.removeExtension
282+ assert Path.removeExtension(fs("file.txt")) == fs("file")
283+ assert Path.removeExtension(fs("file")) == fs("file")
284+ assert Path.removeExtension(fs("file.tar.gz")) == fs("file")
285+ assert Path.removeExtension(fs("/test/")) == fs("/test/")
286+ assert Path.removeExtension(fs("/test/test")) == fs("/test/test")
287+ assert Path.removeExtension(fs(".gitignore")) == fs(".gitignore")
Original file line number Diff line number Diff line change @@ -722,6 +722,29 @@ provide let extension = (path: Path) => {
722722 }
723723}
724724
725+ /**
726+ * Removes the extension from a path, if there is no extension, returns the path as is.
727+ *
728+ * @param path: The path to modify
729+ * @returns The path with the extension removed
730+ *
731+ * @example removeExtension(fromString("file.txt")) == fromString("file")
732+ * @example removeExtension(fromString(".gitignore")) == fromString(".gitignore")
733+ * @example removeExtension(fromString("./dir/file")) == fromString("dir/file")
734+ * @example removeExtension(fromString("./dir/")) == fromString("dir/")
735+ *
736+ * @since v7.0.0
737+ */
738+ provide let removeExtension = (path: Path) => {
739+ match (pathInfo(path)) {
740+ (base, File, [name, ...rest]) as pathInfo => {
741+ let (name, _) = stemExtHelper(pathInfo)
742+ toPath((base, File, [name, ...rest]))
743+ },
744+ _ => path,
745+ }
746+ }
747+
725748// should only be used on absolute paths
726749let rootHelper = (path: PathInfo) => match (path) {
727750 (Abs(root), _, _) => root,
Original file line number Diff line number Diff line change @@ -637,6 +637,49 @@ extension(fromString(".a.tar.gz")) == Ok(".tar.gz")
637637extension(fromString("/dir/")) == Err(IncompatiblePathType) // can only take extension of a file path
638638```
639639
640+ ### Path.** removeExtension**
641+
642+ <details disabled >
643+ <summary tabindex =" -1 " >Added in <code >next</code ></summary >
644+ No other changes yet.
645+ </details >
646+
647+ ``` grain
648+ removeExtension : (path: Path) => Path
649+ ```
650+
651+ Removes the extension from a path, if there is no extension, returns the path as is.
652+
653+ Parameters:
654+
655+ | param| type| description|
656+ | -----| ----| -----------|
657+ | ` path ` | ` Path ` | The path to modify|
658+
659+ Returns:
660+
661+ | type| description|
662+ | ----| -----------|
663+ | ` Path ` | The path with the extension removed|
664+
665+ Examples:
666+
667+ ``` grain
668+ removeExtension(fromString("file.txt")) == fromString("file")
669+ ```
670+
671+ ``` grain
672+ removeExtension(fromString(".gitignore")) == fromString(".gitignore")
673+ ```
674+
675+ ``` grain
676+ removeExtension(fromString("./dir/file")) == fromString("dir/file")
677+ ```
678+
679+ ``` grain
680+ removeExtension(fromString("./dir/")) == fromString("dir/")
681+ ```
682+
640683### Path.** root**
641684
642685<details disabled >
You can’t perform that action at this time.
0 commit comments