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
77 changes: 77 additions & 0 deletions src/__tests__/try-path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,81 @@ describe("mapping-entry", () => {
},
]);
});

it("should resolve paths exactly same as input path with trailing slash", () => {
const result = getPathsToTry(
[],
[
{
pattern: "/opt/*",
paths: [join("/absolute", "src", "*")],
},
],
"/opt/"
);
expect(result).toEqual([
{
path: join("/absolute", "src") + "/",
type: "file",
},
{
path: join("/absolute", "src", "package.json"),
type: "package",
},
]);
});

it("should resolve paths exactly same as input path without trailing slash", () => {
const result = getPathsToTry(
[],
[
{
pattern: "/opt/*",
paths: [join("/absolute", "src", "*")],
},
],
"/opt"
);
expect(result).toEqual([
{
path: join("/absolute", "src") + "/",
type: "file",
},
{
path: join("/absolute", "src", "package.json"),
type: "package",
},
]);
});

it("glob match with suffix part on result", () => {
const result = getPathsToTry(
[],
[
{
pattern: "/opt/*",
paths: [join("/absolute", "src", "*", "suffix", "part")],
},
],
"/opt/relative/part"
);
expect(result).toEqual([
{
path: join("/absolute", "src", "relative", "part", "suffix", "part"),
type: "file",
},
{
path: join(
"/absolute",
"src",
"relative",
"part",
"suffix",
"part",
"package.json"
),
type: "package",
},
]);
});
});
6 changes: 5 additions & 1 deletion src/try-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function exhaustiveTypeException(check: never): never {
* @returns the part of search that * matches, or undefined if no match.
*/
function matchStar(pattern: string, search: string): string | undefined {
if (search.length < pattern.length) {
if (search.length + 2 < pattern.length) {
return undefined;
}
if (pattern === "*") {
Expand All @@ -105,6 +105,10 @@ function matchStar(pattern: string, search: string): string | undefined {
}
const part1 = pattern.substring(0, star);
const part2 = pattern.substring(star + 1);
// "/some/path/*" should match "/some/path"
if (part1 === search + "/" && part2 === "") {
return "";
}
if (search.substr(0, star) !== part1) {
return undefined;
}
Expand Down