Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions compiler/test/stdlib/path.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,11 @@ assert Path.ancestry(fs("../dir1"), fs("./dir2")) == Ok(Path.NoLineage)
assert Path.ancestry(fs("./dir1"), fs("../../dir2")) == Ok(Path.NoLineage)
assert Path.ancestry(fs("./dir1"), fs("/dir2")) == Err(Path.DifferentBases)
assert Path.ancestry(fs("C:/dir1"), fs("/dir2")) == Err(Path.DifferentRoots)

// Path.removeExtension
assert Path.removeExtension(fs("file.txt")) == fs("file")
assert Path.removeExtension(fs("file")) == fs("file")
assert Path.removeExtension(fs("file.tar.gz")) == fs("file")
assert Path.removeExtension(fs("/test/")) == fs("/test/")
assert Path.removeExtension(fs("/test/test")) == fs("/test/test")
assert Path.removeExtension(fs(".gitignore")) == fs(".gitignore")
23 changes: 23 additions & 0 deletions stdlib/path.gr
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,29 @@ provide let extension = (path: Path) => {
}
}

/**
* Removes the extension from a path, if there is no extension, returns the path as is.
*
* @param path: The path to modify
* @returns The path with the extension removed
*
* @example removeExtension(fromString("file.txt")) == fromString("file")
* @example removeExtension(fromString(".gitignore")) == fromString(".gitignore")
* @example removeExtension(fromString("./dir/file")) == fromString("dir/file")
* @example removeExtension(fromString("./dir/")) == fromString("dir/")
*
* @since v7.0.0
*/
provide let removeExtension = (path: Path) => {
match (pathInfo(path)) {
(base, File, [name, ...rest]) as pathInfo => {
let (name, _) = stemExtHelper(pathInfo)
toPath((base, File, [name, ...rest]))
},
_ => path,
}
}

// should only be used on absolute paths
let rootHelper = (path: PathInfo) => match (path) {
(Abs(root), _, _) => root,
Expand Down
43 changes: 43 additions & 0 deletions stdlib/path.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,49 @@ extension(fromString(".a.tar.gz")) == Ok(".tar.gz")
extension(fromString("/dir/")) == Err(IncompatiblePathType) // can only take extension of a file path
```

### Path.**removeExtension**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
removeExtension : (path: Path) => Path
```

Removes the extension from a path, if there is no extension, returns the path as is.

Parameters:

|param|type|description|
|-----|----|-----------|
|`path`|`Path`|The path to modify|

Returns:

|type|description|
|----|-----------|
|`Path`|The path with the extension removed|

Examples:

```grain
removeExtension(fromString("file.txt")) == fromString("file")
```

```grain
removeExtension(fromString(".gitignore")) == fromString(".gitignore")
```

```grain
removeExtension(fromString("./dir/file")) == fromString("dir/file")
```

```grain
removeExtension(fromString("./dir/")) == fromString("dir/")
```

### Path.**root**

<details disabled>
Expand Down