denoland / denoland/std

Path: Should `normalize()` remove trailing slashes?

Open
#2,441 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
3.6k
Forks
681
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**

I'm trying to find out if a file is inside a specific directory. My plan was to do it like this:

```js
import { common } from "https://deno.land/std@0.148.0/path/mod.ts";
function insideDir(file, dir) {
const result = common([file, dir]);
return result == dir;
}
```

However, this doesn't work if the `dir` argument doesn't contain a trailing slash. For example:
```js
common(["/path/to/dir", "/path/to/dir/file.txt"]);
```
returns `"/path/to/dir/"`, and `"/path/to/dir/" != "/path/to/dir"`

**Describe the solution you'd like**

If I'm not mistaken, `/path/to/dir` and `/path/to/dir/` both point to the same thing. If so, then wouldn't it make sense to have a function that removes trailing slashes from paths? Maybe `normalize()` would be a good fit for this.

**Describe alternatives you've considered**

- Add a function to std that performs this exact algorithm
- Make `common()` remove the trailing slash
- It seems `resolve()` also removes trailing slashes, though passing just a single command isn't really documented anywhere (and neither does it contain tests for this case)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.