Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): reverse resolve alias #173

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jobs:
- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 16
cache: "pnpm"
node-version: "18"
cache: pnpm
- run: pnpm install
- run: pnpm lint
- run: pnpm build
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Pathe exports some extra utilities that do not exist in standard Node.js [path m
In order to use them, you can import from `pathe/utils` subpath:

```js
import { filename, normalizeAliases, resolveAlias } from 'pathe/utils'
import { filename, normalizeAliases, resolveAlias, reverseResolveAlias } from 'pathe/utils'
```

## License
Expand Down
22 changes: 22 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@ export function resolveAlias(path: string, aliases: Record<string, string>) {
return _path;
}

export function reverseResolveAlias(
path: string,
aliases: Record<string, string>,
) {
const _path = normalizeWindowsPath(path);
aliases = normalizeAliases(aliases);

for (const [to, alias] of Object.entries(aliases).reverse()) {
if (!_path.startsWith(alias)) {
continue;
}

// Strip trailing slash from alias for check
const _alias = hasTrailingSlash(alias) ? alias.slice(0, -1) : alias;

if (hasTrailingSlash(_path[_alias.length])) {
return join(to, _path.slice(alias.length));
}
}
return _path;
}

const FILENAME_RE = /(^|[/\\])([^/\\]+?)(?=(\.[^.]+)?$)/;

export function filename(path: string) {
Expand Down
34 changes: 33 additions & 1 deletion test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { describe, expect, it } from "vitest";

import { normalizeAliases, filename, resolveAlias } from "../src/utils";
import {
normalizeAliases,
filename,
resolveAlias,
reverseResolveAlias,
} from "../src/utils";

describe("alias", () => {
const _aliases = {
Expand Down Expand Up @@ -56,6 +61,33 @@ describe("alias", () => {
expect(resolveAlias("~win/foo/bar", aliases)).toBe("C:/src/foo/bar");
});
});

describe("reverseResolveAlias", () => {
for (const [to, from] of Object.entries(aliases)) {
it(from, () => {
expect(reverseResolveAlias(from, aliases)).toBe(to);
});
}
it("respects path separators", () => {
const aliases = {
"~": "/root",
"~assets": "/root/some/assets",
};
expect(
reverseResolveAlias("/root/some/assets/smth.jpg", aliases),
).toMatchInlineSnapshot('"~assets/smth.jpg"');
});
it("unchanged", () => {
expect(reverseResolveAlias("foo/bar.js", aliases)).toBe("foo/bar.js");
expect(reverseResolveAlias("./bar.js", aliases)).toBe("./bar.js");
});
it("respect ending with /", () => {
expect(reverseResolveAlias("/src/foo/bar", aliases)).toBe("~/foo/bar");
expect(reverseResolveAlias("C:/src/foo/bar", aliases)).toBe(
"~win/foo/bar",
);
});
});
});

describe("filename", () => {
Expand Down