Skip to content

Commit 7b7acfd

Browse files
authored
feat(stdlib): add Path.removeExtension (#2226)
1 parent 9c49a23 commit 7b7acfd

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

compiler/test/stdlib/path.test.gr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,11 @@ assert Path.ancestry(fs("../dir1"), fs("./dir2")) == Ok(Path.NoLineage)
277277
assert Path.ancestry(fs("./dir1"), fs("../../dir2")) == Ok(Path.NoLineage)
278278
assert Path.ancestry(fs("./dir1"), fs("/dir2")) == Err(Path.DifferentBases)
279279
assert 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")

stdlib/path.gr

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff 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
726749
let rootHelper = (path: PathInfo) => match (path) {
727750
(Abs(root), _, _) => root,

stdlib/path.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,49 @@ extension(fromString(".a.tar.gz")) == Ok(".tar.gz")
637637
extension(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>

0 commit comments

Comments
 (0)