Skip to content

Commit

Permalink
feat: add parseFilename utility (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
divine authored Jul 28, 2023
1 parent 716cf8d commit 4cbab46
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ getQuery('http://foo.com/foo?test=123&unicode=%E5%A5%BD')
// { test: '123', unicode: '好' }
```

### `parseFilename`

```ts
// Result: filename.ext
parseFilename('http://example.com/path/to/filename.ext')

// Result: undefined
parseFilename('/path/to/.hidden-file', { strict: true })
```

### `$URL`

Implementing URL interface with improvements:
Expand Down
11 changes: 11 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,14 @@ export function stringifyParsedURL(parsed: ParsedURL): string {
fullpath
);
}

const FILENAME_STRICT_REGEX = /\/([^/]+\.[^/]+)$/;
const FILENAME_REGEX = /\/([^/]+)$/;

export function parseFilename(input = "", { strict }): string | undefined {
const { pathname } = parseURL(input);
const matches = strict
? pathname.match(FILENAME_STRICT_REGEX)
: pathname.match(FILENAME_REGEX);
return matches ? matches[1] : undefined;
}
97 changes: 96 additions & 1 deletion test/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "vitest";
import { parseURL, parseHost } from "../src";
import { parseURL, parseHost, parseFilename } from "../src";

describe("parseURL", () => {
const tests = [
Expand Down Expand Up @@ -102,3 +102,98 @@ describe("parseHost", () => {
});
}
});

describe("parseFilename", () => {
const tests = [
{ input: ["/path/to/filename.ext", false], out: "filename.ext" },
{ input: ["/path/to/.hidden-file", false], out: ".hidden-file" },
{ input: ["/path/to/dir/", false], out: undefined },
{ input: [".", false], out: undefined },
{ input: ["/", false], out: undefined },
{ input: ["", false], out: undefined },
{
input: ["http://example.com/path/to/filename.ext", false],
out: "filename.ext",
},
{
input: ["http://example.com/path/to/filename.ext?query=true", false],
out: "filename.ext",
},
{
input: ["http://example.com/path/to/filename.ext#hash", false],
out: "filename.ext",
},
{
input: ["http://example.com/path/to/filename.ext?query=true#hash", false],
out: "filename.ext",
},
{
input: [
"http://example.com/path/to/filename.ext/?query=true#hash",
false,
],
out: undefined,
},
{ input: ["http://example.com/path/to/dir/", false], out: undefined },
{
input: ["http://example.com/path/to/dir/?query=true#hash", false],
out: undefined,
},
{ input: ["http://example.com/path/to/dir/#hash", false], out: undefined },
{ input: ["http://example.com", false], out: undefined },
{
input: ["ftp://example.com/path/to/filename.ext", false],
out: "filename.ext",
},
{ input: ["file:///path/to/filename.ext", false], out: "filename.ext" },
{ input: ["/path/to/filename.ext", true], out: "filename.ext" },
{ input: ["/path/to/.hidden-file", true], out: undefined },
{ input: ["/path/to/dir/", true], out: undefined },
{ input: [".", true], out: undefined },
{ input: ["/", true], out: undefined },
{ input: ["", true], out: undefined },
{
input: ["http://example.com/path/to/filename.ext", true],
out: "filename.ext",
},
{
input: ["http://example.com/path/to/filename.ext?query=true", true],
out: "filename.ext",
},
{
input: ["http://example.com/path/to/filename.ext#hash", true],
out: "filename.ext",
},
{
input: ["http://example.com/path/to/filename.ext?query=true#hash", true],
out: "filename.ext",
},
{
input: ["http://example.com/path/to/filename.ext/?query=true#hash", true],
out: undefined,
},
{ input: ["http://example.com/path/to/dir/", true], out: undefined },
{
input: ["http://example.com/path/to/dir/?query=true#hash", true],
out: undefined,
},
{ input: ["http://example.com/path/to/dir/#hash", true], out: undefined },
{ input: ["http://example.com", true], out: undefined },
{
input: ["ftp://example.com/path/to/filename.ext", true],
out: "filename.ext",
},
{ input: ["file:///path/to/filename.ext", true], out: "filename.ext" },
{ input: ["/path/to/filename.ext/", true], out: undefined },
{ input: ["/path/to/dir/../filename.ext", true], out: "filename.ext" },
{ input: ["/path/to/dir/../filename.ext/", true], out: undefined },
];

for (const t of tests) {
test(t.input.toString(), () => {
expect(
parseFilename(t.input[0].toString(), { strict: t.input[1] })
).toStrictEqual(t.out);
});
}
});

0 comments on commit 4cbab46

Please sign in to comment.