Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const PROTOCOL_STRICT_REGEX = /^[\s\w\0+.-]{2,}:([/\\]{1,2})/;
const PROTOCOL_REGEX = /^[\s\w\0+.-]{2,}:([/\\]{2})?/;
const PROTOCOL_RELATIVE_REGEX = /^([/\\]\s*){2,}[^/\\]/;
const PROTOCOL_SCRIPT_RE = /^[\s\0]*(blob|data|javascript|vbscript):$/i;
const TRAILING_SLASH_RE = /\/$|\/\?|\/#/;
const TRAILING_SLASH_RE = /^[^?#]*\/(?:[?#]|$)/;
const JOIN_LEADING_SLASH_RE = /^\.?\//;

/**
Expand Down
26 changes: 25 additions & 1 deletion test/trailing-slash.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { describe, expect, test } from "vitest";
import { withTrailingSlash, withoutTrailingSlash } from "../src";
import {
hasTrailingSlash,
withTrailingSlash,
withoutTrailingSlash,
} from "../src";

describe("withTrailingSlash, queryParams: false", () => {
const tests = {
Expand Down Expand Up @@ -31,6 +35,10 @@ describe("withTrailingSlash, queryParams: true", () => {
"foo?123": "foo/?123",
"foo/?123": "foo/?123",
"foo?123#abc": "foo/?123#abc",
"foo?query=/": "foo/?query=/",
"foo?query=/#abc": "foo/?query=/#abc",
"/foo#/bar/": "/foo/#/bar/",
"foo/?query=/": "foo/?query=/",
"/#abc": "/#abc",
"#abc": "#abc",
"#": "#",
Expand All @@ -47,6 +55,22 @@ describe("withTrailingSlash, queryParams: true", () => {
});
});

describe("hasTrailingSlash, queryParams: true", () => {
const tests = {
"foo?query=/": false,
"foo?query=/#abc": false,
"/foo#/bar/": false,
"foo/?query=/": true,
"foo/#abc": true,
};

for (const input in tests) {
test(input, () => {
expect(hasTrailingSlash(input, true)).toBe(tests[input]);
});
}
});

describe("withoutTrailingSlash, queryParams: false", () => {
const tests = {
"": "/",
Expand Down