From 6962660371929318a409d20800f6f8f8464d3cbb Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 5 Aug 2022 22:02:14 +0100 Subject: [PATCH 01/14] feat: add `/utils` subpath with `sortPaths`, `resolveAliases` and `filename` --- .github/workflows/ci.yml | 1 + build.config.ts | 4 ++++ package.json | 7 ++++++- src/_internal.ts | 7 +++++++ src/path.ts | 2 +- src/utils.ts | 33 ++++++++++++++++++++++++++++----- test/index.spec.ts | 2 +- tsconfig.json | 14 ++++++++++++-- 8 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 build.config.ts create mode 100644 src/_internal.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3db5858..e1ff5e5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,5 +21,6 @@ jobs: - run: pnpm install - run: pnpm lint - run: pnpm build + - run: pnpm test:types - run: pnpm vitest --coverage - uses: codecov/codecov-action@v3 diff --git a/build.config.ts b/build.config.ts new file mode 100644 index 0000000..e6a13ae --- /dev/null +++ b/build.config.ts @@ -0,0 +1,4 @@ +import { defineBuildConfig } from 'unbuild' +export default defineBuildConfig({ + externals: ['pathe'] +}) diff --git a/package.json b/package.json index afe0412..2dbdd18 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,10 @@ ".": { "import": "./dist/index.mjs", "require": "./dist/index.cjs" + }, + "./utils": { + "import": "./dist/utils.mjs", + "require": "./dist/utils.cjs" } }, "main": "./dist/index.cjs", @@ -22,7 +26,8 @@ "lint": "eslint --ext .ts .", "prepublishOnly": "pnpm build", "release": "pnpm vitest run && standard-version && git push --follow-tags && pnpm publish", - "test": "pnpm lint && vitest run --coverage" + "test": "pnpm lint && vitest run --coverage", + "test:types": "tsc --noEmit" }, "devDependencies": { "@nuxtjs/eslint-config-typescript": "latest", diff --git a/src/_internal.ts b/src/_internal.ts new file mode 100644 index 0000000..8698673 --- /dev/null +++ b/src/_internal.ts @@ -0,0 +1,7 @@ +// Util to normalize windows paths to posix +export function normalizeWindowsPath (input: string = '') { + if (!input.includes('\\')) { + return input + } + return input.replace(/\\/g, '/') +} diff --git a/src/path.ts b/src/path.ts index 0c619e3..b0655cf 100644 --- a/src/path.ts +++ b/src/path.ts @@ -8,7 +8,7 @@ Check LICENSE file import type path from 'path' -import { normalizeWindowsPath } from './utils' +import { normalizeWindowsPath } from './_internal' const _UNC_REGEX = /^[/][/]/ const _UNC_DRIVE_REGEX = /^[/][/]([.]{1,2}[/])?([a-zA-Z]):[/]/ diff --git a/src/utils.ts b/src/utils.ts index 8698673..c2eb004 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,30 @@ -// Util to normalize windows paths to posix -export function normalizeWindowsPath (input: string = '') { - if (!input.includes('\\')) { - return input +import { basename } from 'pathe' + +export function resolveAliases (_aliases: Record) { + // Sort aliases from specific to general (ie. fs/promises before fs) + const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) => + (b.split('/').length - a.split('/').length) || (b.length - a.length) + )) + // Resolve alias values in relation to each other + for (const key in aliases) { + for (const alias in aliases) { + if (!['~', '@', '#'].includes(alias[0])) { continue } + if (alias === '@' && !aliases[key].startsWith('@/')) { continue } // Don't resolve @foo/bar + + if (aliases[key].startsWith(alias)) { + aliases[key] = aliases[alias] + aliases[key].slice(alias.length) + } + } } - return input.replace(/\\/g, '/') + return aliases +} + +export function sortPaths (paths: string[]) { + return paths.sort((a, b) => + (b.split('/').length - a.split('/').length) || (b.length - a.length) + ) +} + +export function filename (path: string) { + return basename(path).replace(/\.[^.]+$/, '') } diff --git a/test/index.spec.ts b/test/index.spec.ts index 74cb8c6..262c01b 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -2,7 +2,7 @@ import { describe, expect, it, vi } from 'vitest' import { basename, dirname, extname, format, parse, relative, delimiter, isAbsolute, join, normalize, resolve, sep, toNamespacedPath } from '../src' -import { normalizeWindowsPath } from '../src/utils' +import { normalizeWindowsPath } from '../src/_internal' runTest('normalizeWindowsPath', normalizeWindowsPath, { // POSIX diff --git a/tsconfig.json b/tsconfig.json index 7ed9c46..f9f3e13 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,9 +3,19 @@ "target": "ESNext", "module": "ESNext", "moduleResolution": "Node", - "esModuleInterop": true + "esModuleInterop": true, + "skipLibCheck": true, + "paths": { + "pathe/utils": [ + "./src/utils" + ], + "pathe": [ + "./src/index" + ] + }, }, "include": [ - "src" + "src", + "test" ] } From 05090a31ebe5b3839288c4613d9638cdf4736b8d Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Aug 2022 12:47:32 +0100 Subject: [PATCH 02/14] refactor: create standalone filename regexp --- src/utils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index c2eb004..33868ef 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,3 @@ -import { basename } from 'pathe' - export function resolveAliases (_aliases: Record) { // Sort aliases from specific to general (ie. fs/promises before fs) const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) => @@ -25,6 +23,8 @@ export function sortPaths (paths: string[]) { ) } +const FILENAME_RE = /(?<=^|\/)([^/]+?)(?=(\.[^.]+)?$)/ + export function filename (path: string) { - return basename(path).replace(/\.[^.]+$/, '') + return path.match(FILENAME_RE)?.[0] } From 4d5482d51ab15238e928f06d66f1ca08a40eb3de Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Aug 2022 12:47:40 +0100 Subject: [PATCH 03/14] fix: remove nuxt-specific aliases --- src/utils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 33868ef..b89a3d0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -6,7 +6,6 @@ export function resolveAliases (_aliases: Record) { // Resolve alias values in relation to each other for (const key in aliases) { for (const alias in aliases) { - if (!['~', '@', '#'].includes(alias[0])) { continue } if (alias === '@' && !aliases[key].startsWith('@/')) { continue } // Don't resolve @foo/bar if (aliases[key].startsWith(alias)) { From f970367988c240f939f5f9f028006b1fadd949c2 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Aug 2022 12:51:34 +0100 Subject: [PATCH 04/14] refactor: extract `comparePaths` util --- src/utils.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index b89a3d0..eae0931 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,8 +1,6 @@ export function resolveAliases (_aliases: Record) { // Sort aliases from specific to general (ie. fs/promises before fs) - const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) => - (b.split('/').length - a.split('/').length) || (b.length - a.length) - )) + const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) => comparePaths(a, b))) // Resolve alias values in relation to each other for (const key in aliases) { for (const alias in aliases) { @@ -17,11 +15,11 @@ export function resolveAliases (_aliases: Record) { } export function sortPaths (paths: string[]) { - return paths.sort((a, b) => - (b.split('/').length - a.split('/').length) || (b.length - a.length) - ) + return paths.sort(comparePaths) } +export const comparePaths = (a: string, b: string) => (b.split('/').length - a.split('/').length) || (b.length - a.length) + const FILENAME_RE = /(?<=^|\/)([^/]+?)(?=(\.[^.]+)?$)/ export function filename (path: string) { From 2fe4daf74529e7638d875ea5378ea8d2261a53ae Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Aug 2022 13:08:57 +0100 Subject: [PATCH 05/14] test: add tests and fix issues highlighted therein --- src/utils.ts | 7 ++++-- test/utils.spec.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 test/utils.spec.ts diff --git a/src/utils.ts b/src/utils.ts index eae0931..6498587 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,7 +4,10 @@ export function resolveAliases (_aliases: Record) { // Resolve alias values in relation to each other for (const key in aliases) { for (const alias in aliases) { - if (alias === '@' && !aliases[key].startsWith('@/')) { continue } // Don't resolve @foo/bar + // Don't resolve @foo/bar + if (alias === '@' && !aliases[key].startsWith('@/')) { continue } + // don't resolve a more specific alias with regard to a less specific one + if (alias === key || key.startsWith(alias)) { continue } if (aliases[key].startsWith(alias)) { aliases[key] = aliases[alias] + aliases[key].slice(alias.length) @@ -20,7 +23,7 @@ export function sortPaths (paths: string[]) { export const comparePaths = (a: string, b: string) => (b.split('/').length - a.split('/').length) || (b.length - a.length) -const FILENAME_RE = /(?<=^|\/)([^/]+?)(?=(\.[^.]+)?$)/ +const FILENAME_RE = /(?<=^|[\\/])([^\\/]+?)(?=(\.[^.]+)?$)/ export function filename (path: string) { return path.match(FILENAME_RE)?.[0] diff --git a/test/utils.spec.ts b/test/utils.spec.ts new file mode 100644 index 0000000..35ea9d2 --- /dev/null +++ b/test/utils.spec.ts @@ -0,0 +1,58 @@ +import { describe, expect, it } from 'vitest' + +import { resolveAliases, sortPaths, comparePaths, filename } from '../src/utils' + +describe('resolveAliases', () => { + it('should work', () => { + expect(resolveAliases({ + '@foo/bar': '@foo/bar/dist/index.mjs', + '@foo/bar/utils': '@foo/bar/dist/utils.mjs', + '@': '/root' + })).toMatchInlineSnapshot(` + { + "@": "/root", + "@foo/bar": "@foo/bar/dist/index.mjs", + "@foo/bar/utils": "@foo/bar/dist/utils.mjs", + } + `) + }) +}) + +describe('sortPaths', () => { + it('should work', () => { + expect(sortPaths(['a/foo', 'a/foo-barbaz', 'a/foo/bar', 'a/b/c'])).toMatchInlineSnapshot(` + [ + "a/foo/bar", + "a/b/c", + "a/foo-barbaz", + "a/foo", + ] + `) + }) +}) + +describe('comparePaths', () => { + it('should work', () => { + expect(comparePaths('foo', 'foo/bar')).toEqual(1) + }) +}) + +describe('filename', () => { + const files = { + // POSIX + 'test.html': 'test', + '/temp/myfile.html': 'myfile', + './myfile.html': 'myfile', + + // Windows + 'C:\\temp\\': undefined, + 'C:\\temp\\myfile.html': 'myfile', + '\\temp\\myfile.html': 'myfile', + '.\\myfile.html': 'myfile' + } + for (const file in files) { + it(file, () => { + expect(filename(file)).toEqual(files[file]) + }) + } +}) From 5dc2511981c08cecb51103c20f419106fcc10302 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Aug 2022 13:10:37 +0100 Subject: [PATCH 06/14] chore: revert build config --- build.config.ts | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 build.config.ts diff --git a/build.config.ts b/build.config.ts deleted file mode 100644 index e6a13ae..0000000 --- a/build.config.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { defineBuildConfig } from 'unbuild' -export default defineBuildConfig({ - externals: ['pathe'] -}) From de08e145eee6c17dbee96aebb7277febd98c793e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Aug 2022 13:11:50 +0100 Subject: [PATCH 07/14] fix: revert unneeded tsconfig changes --- tsconfig.json | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index f9f3e13..a6b3f69 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,16 +3,8 @@ "target": "ESNext", "module": "ESNext", "moduleResolution": "Node", - "esModuleInterop": true, "skipLibCheck": true, - "paths": { - "pathe/utils": [ - "./src/utils" - ], - "pathe": [ - "./src/index" - ] - }, + "esModuleInterop": true }, "include": [ "src", From 4cbd768a99526d8e6b182b1cf7510b8bc5baa0a1 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Aug 2022 13:46:37 +0100 Subject: [PATCH 08/14] test: add test for path replacement --- test/utils.spec.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 35ea9d2..53debab 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -7,12 +7,14 @@ describe('resolveAliases', () => { expect(resolveAliases({ '@foo/bar': '@foo/bar/dist/index.mjs', '@foo/bar/utils': '@foo/bar/dist/utils.mjs', - '@': '/root' + '@': '/root', + bingpot: '@/bingpot/index.ts' })).toMatchInlineSnapshot(` { "@": "/root", "@foo/bar": "@foo/bar/dist/index.mjs", "@foo/bar/utils": "@foo/bar/dist/utils.mjs", + "bingpot": "/root/bingpot/index.ts", } `) }) From 081be23818e59a0dd774fd09411be7bbeb85c06c Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 10 Aug 2022 11:35:50 +0100 Subject: [PATCH 09/14] fix: require strict slash to match alias --- src/utils.ts | 6 +++--- test/utils.spec.ts | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 6498587..27e68cb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,15 +1,15 @@ +const pathSeparators = ['/', '\\', undefined] + export function resolveAliases (_aliases: Record) { // Sort aliases from specific to general (ie. fs/promises before fs) const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) => comparePaths(a, b))) // Resolve alias values in relation to each other for (const key in aliases) { for (const alias in aliases) { - // Don't resolve @foo/bar - if (alias === '@' && !aliases[key].startsWith('@/')) { continue } // don't resolve a more specific alias with regard to a less specific one if (alias === key || key.startsWith(alias)) { continue } - if (aliases[key].startsWith(alias)) { + if (aliases[key].startsWith(alias) && pathSeparators.includes(aliases[key][alias.length])) { aliases[key] = aliases[alias] + aliases[key].slice(alias.length) } } diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 53debab..938e7c3 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -8,13 +8,15 @@ describe('resolveAliases', () => { '@foo/bar': '@foo/bar/dist/index.mjs', '@foo/bar/utils': '@foo/bar/dist/utils.mjs', '@': '/root', - bingpot: '@/bingpot/index.ts' + bingpot: '@/bingpot/index.ts', + unchanged: '@bingpot/index.ts' })).toMatchInlineSnapshot(` { "@": "/root", "@foo/bar": "@foo/bar/dist/index.mjs", "@foo/bar/utils": "@foo/bar/dist/utils.mjs", "bingpot": "/root/bingpot/index.ts", + "unchanged": "@bingpot/index.ts", } `) }) From 6cd41bf9e2fc12afe59d5b79204a2a2ce8cae3e4 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 10 Aug 2022 11:36:33 +0100 Subject: [PATCH 10/14] refactor: rename to `normalizeAliases` --- src/utils.ts | 2 +- test/utils.spec.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 27e68cb..7afbc32 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,6 @@ const pathSeparators = ['/', '\\', undefined] -export function resolveAliases (_aliases: Record) { +export function normalizeAliases (_aliases: Record) { // Sort aliases from specific to general (ie. fs/promises before fs) const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) => comparePaths(a, b))) // Resolve alias values in relation to each other diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 938e7c3..c4cc6e7 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -1,10 +1,10 @@ import { describe, expect, it } from 'vitest' -import { resolveAliases, sortPaths, comparePaths, filename } from '../src/utils' +import { normalizeAliases, sortPaths, comparePaths, filename } from '../src/utils' -describe('resolveAliases', () => { +describe('normalizeAliases', () => { it('should work', () => { - expect(resolveAliases({ + expect(normalizeAliases({ '@foo/bar': '@foo/bar/dist/index.mjs', '@foo/bar/utils': '@foo/bar/dist/utils.mjs', '@': '/root', From c12bb81e1658bc81f19163fb881b9661af8ab21a Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 10 Aug 2022 12:46:40 +0200 Subject: [PATCH 11/14] add failing case --- test/utils.spec.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/utils.spec.ts b/test/utils.spec.ts index c4cc6e7..948d397 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -33,6 +33,18 @@ describe('sortPaths', () => { ] `) }) + + it.todo('compare unrelated prefixes', () => { + expect(sortPaths([ + 'path/to/1.intro/foo/bar', + 'path/to/2.guide/foo/bar/baz', + ])).toMatchInlineSnapshot(` + [ + "path/to/2.guide/foo/bar/baz", + "path/to/1.intro/foo/bar", + ] + `) + }) }) describe('comparePaths', () => { From 40ae6ed085a1b51614858cd5c6235fc773d4faf7 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 10 Aug 2022 11:54:32 +0100 Subject: [PATCH 12/14] refactor: rename to `sortAliases` --- src/utils.ts | 10 +++++----- test/utils.spec.ts | 18 ++++++------------ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 7afbc32..c596a61 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,8 +1,8 @@ const pathSeparators = ['/', '\\', undefined] -export function normalizeAliases (_aliases: Record) { +export function resolveAliases (_aliases: Record) { // Sort aliases from specific to general (ie. fs/promises before fs) - const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) => comparePaths(a, b))) + const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) => compareAliases(a, b))) // Resolve alias values in relation to each other for (const key in aliases) { for (const alias in aliases) { @@ -17,11 +17,11 @@ export function normalizeAliases (_aliases: Record) { return aliases } -export function sortPaths (paths: string[]) { - return paths.sort(comparePaths) +export function sortAliases (paths: string[]) { + return paths.sort(compareAliases) } -export const comparePaths = (a: string, b: string) => (b.split('/').length - a.split('/').length) || (b.length - a.length) +const compareAliases = (a: string, b: string) => b.split('/').length - a.split('/').length const FILENAME_RE = /(?<=^|[\\/])([^\\/]+?)(?=(\.[^.]+)?$)/ diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 948d397..b4f5f8a 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -1,10 +1,10 @@ import { describe, expect, it } from 'vitest' -import { normalizeAliases, sortPaths, comparePaths, filename } from '../src/utils' +import { resolveAliases, sortAliases, filename } from '../src/utils' -describe('normalizeAliases', () => { +describe('resolveAliases', () => { it('should work', () => { - expect(normalizeAliases({ + expect(resolveAliases({ '@foo/bar': '@foo/bar/dist/index.mjs', '@foo/bar/utils': '@foo/bar/dist/utils.mjs', '@': '/root', @@ -22,14 +22,14 @@ describe('normalizeAliases', () => { }) }) -describe('sortPaths', () => { +describe('sortAliases', () => { it('should work', () => { - expect(sortPaths(['a/foo', 'a/foo-barbaz', 'a/foo/bar', 'a/b/c'])).toMatchInlineSnapshot(` + expect(sortAliases(['a/foo', 'a/foo-barbaz', 'a/foo/bar', 'a/b/c'])).toMatchInlineSnapshot(` [ "a/foo/bar", "a/b/c", - "a/foo-barbaz", "a/foo", + "a/foo-barbaz", ] `) }) @@ -47,12 +47,6 @@ describe('sortPaths', () => { }) }) -describe('comparePaths', () => { - it('should work', () => { - expect(comparePaths('foo', 'foo/bar')).toEqual(1) - }) -}) - describe('filename', () => { const files = { // POSIX From d316d6cbb01006d939920bbc42e05b5ba6708073 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 10 Aug 2022 11:55:54 +0100 Subject: [PATCH 13/14] test: remove failing case --- test/utils.spec.ts | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/test/utils.spec.ts b/test/utils.spec.ts index b4f5f8a..aab6fc2 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -33,18 +33,6 @@ describe('sortAliases', () => { ] `) }) - - it.todo('compare unrelated prefixes', () => { - expect(sortPaths([ - 'path/to/1.intro/foo/bar', - 'path/to/2.guide/foo/bar/baz', - ])).toMatchInlineSnapshot(` - [ - "path/to/2.guide/foo/bar/baz", - "path/to/1.intro/foo/bar", - ] - `) - }) }) describe('filename', () => { From 2b053d6a5b6ef96a72fe8843b48e125b106de93e Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 10 Aug 2022 16:51:25 +0200 Subject: [PATCH 14/14] refactor: only expose normalizeAliases --- src/utils.ts | 16 ++++++++-------- test/utils.spec.ts | 19 +++---------------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index c596a61..f0ae8ae 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,8 +1,8 @@ const pathSeparators = ['/', '\\', undefined] -export function resolveAliases (_aliases: Record) { +export function normalizeAliases (_aliases: Record) { // Sort aliases from specific to general (ie. fs/promises before fs) - const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) => compareAliases(a, b))) + const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) => _compareAliases(a, b))) // Resolve alias values in relation to each other for (const key in aliases) { for (const alias in aliases) { @@ -17,14 +17,14 @@ export function resolveAliases (_aliases: Record) { return aliases } -export function sortAliases (paths: string[]) { - return paths.sort(compareAliases) -} - -const compareAliases = (a: string, b: string) => b.split('/').length - a.split('/').length - const FILENAME_RE = /(?<=^|[\\/])([^\\/]+?)(?=(\.[^.]+)?$)/ export function filename (path: string) { return path.match(FILENAME_RE)?.[0] } + +// --- internals --- + +function _compareAliases (a: string, b: string) { + return b.split('/').length - a.split('/').length +} diff --git a/test/utils.spec.ts b/test/utils.spec.ts index aab6fc2..93dd605 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -1,10 +1,10 @@ import { describe, expect, it } from 'vitest' -import { resolveAliases, sortAliases, filename } from '../src/utils' +import { normalizeAliases, filename } from '../src/utils' -describe('resolveAliases', () => { +describe('normalizeAliases', () => { it('should work', () => { - expect(resolveAliases({ + expect(normalizeAliases({ '@foo/bar': '@foo/bar/dist/index.mjs', '@foo/bar/utils': '@foo/bar/dist/utils.mjs', '@': '/root', @@ -22,19 +22,6 @@ describe('resolveAliases', () => { }) }) -describe('sortAliases', () => { - it('should work', () => { - expect(sortAliases(['a/foo', 'a/foo-barbaz', 'a/foo/bar', 'a/b/c'])).toMatchInlineSnapshot(` - [ - "a/foo/bar", - "a/b/c", - "a/foo", - "a/foo-barbaz", - ] - `) - }) -}) - describe('filename', () => { const files = { // POSIX