Skip to content

Commit

Permalink
simplify return value
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 3, 2025
1 parent 7644809 commit 4be44f7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
12 changes: 4 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,12 @@ export function resolveAlias(path: string, aliases: Record<string, string>) {
/**
* Resolves a path string to its possible alias.
*
* Returns an array of possible alias resolutions, sorted by specificity (longest first).
*
* Returns `undefined` if no alias is found.
* Returns an array of possible alias resolutions (could be empty), sorted by specificity (longest first).
*/
export function reverseResolveAlias(
path: string,
aliases: Record<string, string>,
): undefined | string[] {
): string[] {
const _path = normalizeWindowsPath(path);
aliases = normalizeAliases(aliases);

Expand All @@ -103,10 +101,8 @@ export function reverseResolveAlias(
}
}

return matches.length > 0
? // Sort by length, longest (more specific) first
matches.sort((a, b) => b.length - a.length)
: undefined;
// Sort by length, longest (more specific) first
return matches.sort((a, b) => b.length - a.length);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ describe("alias", () => {
});

it("no match", () => {
expect(reverseResolveAlias("foo/bar.js", aliases)).toBeUndefined();
expect(reverseResolveAlias("./bar.js", aliases)).toBeUndefined();
expect(reverseResolveAlias("foo/bar.js", aliases)).toMatchObject([]);
expect(reverseResolveAlias("./bar.js", aliases)).toMatchObject([]);
});

it("respect ending with /", () => {
Expand Down

0 comments on commit 4be44f7

Please sign in to comment.