Skip to content

Commit 6d795d0

Browse files
authored
feat(stdlib): Add Path.updateExtension (#2282)
1 parent 0431e74 commit 6d795d0

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

compiler/test/stdlib/path.test.gr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,10 @@ assert Path.removeExtension(fs("file.tar.gz")) == fs("file")
285285
assert Path.removeExtension(fs("/test/")) == fs("/test/")
286286
assert Path.removeExtension(fs("/test/test")) == fs("/test/test")
287287
assert Path.removeExtension(fs(".gitignore")) == fs(".gitignore")
288+
289+
// Path.updateExtension
290+
assert Path.updateExtension(fs("file.txt"), "ext") == fs("file.ext")
291+
assert Path.updateExtension(fs("file.txt"), "") == fs("file.")
292+
assert Path.updateExtension(fs(".gitignore"), "ext") == fs(".gitignore.ext")
293+
assert Path.updateExtension(fs("./dir/file"), "ext") == fs("dir/file.ext")
294+
assert Path.updateExtension(fs("./dir/"), "ext") == fs("dir/")

stdlib/path.gr

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,31 @@ provide let removeExtension = (path: Path) => {
745745
}
746746
}
747747

748+
/**
749+
* Updates the file extension of the given path.
750+
*
751+
* @param path: The path to modify
752+
* @param extension: The new extension
753+
* @returns The modified path
754+
*
755+
* @example updateExtension(fromString("file.txt"), "ext") == fromString("file.ext")
756+
* @example updateExtension(fromString("file.txt"), "") == fromString("file.")
757+
* @example updateExtension(fromString(".gitignore"), "ext") == fromString(".gitignore.ext")
758+
* @example updateExtension(fromString("./dir/file"), "ext") == fromString("dir/file.ext")
759+
* @example updateExtension(fromString("./dir/"), "ext") == fromString("dir/")
760+
*
761+
* @since v7.0.0
762+
*/
763+
provide let updateExtension = (path: Path, extension: String) => {
764+
match (pathInfo(path)) {
765+
(base, File, [name, ...rest]) as pathInfo => {
766+
let (name, _) = stemExtHelper(pathInfo)
767+
toPath((base, File, [name ++ "." ++ extension, ...rest]))
768+
},
769+
_ => path,
770+
}
771+
}
772+
748773
// should only be used on absolute paths
749774
let rootHelper = (path: PathInfo) => match (path) {
750775
(Abs(root), _, _) => root,

stdlib/path.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,54 @@ removeExtension(fromString("./dir/file")) == fromString("dir/file")
680680
removeExtension(fromString("./dir/")) == fromString("dir/")
681681
```
682682

683+
### Path.**updateExtension**
684+
685+
<details disabled>
686+
<summary tabindex="-1">Added in <code>next</code></summary>
687+
No other changes yet.
688+
</details>
689+
690+
```grain
691+
updateExtension: (path: Path, extension: String) => Path
692+
```
693+
694+
Updates the file extension of the given path.
695+
696+
Parameters:
697+
698+
|param|type|description|
699+
|-----|----|-----------|
700+
|`path`|`Path`|The path to modify|
701+
|`extension`|`String`|The new extension|
702+
703+
Returns:
704+
705+
|type|description|
706+
|----|-----------|
707+
|`Path`|The modified path|
708+
709+
Examples:
710+
711+
```grain
712+
updateExtension(fromString("file.txt"), "ext") == fromString("file.ext")
713+
```
714+
715+
```grain
716+
updateExtension(fromString("file.txt"), "") == fromString("file.")
717+
```
718+
719+
```grain
720+
updateExtension(fromString(".gitignore"), "ext") == fromString(".gitignore.ext")
721+
```
722+
723+
```grain
724+
updateExtension(fromString("./dir/file"), "ext") == fromString("dir/file.ext")
725+
```
726+
727+
```grain
728+
updateExtension(fromString("./dir/"), "ext") == fromString("dir/")
729+
```
730+
683731
### Path.**root**
684732

685733
<details disabled>

0 commit comments

Comments
 (0)